Search code examples
javascripthtmlfunctionreturnboolean

How to create function (number, from, to) with a returning interval of boolean + input&print out


I've tried searching here on Stackoverflow and on Google search engine on creating a function(number, from, to) in a interval, that returns with boolean and get their data from an input source with having the possiblity of a print out on the page. I've tried different types of combinations, but the text will not appear on the page at all. By the way, if possible I'd like to accomplish this without using php and jquery. My task is to check if a number is within the from and to interval.

Here is my code:

<!-- JS -->
        window.onload = oppstart;

        function oppstart(tall, fra, til){
          document.getElementById("btnVisSvaret").onclick=innenforInterval;
          document.getElementById("storre").innerHTML = "";
          document.getElementById("mindre").innerHTML = "";

          if (innenforInterval(parseInt(tall, parseInt(fra), parseInt(til) === true)))
          {
            document.getElementById("utskrift1").innerHTML = ("Ja! Tallet er innenfor intervallet!");
          }
          else {
            document.getElementById("utskrift2").innerHTML = ("Nei! Tallet er ikke innenfor intervallet!");
          }
        }

        function InnenforInterval(tall, fra, til){
          if(tall < til  || tall > fra){
          return false;
        }
        else {
          return true;
        }
      }
      </script>

    <p> Sjekk om et spesifikt tall er innenfor intervallet </p>

      Er tallet: <input id="siffere"/></input>
      <br></br>

      Større enn: <input id="storre"/></input>
      og mindre enn: <input id="mindre"/></input>

      <button id="btnVisSvaret" type="button"
      value="getElementById("storre")",
      value="getElementById("mindre")",
      value="getElementById("siffere")";/> Vis Svaret </button>

      <p id="utskrift1"/></p>
      <p id="utskrift2"/></p>

Solution

  • If i understand it correctly, you want to check a number if it is inside of the given interval.

    I have stripped the code a bit and now ther are only two function which makes the code working.

    Please have a look to the button, ther is no value attribute. This is an attribute of input tag.

    window.onload = oppstart;
    
    function oppstart() {
        document.getElementById("btnVisSvaret").onclick = innenforInterval;
    }
    
    function innenforInterval() {
        var tall = +document.getElementById('siffere').value,
            fra = +document.getElementById('mindre').value,
            til = +document.getElementById('storre').value;
    
        console.log(tall, fra, til);
        if (tall >= fra && tall <= til) {
            document.getElementById("utskrift1").innerHTML = "Ja! Tallet er innenfor intervallet!";
        } else {
            document.getElementById("utskrift2").innerHTML = "Nei! Tallet er ikke innenfor intervallet!";
        }
    }
    <p>Sjekk om et spesifikt tall er innenfor intervallet</p>
    Er tallet:<input id="siffere" /><br>
    Større enn: <input id="storre" />
    og mindre enn:<input id="mindre" />
    <button id="btnVisSvaret" />Vis Svaret</button>
    <p id="utskrift1" /></p>
    <p id="utskrift2" /></p>