0

I'm having matching a user input with an element in the Array. "Index === lengthOfBoat" doesn't output "Works", even though the user input is matching a number in the Array. If I use a ">" sign, the console outputs "Works".

Thought this should be pretty simple.

What am i doing wrong?

var lenghtOfBoat = [4, 5, 6, 7, 8, 9];
var inputBoatLenght = document.getElementById("boatLenght");

inputBoatLenght.addEventListener("change", function() {
    var index = lenghtOfBoat.indexOf(inputBoatLenght);

    if (index === lenghtOfBoat) {
        console.log("Works");
    }
    else {
        console.log("Nooo");
    }
})

<!DOCTYPE html>
<html>
    <head>
        <title>Båd Dækning</title>
    </head>
    <body>
        <input type="number" name="boatLenght" id="boatLenght" placeholder="Båd Længde">
        <input type="number" name="boatHK" id="boatHK" placeholder="Båd HK"> 

        <script type="text/javascript" src="script.js"></script>
    </body> 
</html>

BR Martin

5 Answers5

0

You can simply use includes in this case which will return you the boolean value

if (lenghtOfBoat.includes(inputBoatLenght)) {
  console.log("Works");
}
else {
   console.log("Nooo");
}
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

You could just check if lenghtOfBoat.indexOf(+this.value) > -1, and act upon that :

var lenghtOfBoat = [4, 5, 6, 7, 8, 9];
var inputBoatLenght = document.getElementById("boatLenght");

inputBoatLenght.addEventListener("change", function() {
  if(lenghtOfBoat.indexOf(+this.value) > -1){
    console.log("Works");
  }else{
    console.log("Nooo");
  }
})
<input type="number" name="boatLenght" id="boatLenght">
Zenoo
  • 12,670
  • 4
  • 45
  • 69
  • Thanks Zenoo.Quick questions, as I'm trying to learn all of this. Why do you -1? –  Mar 06 '18 at 09:07
  • @Zoke `indexOf` returns the index of the element in the array. If it doesn't find anything, it returns `-1`. – Zenoo Mar 06 '18 at 09:08
  • @Rajesh His problem isn't only with `indexOf`. he doesn't get the `value` of his input correctly. – Zenoo Mar 06 '18 at 09:10
0

You can just check if the lengthOfBoat array contains inputBoatLength. You will need to convert the inputBoatLength to number to check if it exists in the array or not.

var lenghtOfBoat = [4, 5, 6, 7, 8, 9];
var inputBoatLenght = document.getElementById("boatLenght");
inputBoatLenght.addEventListener("change", function() {

  var inp = +this.value;
  if (lenghtOfBoat.includes(inp)) {
    console.log("Works");
  } else {
    console.log("Nooo");
  }

})
void
  • 36,090
  • 8
  • 62
  • 107
  • @Rajesh just go through the duplicate once and try and understand from the OPs prespective that how easy it would be for him to solve his problem after reading the answers in that. – void Mar 06 '18 at 09:11
0

The getElementById gives you the HTMLElement but you need to take the value of the selected element. Then the value will be a string. So, use parseInt() or Number() to convert into number to compare it with the index.

var lenghtOfBoat = [4, 5, 6, 7, 8, 9];
var inputBoatLenght = document.getElementById("boatLenght").value;

inputBoatLenght.addEventListener("change", function() {
    var index = lenghtOfBoat.indexOf(parseInt(inputBoatLenght));


    if (index === lenghtOfBoat) {
        console.log("Works");
    }
    else {
        console.log("Nooo");
    }
})
<input type="number" name="boatLenght" id="boatLenght" placeholder="Båd Længde">
        <input type="number" name="boatHK" id="boatHK" placeholder="Båd HK"> 

    
Prashanth KR
  • 294
  • 2
  • 8
0

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. replace:

index === lenghtOfBoat

by:

index >= 0

read more from here

WSMathias9
  • 669
  • 8
  • 15