2

I need to make a domain with the following:

  • Ask the user for his or her name.
  • Prompt the user for a number between 1 and 100 both included.
  • Prompt the user for a number between 10 and 20 both included. <
  • Give an output with the following text:

"Dear name of user the first number you entered was first number the second was second number. If we multiply these two number we get

So far I've gotten this:

function myFunction() {
  var person = prompt("Please enter your name", "enter name");
  if (person != null) {
    document.getElementById("demo").innerHTML =
      "Hello " + person + "! How are you today?";
  }

  function addNumbers() {

    var val1 = parseInt(document.getElementById("value1").value);
    val val2 = parseInt(document.getElementByID("value").value);
    var ansD = document.getElementByID("answer");
    ansD.value = val1 + val2;
  }
<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

4
  • I made you a snippet. Please fix the error seen in the console. For example add a missing bracket of the first function and spell the JS the same - ID is not Id and you are missing value, value1 and answer Commented Sep 24, 2018 at 8:37
  • 1
    Also please ask a question...and there is a difference between id and ID. Commented Sep 24, 2018 at 8:38
  • My bad. Basically I need more than one function running on the same site. So I need the users name, and the ability to put two numbers together which ends with the name of the user and the result of val1 and val2. Commented Sep 24, 2018 at 8:40
  • See my updated comment. You need to give us a minimal reproducible example - press F12 and fix all errors you see. Please update the question with an actual question. Please look at How to Ask to see what we expect Commented Sep 24, 2018 at 8:42

1 Answer 1

1

There is the code that runs as you wanted. If you want to output the result in the html page you could just simply use the innerHTML method of the id you want to change.

<!DOCTYPE html>
<html>
<body>

<p>Click the button to demonstrate the prompt box.</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
    var person = prompt("Please enter your name");
    var firstN = prompt("Please enter a number between 1 and 100");
    var secondN = prompt("Please enter a number between 10 and 20");
    document.getElementById("demo").innerHTML = "Dear "+person+" the first number you entered was "+firstN+" the second was "+secondN+". If we multiply these two number we get "+(firstN*secondN);
}
</script>

</body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

I apologize, I wasn't aware you could stack variables within the same function like that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.