In: Computer Science
I need to create a web page(JAVASCRIPT) that allows the user to reverse a string. For Example, if the user types Mirror Image, after the button is clicked, the text will say egamI rorriM.You have to implement each button by using a while, do while and for loop.
here is a default code
<html>
<head>
<title>Mirror</title>
</head>
<body>
<p id="label1">Original Text: <h1
id="text1">Some text goes here!!!</h1></p>
<p id="label2">Mirror Text: <h1
id="text2">!!!ereh seog txet emoS</h1>
<input type="button" value="Reverse (while loop)"
/><br />
<input type="button" value="Reverse (do while
loop)" /><br />
<input type="button" value="Reverse (for loop)"
/><br />
</body>
</html>
Code:
<html>
<head>
<title>Mirror</title>
</head>
<body>
<p id="label1">Original Text: <h1 id="text1">Some text
goes here!!!</h1></p>
<!-- for user input here input tag is used with id = inp_text
-->
<!-- NOTE: Provided code is kept as it is if needed changes can
be accordingly -->
<b>Type here: </b><input type="text" name="inp_text"
id="inp_text">
<p id="label2">Mirror Text: <h1 id="text2">!!!ereh seog
txet emoS</h1>
<!-- p tag is used to print the ouptut
produced by functions -->
<p id="output"></p>
<!-- respective functions are called using onclick property
of button -->
<input type="button" value="Reverse (while loop)"
onclick="func1()"/><br />
<input type="button" value="Reverse (do while loop)"
onclick="func2()"/><br />
<input type="button" value="Reverse (for loop)"
onclick="func3()"/><br />
<script>
// Defining function to reverse a string using while
loop
function func1() {
// getting user input value by using id
var x = document.getElementById("inp_text").value;
// initializing loop counter to length - 1 (last index of
string)
var i = x.length-1;
// creating variable to store reverse
string
var revString = "";
while(i>=0)
{
// adding characters of input string from last to
first
revString += x[i];
i--;
}
// printing reversed string as output
document.getElementById(
"output").innerHTML = revString;
}
// Defining function to reverse a string using do while
loop
function func2() {
// getting user input value by using id
var x = document.getElementById("inp_text").value;
// initializing loop counter to length - 1 (last index of
string)
var i = x.length-1;
// creating variable to store reverse
string
var revString = "";
// using do while loop to reverse string
do
{
// adding characters of input string from last to
first
revString += x[i];
i--;
}while(i>=0);
// printing reversed string as output
document.getElementById(
"output").innerHTML = revString;
}
// Defining function to reverse a string using for loop
function func3() {
// getting user input value by using id
var x = document.getElementById("inp_text").value;
// loop counter variable
var i;
// creating variable to store reverse
string
var revString = "";
// using for loop to reverse string
for(i=x.length-1; i>=0; i--)
{
revString += x[i];
}
// printing reversed string as output
document.getElementById(
"output").innerHTML = revString;
}
</script>
</body>
</html>
Code Snippet:
<html>
<head>
<title>Mirror</title>
</head>
<body>
<p id="label1">Original Text: <h1 id="text1">Some text goes here!!!</h1></p>
<!-- for user input here input tag is used with id = inp_text -->
<!-- NOTE: Provided code is kept as it is if needed changes can be accordingly -->
<b>Type here: </b><input type="text" name="inp_text" id="inp_text">
<p id="label2">Mirror Text: <h1 id="text2">!!!ereh seog txet emoS</h1>
<!-- p tag is used to print the ouptut produced by functions -->
<p id="output"></p>
<!-- respective functions are called using onclick property of button -->
<input type="button" value="Reverse (while loop)" onclick="func1()"/><br />
<input type="button" value="Reverse (do while loop)" onclick="func2()"/><br />
<input type="button" value="Reverse (for loop)" onclick="func3()"/><br />
<script>
// Defining function to reverse a string using while loop
function func1() {
// getting user input value by using id
var x = document.getElementById("inp_text").value;
// initializing loop counter to length - 1 (last index of string)
var i = x.length-1;
// creating variable to store reverse string
var revString = "";
while(i>=0)
{
// adding characters of input string from last to first
revString += x[i];
i--;
}
// printing reversed string as output
document.getElementById(
"output").innerHTML = revString;
}
// Defining function to reverse a string using do while loop
function func2() {
// getting user input value by using id
var x = document.getElementById("inp_text").value;
// initializing loop counter to length - 1 (last index of string)
var i = x.length-1;
// creating variable to store reverse string
var revString = "";
// using do while loop to reverse string
do
{
// adding characters of input string from last to first
revString += x[i];
i--;
}while(i>=0);
// printing reversed string as output
document.getElementById(
"output").innerHTML = revString;
}
// Defining function to reverse a string using for loop
function func3() {
// getting user input value by using id
var x = document.getElementById("inp_text").value;
// loop counter variable
var i;
// creating variable to store reverse string
var revString = "";
// using for loop to reverse string
for(i=x.length-1; i>=0; i--)
{
revString += x[i];
}
// printing reversed string as output
document.getElementById(
"output").innerHTML = revString;
}
</script>
</body>
</html>
Output Snippet:
I hope you got the provided solution.
Thank You.