My if statement gives me the wrong output

Viewed 126

window.onload = start;


function start() {

  document.getElementById("kalk").onclick = find;
  find(1, 9999);
}

function find(min, max) {
  var factor = document.getElementById("tall").value;
  var factor2 = document.getElementById("tall2").value;

  var x = factor * factor2;

  document.getElementById("utskrift").innerHTML = x;

  if (x >= min && x <= max) {
    document.getElementById("msg").innerHTML = "Number is in interval."
  } else {
    document.getElementById("msg").innerHTML = "Number is not in interval."
  }
}
<h2>Gang to tall</h2>

T1 <input type="number" id="tall" /> T2 <input type="number" id="tall2" />

<button id="kalk">Finn tall i intervall</button> Sum: <span id="utskrift"></span>

<p id="msg"></p>

So by reading this code.. what Im trying to do is have two inputs where i multiply the numbers typed in them. In my "Find()" Parameter i have two arguments that says the numbers should be between 1-9999. In my "function find" i called these arguments min and max. Further down the code Im asking if the output is between these numbers are between min and max give "Number is in interval". The problem is that when i even when the numbers are in these arguments i get my else statement. Is there anyway to fix this or put an input felt in the parameter?

Thanks

2 Answers

You are attaching the function find directly to the click event listener. The function is expecting two parameters min and max:

function find(min, max)

But when a click happens, it recieves, being an event listener, only one parameter which is the event object. Thus min is going to be an event object, and max will be undefined and your if statement won't work. You can check this out by logging min and max inside find to the console.

Wrap the function find call in another function and attach the latter as the event listener:

document.getElementById("kalk").onclick = function(event) { // the event listener function recieves the event object as the only parameter (not that argument event here is not necessary, I just added it for explanation)
   find(1, 90000);                                          // when this function get executed (when a click happens) then call find with proper parameters
}

Well, first you have to remember that the "T" in HTML stands for "text". There is only one data type in HTML... strings. When you get a value from HTML and want to use it as a number in JavaScript, you have to convert it to a number.

Next, you don't really have your functions organized correctly. find shouldn't just run as soon as the page is loaded. You want it to run when the button is clicked. And that way, the min and max values are passed at the moment you need them.

Lastly, you HTML statically says "Sum" when, you are in fact calculating a product.

See other best practice items in the comments.

<html>

<head>

</head>

<body>
  <h2>Gang to tall</h2>

T1 <input type="number" id="tall"/>
T2 <input type="number" id="tall2"/>

<button id="kalk">Finn tall i intervall</button>

Product: <span id="utskrift"></span>

<p id="msg"></p>

<!-- It's a good idea to place your scripts just before the closing
     body tag. That way, the HTML has been loaded by the time the
     JavaScript runs. -->
<script>
  document.getElementById("kalk").onclick = result;
  
  // Get your DOM references just once so you don't have to re-scan the DOM
  // for them every time the button is clicked. 
  
  // Also, just reference the elements themselves, not properties (.value) of
  // the element because if you just reference the property and later decide 
  // you need some other property, you'll have to re-scan the DOM for the same
  // element again. This way, you maintain a reference to the element and can
  // get any property you need at any time.
  var factor = document.getElementById("tall");
  var factor2 = document.getElementById("tall2");  
  var product = document.getElementById("utskrift");
  var msg = document.getElementById("msg");

  function find(min,max){
    // All values coming from HTML are strings. You should always 
    // explicitly convert them to numbers when numbers are expected.
    var x = parseInt(factor.value, 10) * parseInt(factor2.value,10);

    // .innerHTML is for when you are assigning a string that contains HTML.
    // It tells the HTML parser to parse the string for HTML. If you are not
    // including HTML, use .textContent, which doesn't do this extra parsing.
    product.textContent = x;
    if (x >= min && x <= max) {
      msg.textContent = "Number is in interval."
    } else {
      msg.textContent = "Number is not in interval."
    }
}

function result(){
  find(1, 9999);
}

</script>
</body>
</html>

Related