Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
JavaScript exercises and solutions - Swap two numbers
#1
The HTML part between the <body></body> tags looks like this:

Code:
Enter the first number here:
<br>
<input type="Text" id="Num1">
<br>
Enter the second number here:
<br>
<input type="Text" id="Num2">
<br>
<input type="button" id="submit" value="submit" onclick="SwapNumbers()"></input>
<p id="Output"></p>
<input type="button" id="reset" value="Reset" onclick="Reset()"></input>

The JavaScript part looks like this:

Code:
function SwapNumbers()
{
  Num1 = document.getElementById("Num1").value;
  Num2 = document.getElementById("Num2").value;
  Num3 = Num1;
  Num4 = Num2;
  Output = "You have entered " + Num4 + " and " + Num3;
  document.getElementById("Output").innerHTML = Output;
}

function Reset()
{
  document.getElementById("Num1").value = "";
  document.getElementById("Num2").value = "";
  document.getElementById("Output").innerHTML = "";
}

Note: The reset button and the reset function are optional in case you want to delete what you were looking for in order to look for something else.
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  JavaScript exercises and solutions - Identify if a number is odd or even Qomplainerz 0 269 04-04-2023, 07:56 AM
Last Post: Qomplainerz
  JavaScript exercises and solutions - Iterating through an array with keys and values Qomplainerz 0 307 04-04-2023, 07:52 AM
Last Post: Qomplainerz
  Find the min, max and avg of two numbers in JavaScript Qomplainerz 0 250 04-04-2023, 07:38 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 1 Guest(s)