Site icon T4Tutorials.com

JavaScript Program to Replace String with a number with Algorithm and Pseudocode

JavaScript Program to Replace String with a number with Algorithm and Pseudocode

JavaScript Program to Replace String with a number with Algorithm and Pseudocode is the today topic of discussion in this tutorial.

JavaScript Program to Replace String with a number with Algorithm and Pseudocode

Pseudo code of Replacing the String with number

button onclick="replace()">
function replace() {
  var str = document.getElementById("demo").innerHTML;
  var res = str.replace("Fourteen", "14");
  document.getElementById("demo").innerHTML = res;

Program to Replace String with number

<!DOCTYPE html>
<html>
<Title>String Replace</Title>
<body>
<h1>Click the button to replace Fourteen with its numeric form </h1>
<h2 id="demo">Independence Day of Pakistan is Fourteen August</h2>
<button onclick="replace()">Replace</button>
<script>
function replace() {
  var str = document.getElementById("demo").innerHTML;
  var res = str.replace("Fourteen", "14");
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Explanation

In the Following JavaScript Code, we have converted a string into number by using replace (Built-in ) function of javascript, by using this function developer can replace string with another string or String with a number.

For Example in JavaScript Code written above, we have replaced Fourteen which is a String into its numeric form as 14 by using replace function.

The main code is in the button named Replace as by pressing the button string is converted into a number, basically, when the button is pressed the function is called in which built-in function replace() is used to replace string with another string or any number.

Syntax

string.replace(value, newvalue)

Exit mobile version