- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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?
- Related Articles
- Contiguous subarray with 0 and 1 in JavaScript
- Replacing strings with numbers in Python for Data Analysis
- Replacing spaces with underscores in JavaScript?
- Replacing vowels with their 1-based index in a string in JavaScript
- JavaScript: replacing object keys with an array
- Replacing zero starting with whitespace in JavaScript
- Replacing dots with dashes in a string using JavaScript
- Construct DFA with Σ= {0,1} accepts all strings with 0.
- Find duplicates in constant array with elements 0 to N-1 in O(1) space in C++
- Show that:(i) $tan 48^o tan 23^o tan 42^o tan 67^o = 1$(ii) $cos 38^o cos 52^o - sin 38^o sin 52^o = 0$
- Mode 1—strobed I/O
- Replacing words with asterisks in C++
- Replacing all special characters with their ASCII value in a string - JavaScript
- Linux and Unix Test Disk I/O Performance with DD Command
- Design DFA for language over {0,1} accepting strings with odd number of 1’s and even number of 0’s

Advertisements