The if if statement gives me the wrong output

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>
Run codeHide result

So, after reading this code ... what I'm trying to do is two inputs in which I multiply the numbers entered into them. In my Find () parameter, I have two arguments that indicate that the numbers must be between 1-9999. In my "find function", I named these arguments min and max. Further down the code Im asking if the output between these digits is between min and max gives "Number is in interval". The problem is that when I even when the numbers are in these arguments, I get an else statement. Is it still necessary to fix this or put the input signal in the parameter?

thank

+4
source share
2 answers

find . min max:

function find(min, max)

, , , , . , min , max undefined, if . , min max find .

find :

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
}
+1

, , "" HTML "". HTML... . HTML JavaScript, .

. find . , . min max , .

, HTML "", .

. .

<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 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>
Hide result
+1

Source: https://habr.com/ru/post/1689834/


All Articles