Just try to ask them to input their numbers or grade, separated by a comma and then you can split on it.
var arr = prompt("Enter your numbers").split(",")
Or, ask prompt ten times
var arr = [];
for(var i = 0; i < 10; i++)
arr.push(prompt("Enter a number");
If you want them to be numbers, just prefix prompt with +, so it becomes a number(provided they're actual numbers) or just do
arr = arr.map(Number);
Answer from Amit Joki on Stack OverflowJust try to ask them to input their numbers or grade, separated by a comma and then you can split on it.
var arr = prompt("Enter your numbers").split(",")
Or, ask prompt ten times
var arr = [];
for(var i = 0; i < 10; i++)
arr.push(prompt("Enter a number");
If you want them to be numbers, just prefix prompt with +, so it becomes a number(provided they're actual numbers) or just do
arr = arr.map(Number);
See the explanations in comments:
var arr = []; // define our array
for (var i = 0; i < 10; i++) { // loop 10 times
arr.push(prompt('Enter grade ' + (i+1))); // push the value into the array
}
alert('Full array: ' + arr.join(', ')); // alert the results
Adding User Input to the Array in JavaScript
javascript - Using an array function inside a prompt - Stack Overflow
How to create an array from names input through prompt?
how do i fix this javascript prompt=userInput from array to alert problem - Stack Overflow
The way you are doing it is close. You just need to put the prompt() outside the loop. Once you have the input to search, you then loop through your whole array of objects.
For example:
var nameInput = prompt();
for (i =0; i < array.length; i++){
if (nameInput == array[i].name) {
console.log(array[i].id)
}
}
Small explanation
Since your prompt is in a loop that also loops your array of objects, there can only be 1 "correct" answer at that given prompt point - whatever index (array[i].name) the loop is currently on.
To see more of what I mean run your current code and type jef the first time the prompt comes up, Steve the second time, and Ryan the third time- this way gets your IDs, but is probably not your intended outcome.
Try this. You need to sort through your array and perform a strong match, then return the ID.
function NameExists(strName,objectArray)
{
//convert to lower for type sensitive
strName = strName.toLowerCase();
for(var i = 0; i <objectArray.length; i++)
{
//convert to lower for type sensitive
var comparison = objectArray[i].Name.toLowerCase();
if(comparison.match(strName)==comparison)
return objectArray[i].id;
}
}