I am currently learning JavaScript Arrays and am trying to do a small program that allows a user to enter a value input. From there, once they click on the "Check" button, the JavaScript is meant to loop through the array and check if the value is found within.
If so, a success message is printed, if the value is not found the program prompts the user to try again.
My issue is I am having trouble with looping through the array to find the value. Below is all my code, and I would appreciate the help!
const numbers = [58, 4, 17, 32, 24, 53, 44, 99, 70, 56, 72,
43, 36, 51, 37, 46, 35, 25, 29, 64, 76, 21,
82, 94, 47, 12, 19, 31, 69, 81, 20, 91, 50,
3, 34, 79, 2, 27, 68, 52, 23, 22, 84, 18, 16,
33, 13, 39, 77, 8
];
let value = document.getElementById("num");
let output = document.getElementById("display");
function printValue() {
for (let i = 0; i < numbers.length; i++) {
if (value == i) {
output.innerHTML = value.value;
output.innerHTML = "Value was found in the array!"
}
}
output.innerHTML = "Value is not found in array, please try again";
}
value.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
event.preventDefault();
document.getElementById("sub").click();
}
})
<h1>Check to see if your value is found in an array</h1>
<label for="num">Enter value:</label>
<input type="text" id="num" name="num">
<br><br>
<button id="sub" onclick="printValue()">Check</button>
<br><br>
<div id="display"></div>
3
Answers
There are few issues in your code:
You are comparing the element with the index where you should compare the element’s value with array value.
You have to
return
from the function after a match found.I will also suggest you to convert the value to number and use strict equality (
===
) operator for checking. I will also suggest you to usetextContent
instead ofinnerHTML
.Try the following way:
Though I prefer using
Array.prototype.includes()
instead for loop:Code Example:
In your code you compare the input’s value to the
for
loop’s index which doesn’t have much sense. We should rather compare an array element.There’s a nice method to check whether an array has a value:
Array::includes()
.But first we should convert the input’s value to a number and pass it to
numbers.includes()
.The problem here that parsing a string to a number is tricky. For example
Number(' ') === 0
orparseInt('12asdf') === 12
.Here’s some parsing options of strings to numbers:
What is the standard way to check a variable is initialised as a number in javascript?
So for checking if a string contains only digits we could use a regular expression.
That could be considered advanced, so check the further easier solutions.
So our
printValue()
could be compressed to 2 lines:Here we
+
(which is the same asNumber()
) is in thenumbers
withArray::includes()
.+
which will be the index of the corresponding message.But I guess for the OP’s purposes comparing strings is easier to understand.
We could use
Array::some()
to compare strings rather than numbers:Regarding the OP’s code it could be fixed as:
Add
return