Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

How to turn a list of strings to an array in javascript

I am trying to code a program which when a user inputs some numbers it will reverse it it and display it on the page. I have to tried to use 'Object.assign', but it doesn't work for me. I have also tried '.toArray', but it still doesn't work(maybe my formatting is wrong?). I am just a beginner so go easy on me. Thanks for t. This is what i have so far:

not working code but what i wanted

This works though: Working code but not what i wanted

question from:https://stackoverflow.com/questions/65601002/how-to-turn-a-list-of-strings-to-an-array-in-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

edit: when I reread the question, I realized you wanted the array in reverse. Updated answer at the bottom.

press enter to add the input. If you don't want only numbers delete type='number'.

var arr = [];
const input = document.getElementById('inpt');

input.addEventListener("keydown", event => {
  if (event.keyCode === 13) {
    arr[arr.length]=input.value;
    input.value="";
    console.log(arr);
  }
});
<input placeholder="Enter a number" type='number' id ='inpt'/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...