我需要创建一个程序,要求用户输入一个值来存储数组,并继续不断询问要添加的值,直到他们没有更多的数字要输入到数组中。 之后,我必须输出不包含零的数组(它们被允许输入零,所以我必须从输出中过滤它们)并返回数组的总和。 我目前与我的程序有关的问题是, arr.push(x)当前正试图在arrin函数中推送未定义的值。 我觉得好像还有更好的方法比我现在正在尝试,所以我都乐意改进。
var x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); var y = arrin(x); var arr = []; var s; function arrin(x) { if(x != NaN){ arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); }else{ document.write("<p>"+arr.toString()+"</p>"); s = sum(arr); doucment.write("<p> The sum of all elements in the array is "+s+"</p>"); }}
I need to create a program that asks the user for a value to store in an array and continues to keep asking for values to add till they have no more numbers they want to enter into the array. Afterwards I must output the array containing no zeros (Which they're allowed to enter zeros so I have to filter them out from the output) and return the sum of the array. My current issue with my program is that arr.push(x) is currently trying to push an undefined value in the function arrin. I feel as if there's a much better way of going about this than I'm currently trying so I'm all ears for improvement.
var x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); var y = arrin(x); var arr = []; var s; function arrin(x) { if(x != NaN){ arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); }else{ document.write("<p>"+arr.toString()+"</p>"); s = sum(arr); doucment.write("<p> The sum of all elements in the array is "+s+"</p>"); }}
最满意答案
x != NaN将不起作用。
!isNaN(x)将检查var是否是一个数字。
x != "NaN"将检查文字是否字面意思是“NaN”
最后两个中的任何一个都可以工作。
var arr = []; var y = arrin(0); var s; function arrin(x) { if (!isNaN(x)) { if (x != 0) arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); } else { document.write("<p>" + arr.toString() + "</p>"); s = sum(); document.write("<p> The sum of all elements in the array is " + s + "</p>"); } } function sum() { var t = 0; for (var i = arr.length; i--;) t += parseInt(arr[i]); return t; }x != NaN will not work.
!isNaN(x) will check if the var is a number.
x != "NaN" will check if the text is literally "NaN"
either of the last two will work.
var arr = []; var y = arrin(0); var s; function arrin(x) { if (!isNaN(x)) { if (x != 0) arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); } else { document.write("<p>" + arr.toString() + "</p>"); s = sum(); document.write("<p> The sum of all elements in the array is " + s + "</p>"); } } function sum() { var t = 0; for (var i = arr.length; i--;) t += parseInt(arr[i]); return t; } 推送未定义的语法错误JS(Push Undefined syntax error JS)我需要创建一个程序,要求用户输入一个值来存储数组,并继续不断询问要添加的值,直到他们没有更多的数字要输入到数组中。 之后,我必须输出不包含零的数组(它们被允许输入零,所以我必须从输出中过滤它们)并返回数组的总和。 我目前与我的程序有关的问题是, arr.push(x)当前正试图在arrin函数中推送未定义的值。 我觉得好像还有更好的方法比我现在正在尝试,所以我都乐意改进。
var x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); var y = arrin(x); var arr = []; var s; function arrin(x) { if(x != NaN){ arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); }else{ document.write("<p>"+arr.toString()+"</p>"); s = sum(arr); doucment.write("<p> The sum of all elements in the array is "+s+"</p>"); }}
I need to create a program that asks the user for a value to store in an array and continues to keep asking for values to add till they have no more numbers they want to enter into the array. Afterwards I must output the array containing no zeros (Which they're allowed to enter zeros so I have to filter them out from the output) and return the sum of the array. My current issue with my program is that arr.push(x) is currently trying to push an undefined value in the function arrin. I feel as if there's a much better way of going about this than I'm currently trying so I'm all ears for improvement.
var x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); var y = arrin(x); var arr = []; var s; function arrin(x) { if(x != NaN){ arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); }else{ document.write("<p>"+arr.toString()+"</p>"); s = sum(arr); doucment.write("<p> The sum of all elements in the array is "+s+"</p>"); }}
最满意答案
x != NaN将不起作用。
!isNaN(x)将检查var是否是一个数字。
x != "NaN"将检查文字是否字面意思是“NaN”
最后两个中的任何一个都可以工作。
var arr = []; var y = arrin(0); var s; function arrin(x) { if (!isNaN(x)) { if (x != 0) arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); } else { document.write("<p>" + arr.toString() + "</p>"); s = sum(); document.write("<p> The sum of all elements in the array is " + s + "</p>"); } } function sum() { var t = 0; for (var i = arr.length; i--;) t += parseInt(arr[i]); return t; }x != NaN will not work.
!isNaN(x) will check if the var is a number.
x != "NaN" will check if the text is literally "NaN"
either of the last two will work.
var arr = []; var y = arrin(0); var s; function arrin(x) { if (!isNaN(x)) { if (x != 0) arr.push(x) x = parseInt(prompt("Enter a number, to exit enter NaN", "0"), 10); y = arrin(x); } else { document.write("<p>" + arr.toString() + "</p>"); s = sum(); document.write("<p> The sum of all elements in the array is " + s + "</p>"); } } function sum() { var t = 0; for (var i = arr.length; i--;) t += parseInt(arr[i]); return t; }
发布评论