-2

I'm new to programming. I've learned a little bit of JavaScript and I made a code. It was all fine when everything was true, but I needed to add something to happen when it is false. So I added a while and it didn't work out for me. Someone can help me please? Thank you for the answers!

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8"/>
    <link rel="stylesheet" href="styles.css"/>
    <script src="JavaScript.js"></script>
    <title>
      Hello
    </title>
  </head>
  <body>
    <p id="Hello"></p>
    <script>
      function onload() {
      alert("choose a number between 1 and 9");
      var Multiply1 = prompt("Multiply this number by 9");
      
      while (Multiply1 % 9 != 0) {
      alert("This can't be true");
      var Multiply = prompt("Multiply this number by 9");
      }
      
      while (multiply1 % 9 = 0) {
      var add = prompt("Add the 2 digits you have");
      }
      
      while (add != 9) {
      alert("This can't be true");
      var add1 = prompt("Add the 2 digits you have");
      }
      
      while (add = 9) {
      var decrease = prompt("deacrease the number you have chosen from the number you got:");
      alert("the number you have chosen is " + (9-decrease));
      
      }
      }
      
    </script>
    <noscript>Please enable JS</noscript>
    <button onclick="onload()">Cilck on me to start"</button>
  </body>
</html>
SLePort
  • 15,211
  • 3
  • 34
  • 44
Or Gold
  • 23
  • 1
  • 5
  • 3
    Welcome to Stack Overflow. Please read http://stackoverflow.com/help/how-to-ask which will help you how to ask questions well. As it stands, this question is in the category "Why doesn't this code work?", which will be removed. We'd like you to show effort in trying to solve a problem, not just show us the code and ask why it doesn't work. – Mikkel Dec 17 '16 at 07:29

3 Answers3

0

I see a few issues - first is that Javascript is case sensitive so you need to use the exact case for variables - Multiply1 != multiply1. The next is that a while loop will continue as long as the condition is true, so you are getting stuck inside whichever loop evaluates to true first. Instead you should have a single outer loop with internal if condition.

function myFunction() {

  var myValue = 0;

  // this will run until "myValue" equals "something"
  while(myValue !== something) {
    if (myValue < 9) {
      // update the value if it is less than 9
    }

    if (myValue === 10) {
      // update the value if it is equal to 10
    }
  }

  return myValue;
}
doublesharp
  • 26,888
  • 6
  • 52
  • 73
0

Try to put the false cases inside if statement.No need for separate while loops for true and false.Two while loops will suffice.

0

You should avoid calling javascript functions in html like this:

<button onclick="onload()">

Why is using onClick() in HTML a bad practice?

Community
  • 1
  • 1
  • Welcome to SO as a new user. Nice: new user answers new user, but the question didn't conform on [how to ask](https://stackoverflow.com/help/on-topic) You may answer nevertheless. –  Dec 17 '16 at 13:47