Webbserver - Love Blomberg

Show sourcecode

The following files exists in this folder. Click to view.

public_html/gamla-kurser/webbutv2/övningar/js_while/

js_while_a.html
js_while_b.html
js_while_c.html
js_while_d.html
js_while_e.html
js_while_f.html
js_while_g.html
js_while_h.html
js_while_i.html

js_while_g.html

41 lines ASCII Windows (CRLF)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>JS While G</title>
</head>
<body>
<h1>JS While G</h1>
<input type="number" id="inputRuta1" placeholder="Skriv in tal 1">
<input type="number" id="inputRuta2" placeholder="Skriv in tal 2">

<button onclick="myFunction()">Klicka mig</button>
<br><br>
<div id="divRuta"></div>
<script>
  let inputRuta1 = document.getElementById("inputRuta1");
  let inputRuta2 = document.getElementById("inputRuta2");
  let textRuta = document.getElementById("divRuta");
  function myFunction() {
    let i = parseInt(inputRuta1.value);
    let end = parseInt(inputRuta2.value);
    let resultat = 0;
    let calc = 0;
    let add = true;
    while (i <= end) {
      if (add) {
        resultat += i;
        calc += i + " - ";
        add = false;
      } else {
        resultat -= i;
        calc += i + " + ";
        add = true;
      }
      i++;
    }
    textRuta.innerHTML = calc.slice(1, -2) + " = " + resultat.toString();
  }
</script>
</body>
</html>