- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding the immediate next character to a letter in string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string of characters, str, and a single character, char.
Our function should construct a new string that contains the immediate next character present in str after each instance of char (if any).
Example
Following is the code −
const str = 'this is a string'; const letter = 'i'; const findNextString = (str = '', letter = '') => { let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; const next = str[i + 1]; if(letter === el && next){ res += next; }; }; return res; }; console.log(findNextString(str, letter));
Output
ssn
Advertisements