
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Zig-Zag pattern in strings in JavaScript?
We need to write a function that reads a string and converts the odd indexed characters in the string to upperCase and the even ones to lowerCase and returns a new string.
Full code for doing the same will be −
const text = 'Hello world, it is so nice to be alive.'; const changeCase = (str) => { const newStr = str .split("") .map((word, index) => { if(index % 2 === 0){ return word.toLowerCase(); }else{ return word.toUpperCase(); } }) .join(""); return newStr; }; console.log(changeCase(text));
The code converts the string into an array, maps through each of its word and converts them to uppercase or lowercase based on their index.
Lastly, it converts the array back into a string and returns it.
Following is the output on console −
hElLo wOrLd, It iS So nIcE To bE AlIvE.
- Related Articles
- Convert array into Zig-Zag fashion in C++
- Print Concatenation of Zig-Zag String in n Rows in C++
- Program to convert linked list to zig-zag binary tree in Python
- Zig Zag Level order traversal of a tree using single queue in C++
- How to create a responsive zig zag (alternating) layout with CSS?
- Print matrix in zag-zag fashion in C Programming.
- Number pattern in JavaScript
- Formatted Strings Using Template Strings in JavaScript
- Count strings that end with the given pattern in C++
- Template strings in JavaScript.
- Find all strings that match specific pattern in a dictionary in C++
- Nesting template strings in JavaScript
- Differences in two strings in JavaScript
- The algorithm problem - Backtracing pattern in JavaScript
- Common Character Count in Strings in JavaScript

Advertisements