1

I am trying to make a if statement that check's if a input number is in the existing array.

What I trying to achieve is that I can insert a number in the input field and remove this one from the array. I am wondering what is the best way to do this.

var nummers = [20, 14, 39, 192, 39];
toon();

// button's maken
document.getElementById("addOneBtn").onclick = addNum;
document.getElementById("removeOneBtn").onclick = removeNum;
document.getElementById("sorteerBtn").onclick = sorteer;
document.getElementById("sorteer2Btn").onclick = sorteer2;

function addNum() {
  var num = document.getElementById("addOneInput").value;
  //nummers.push(" " + num);
  nummers.push(parseInt(num));
  toon();
}

function removeNum() {
  if (document.getElementById("removeOneInput").value in nummers) {
    nummers.pop();
    toon();
  } else {
    document.getElementById("removeOneError").innerHTML = "not a number or in the array"
  }
}

function sorteer() {
  nummers.sort(vergelijk);
  toon();
}

function sorteer2() {
  nummers.sort(vergelijk2);
  toon();
}

function vergelijk(a, b) {
  return a - b;
}

function vergelijk2(a, b) {
  return b - a;
}


function toon() {
  document.getElementById("arrayOutput").innerHTML = nummers.toString(); //renault.gegevens()
}
<p id="arrayOutput"></p>
<br>
<input type="input" id="addOneInput">
<button type="button" value="add one" id="addOneBtn">Add one number</button>
<br>
<br>
<input type="input" value="" id="removeOneInput">
<p id="removeOneError"></p>
<button type="button" value="remove one" id="removeOneBtn">Remove a number</button>
<br>
<br>
<button type="button" value="sorteer" id="sorteerBtn">Sorteer</button>
<button type="button" value="sorteer omgekeerd" id="sorteer2Btn">Sorteer andersom</button>
Rajesh
  • 24,354
  • 5
  • 48
  • 79
mike
  • 45
  • 1
  • 5
  • Though the linked duplicate is nominally about "objects", the examples work for number values too. – Pointy Dec 05 '16 at 14:24
  • Also to remove element, you can look into `array.splice`. `.pop` will remove last element only. – Rajesh Dec 05 '16 at 14:25

0 Answers0