 
  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
JavaScript Strings: Replacing i with 1 and o with 0
We are required to write a function that takes in a string as one and only argument and returns another string that has all ‘i’ and ‘o’ replaced with ‘1’ and ‘0’ respectively.
It’s one of those classic for loop problems where we iterate over the string with its index and construct a new string as we move through.
The code for the function will be −
const string = 'Hello, is it raining in Amsterdam?'; const validate = (str) => {    let validatedString = '';    for(let i = 0; i < str.length; i++){       if(str[i] === 'a'){          validatedString += '@';       }else if(str[i] === 'i'){          validatedString += '!';       }else{          validatedString += str[i];       };    };    return validatedString; }; console.log(validate(string)); The console output for the code will be −
Hello, !s !t r@!n!ng !n Amsterd@m?
Advertisements
 