Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 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


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

1 Answer

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'/>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...