admin管理员组文章数量:1026989
I want to be able to search an array with the input a user has entered. So for example if the user enters "18" into the input field, either "value not found" would appear or "value found" would appear depending on whether the number 18 was in the array.
This is what I have so far.
var search = document.getElementById("search");
var arr = [18,23,20,17,21,18,22,19,18,20];
function beginhere() {
var input = document.getElementById("Input").value;
for (i=0; i<arr.length; i++){
if (arr[i] == Input) {
alert(arr[i]);
} else {
alert("Value not found");
}
}
};
I want to be able to search an array with the input a user has entered. So for example if the user enters "18" into the input field, either "value not found" would appear or "value found" would appear depending on whether the number 18 was in the array.
This is what I have so far.
var search = document.getElementById("search");
var arr = [18,23,20,17,21,18,22,19,18,20];
function beginhere() {
var input = document.getElementById("Input").value;
for (i=0; i<arr.length; i++){
if (arr[i] == Input) {
alert(arr[i]);
} else {
alert("Value not found");
}
}
};
Share
Improve this question
asked Apr 8, 2017 at 11:45
KarlKarl
831 gold badge4 silver badges9 bronze badges
1
- javascript is case sensitive – charlietfl Commented Apr 8, 2017 at 11:47
3 Answers
Reset to default 1Your mistake is the case sensitive i
in Input
if (arr[i] == Input) {
should be
if (arr[i] == input) {
And then you really don't need the else
part. Just write the not found alert after the loop and write a return statement inside the if.
function beginhere() {
var input = document.getElementById("Input").value;
for (i=0; i<arr.length; i++){
if (arr[i] == input) {
alert(arr[i]);
return;
}
}
alert("Value not found");
};
And without a loop you can try
function beginhere() {
var input = document.getElementById("Input").value;
if(arr.indexOf(parseInt(input)) != -1) {
alert(input);
return;
}
alert("Value not found");
};
I've refactored your if
conditions, because as for now it alerts result with every cycle of the for
loop. If the loop has found the given number inside the array, log the number and return the function (there's no need to keep the loop alive anymore). If not - log that value wasn't found to the console.
var arr = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function beginhere() {
var input = document.getElementById("Input").value;
for (i = 0; i < arr.length; i++) {
if (arr[i] == input) {
console.log(arr[i]);
return;
}
}
console.log('value not found');
};
<button onclick="beginhere()">click</button>
<input id='Input'>
Another possible solution, using Array#find
.
var arr = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function beginhere() {
var input = document.getElementById("Input").value,
res = arr.find(v => v == input);
console.log(res ? res : "wasn't found");
};
<button onclick="beginhere()">click</button>
<input id='Input'>
You have some issues:
Move
search
declaration and initialization inside of the function, because you need the actual value and not the value at start.Convert the search value to a number by adding a
plus
infront of the value. This uses an unary plus+
for converting a value (string or number) to a number.Test with
search
.Exit function if
search
is found.Display only one message, if not found after the loop.
var arr = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function beginhere() {
var search = +document.getElementById("search").value, // need to covert to integer
i; // declaration missing
for (i = 0; i < arr.length; i++) {
if (arr[i] == search) { // use search variable
alert(arr[i]);
return;
}
}
alert("Value not found");
}
<input id="search" type="text" onchange="beginhere()">
I want to be able to search an array with the input a user has entered. So for example if the user enters "18" into the input field, either "value not found" would appear or "value found" would appear depending on whether the number 18 was in the array.
This is what I have so far.
var search = document.getElementById("search");
var arr = [18,23,20,17,21,18,22,19,18,20];
function beginhere() {
var input = document.getElementById("Input").value;
for (i=0; i<arr.length; i++){
if (arr[i] == Input) {
alert(arr[i]);
} else {
alert("Value not found");
}
}
};
I want to be able to search an array with the input a user has entered. So for example if the user enters "18" into the input field, either "value not found" would appear or "value found" would appear depending on whether the number 18 was in the array.
This is what I have so far.
var search = document.getElementById("search");
var arr = [18,23,20,17,21,18,22,19,18,20];
function beginhere() {
var input = document.getElementById("Input").value;
for (i=0; i<arr.length; i++){
if (arr[i] == Input) {
alert(arr[i]);
} else {
alert("Value not found");
}
}
};
Share
Improve this question
asked Apr 8, 2017 at 11:45
KarlKarl
831 gold badge4 silver badges9 bronze badges
1
- javascript is case sensitive – charlietfl Commented Apr 8, 2017 at 11:47
3 Answers
Reset to default 1Your mistake is the case sensitive i
in Input
if (arr[i] == Input) {
should be
if (arr[i] == input) {
And then you really don't need the else
part. Just write the not found alert after the loop and write a return statement inside the if.
function beginhere() {
var input = document.getElementById("Input").value;
for (i=0; i<arr.length; i++){
if (arr[i] == input) {
alert(arr[i]);
return;
}
}
alert("Value not found");
};
And without a loop you can try
function beginhere() {
var input = document.getElementById("Input").value;
if(arr.indexOf(parseInt(input)) != -1) {
alert(input);
return;
}
alert("Value not found");
};
I've refactored your if
conditions, because as for now it alerts result with every cycle of the for
loop. If the loop has found the given number inside the array, log the number and return the function (there's no need to keep the loop alive anymore). If not - log that value wasn't found to the console.
var arr = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function beginhere() {
var input = document.getElementById("Input").value;
for (i = 0; i < arr.length; i++) {
if (arr[i] == input) {
console.log(arr[i]);
return;
}
}
console.log('value not found');
};
<button onclick="beginhere()">click</button>
<input id='Input'>
Another possible solution, using Array#find
.
var arr = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function beginhere() {
var input = document.getElementById("Input").value,
res = arr.find(v => v == input);
console.log(res ? res : "wasn't found");
};
<button onclick="beginhere()">click</button>
<input id='Input'>
You have some issues:
Move
search
declaration and initialization inside of the function, because you need the actual value and not the value at start.Convert the search value to a number by adding a
plus
infront of the value. This uses an unary plus+
for converting a value (string or number) to a number.Test with
search
.Exit function if
search
is found.Display only one message, if not found after the loop.
var arr = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20];
function beginhere() {
var search = +document.getElementById("search").value, // need to covert to integer
i; // declaration missing
for (i = 0; i < arr.length; i++) {
if (arr[i] == search) { // use search variable
alert(arr[i]);
return;
}
}
alert("Value not found");
}
<input id="search" type="text" onchange="beginhere()">
本文标签: javascriptSearch array using user inputStack Overflow
版权声明:本文标题:javascript - Search array using user input - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745596712a2158216.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论