Replace all '0' with '5' in an Input Integer using JavaScript
Last Updated :
13 May, 2024
Given a number, our task is to take an input integer and replace all occurrences of the digit '0' with the digit '5'. We can solve this problem by using various methods in JavaScript.
Example:
Input:
n = 1020
Output:
1525
Below are the approaches to replace all '0' with '5' in an input integer using JavaScript:
String Conversion and Manipulation
This approach converts the input integer into a string using the toString() method. Then, it replaces all occurrences of '0' with '5' using the replace() method with a regular expression /0/g. Finally, it parses the modified string back to an integer using parseInt() with a radix of 10 for decimal conversion.
Example: The example below shows the replacement of all '0' with '5' in an input integer using String Conversion and Manipulation.
JavaScript
function replaceZeros(n) {
let str = n.toString();
str = str.replace(/0/g, '5');
return parseInt(str, 10);
}
const n = 1020;
console.log(replaceZeros(n));
Time Complexity: O(n), where n is the number of digits in the integer.
Space Complexity: O(n).
Mathematical Manipulation
The function replaceZerosWithMath iterates through each digit of the input integer 'n', replaces '0' with '5', and constructs the resulting integer using mathematical manipulation. It initializes 'result' to 0 and 'multiplier' to 1, then iterates through each digit of 'n', replacing '0' with '5' and building the result by multiplying each digit with 'multiplier' and adding it to 'result'. Finally, it returns the resulting integer after processing all digits.
Example: The example below illustrates the replacement of all '0' with '5' in an input integer using Mathematical Manipulation.
JavaScript
function replaceZeros(n) {
let result = 0;
let multiplier = 1;
while (n > 0) {
let digit = n % 10;
if (digit === 0) digit = 5;
result += digit * multiplier;
multiplier *= 10;
n = Math.floor(n / 10);
}
return result;
}
const n = 1020;
console.log(replaceZeros(n));
Time Complexity: O(n), where n is the number of digits in the integer.
Space Complexity: O(1).
Iterative with Reversal
The function 'replaceZeroWithFive' iterates through each digit of the input integer 'num'. It replaces every '0' digit with '5' by building a temporary integer 'temp' with reversed digits containing '5' instead of '0'. It then reverses 'temp' to obtain the final result integer 'reversed' and returns it.
Example: The example below shows the replacement of all '0' with '5' in an input integer using the Iterative with Reversal method.
JavaScript
function replaceWithFive(num) {
let temp = 0;
while (num > 0) {
let digit = num % 10;
if (digit === 0) {
digit = 5;
}
temp = temp * 10 + digit;
num = Math.floor(num / 10);
}
let reversed = 0;
while (temp > 0) {
reversed = reversed * 10 + temp % 10;
temp = Math.floor(temp / 10);
}
return reversed;
}
console.log(replaceWithFive(9570320003));
Time Complexity: O(n), where n is the number of digits in the integer.
Space Complexity: O(1).
Recursion
The function replaceZeroWithFive recursively replaces each '0' digit in the input integer num with '5'. It checks if num is equal to 0, in which case it returns 5. Otherwise, it extracts the last digit of the num, replaces '0' with '5' if necessary, and recursively calls itself with the remaining digits. Finally, it concatenates the modified digit with the result of the recursive call to build the final integer with '0' replaced by '5'.
Example: The example below shows the replacement of all '0' with '5' in an input integer using the Iterative with the Recursion method.
JavaScript
function replaceWithFive(num) {
if (num === 0) {
return 5;
}
const digit = num % 10;
const modifiedDigit = digit === 0 ? 5 : digit;
return replaceWithFive(Math.floor(num / 10)) * 10 + modifiedDigit;
}
console.log(replaceWithFive(0304003092));
Time Complexity: O(n), where n is the number of digits in the integer.
Space Complexity: O(n).
Similar Reads
Remove all occurrences of a character in a string using JavaScript
These are the following ways to remove all occurrence of a character from a given string: 1. Using Regular ExpressionUsing a regular expression, we create a pattern to match all occurrences of a specific character in a string and replace them with an empty string, effectively removing that character
2 min read
JavaScript Program to Set a Particular Bit in a Number
Setting a specific bit at a given position within a number using JavaScript involves manipulating the binary representation of the number to ensure that a particular bit is turned on set to 1. Examples: Input : n = 10, k = 2Output :14ApproachThe variable "bitPosition" determines the index of the bit
2 min read
Javascript Program to Convert Integer to Roman Numerals
Given an Integer number, the task is to convert the Integer to a Roman Number in JavaScript. Roman numerals are a numeral system that originated in ancient Rome and remained the usual way of writing numbers throughout Europe well into the Late Middle Ages. Table of Content Using a Lookup ObjectUsing
2 min read
JavaScript Program for Double to String Conversion
Converting a double (floating-point) number to a string in JavaScript involves transforming the numeric value into its corresponding string representation. This process is essential when dealing with numeric data that needs to be displayed or manipulated as strings, such as in user interfaces or dat
1 min read
Addition of Two Fractions using JavaScript
Fraction numbers in math are representations of parts of a whole, consisting of a numerator (the part being considered) and a denominator (the total number of equal parts). Add two fractions a/b and c/d and print the answer in simplest form. Examples: Input: 1/2 + 3/2Output: 2/1Input: 1/3 + 3/9Outpu
3 min read
JavaScript Program to Convert a Number to Binary
We are going to learn the conversion of a number to binary by using JavaScript, Convert a number to binary in JavaScript refers to the process of converting a decimal number into its binary representation, which uses only the digits 0 and 1 to represent the value Converting a number to binary in Jav
3 min read
JavaScript Program for String to Long Conversion
In JavaScript, there is no specific data type called "Long", but it uses the "Number" to represent the integer values. We can indirectly convert the String to Long by converting them to Number. There are several approaches in Javascript to convert strings to long which are as follows: Table of Conte
2 min read
JavaScript Program to Convert Float to String
Here are the various methods to convert float to string in JavaScript 1. Using String() FunctionThe String() function is one of the most commonly used and simplest ways to convert a float to a string. It works by converting the given value into its string representation. [GFGTABS] JavaScript let n =
3 min read
JavaScript Program for Min flips of continuous characters to make all characters same in a String
In this article, we will learn about Min flips of continuous characters to make all characters the same in a string using JavaScript. Min flips of continuous characters in a string refer to the minimum number of changes required to turn a sequence of adjacent characters into a uniform sequence, ensu
3 min read
JavaScript Program to Convert a String to Roman Numerals
In this article, we will see how to convert a string to Roman numerals using JavaScript. Converting a string to Roman numerals in JavaScript is a common task when working with text data containing numeric values in Roman numeral format. Roman numerals are a numeral system used in ancient Rome, and t
4 min read