Javascript - How to save prompt input into array

后端 未结 4 464
日久生厌
日久生厌 2021-01-23 10:12

I`m having some issue with Javascript. We just started to study it a couple weeks ago and I have to do a work for class:

Need to do a prompt. get 10 numbers input (10 gr

4条回答
  •  无人共我
    2021-01-23 10:48

    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);
    

提交回复
热议问题