

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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
- How to create a responsive zig zag (alternating) layout with CSS?
- Zig Zag Level order traversal of a tree using single queue in C++
- Print matrix in zag-zag fashion in C Programming.
- Number pattern in JavaScript
- Count strings that end with the given pattern in C++
- Formatted Strings Using Template Strings in JavaScript
- Template strings in JavaScript.
- Find all strings that match specific pattern in a dictionary in C++
- The algorithm problem - Backtracing pattern in JavaScript
- Nesting template strings in JavaScript
- Differences in two strings in JavaScript
- How to validate a date pattern in JavaScript?
Advertisements