

- 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
Change every letter to next letter - JavaScript
We are required to write a JavaScript function that takes in a string and changes every letter of the string from the English alphabets to its succeeding element.
For example: If the string is −
const str = 'how are you';
Then the output should be −
const output = 'ipx bsf zpv'
Example
Following is the code −
const str = 'how are you'; const isAlpha = code => (code >= 65 && code <= 90) || (code >= 97 && code <= 122); const isLast = code => code === 90 || code === 122; const nextLetterString = str => { const strArr = str.split(''); return strArr.reduce((acc, val) => { const code = val.charCodeAt(0); if(!isAlpha(code)){ return acc+val; }; if(isLast(code)){ return acc+String.fromCharCode(code-25); }; return acc+String.fromCharCode(code+1); }, ''); }; console.log(nextLetterString(str));
Output
Following is the output in the console −
ipx bsf zpv
- Related Questions & Answers
- Finding the immediate next character to a letter in string using JavaScript
- Repeating letter string - JavaScript
- JavaScript Count repeating letter
- How to change the Drive letter using PowerShell?
- Convert number to alphabet letter JavaScript
- Program to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript
- Finding letter distance in strings - JavaScript
- Missing letter from strings in JavaScript
- Finding missing letter in a string - JavaScript
- Swapping letter with succeeding alphabet in JavaScript
- Capitalizing first letter of each word JavaScript
- Delete every one of the two strings that start with the same letter in JavaScript
- Letter Spacing using CSS
- Letter Spacing in CSS
- Replace a letter with its alphabet position JavaScript
Advertisements