 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP 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
Swapping letter with succeeding alphabet in 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';
Output
Then the output should be −
const output = 'ipx bsf zpv'
Therefore, let’s write the code for this function −
Example
The code for this will be −
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)); The output in the console will be −
ipx bsf zpv
Advertisements
 