I have tried iterating over the array holder2
while copying it's integer elements to another array temp
. But it doesn't seem to work as the content of temp
remained the same.
var holder=getElementById("userinput").value;
var holder2=holder.split(" ");
var temp =[];
for(vari=0;i<holder2.length;i++){
if(isNaN(holder2[i])===false){
temp[i]=holder2[i];
}
}
That's not how you add an element to an array in Javascript. What you're looking for is push()
.
temp.push(holder2[i]);
You mentioned that you want integers only and that temp
is empty. I'd suggest verifying that elements are integers and then using Array.push()
.
var holder=getElementById("userinput").value;
var holder2=holder.split(" ");
var temp =[];
for(var i=0;i<holder2.length;i++){
if(isNaN(holder2[i])===false && holder2[i] % 1 === 0){
temp.push(holder2[i]);
}
}
Try this:
var holder=getElementById("userinput").value;
var holder2=holder.split(" ");
var temp =[];
for(var i=0;i<holder2.length;i++){
var num = parseInt(holder2[i])
if(isNaN(num)===false){
temp.push(num)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.