简体   繁体   English

我正在运行这个 javascript 代码,但它给了我一个..错误请告诉我出了什么问题,因为我认为代码是正确的?

[英]I am running this javascript code but it is giving me an.. error please tell me what is wrong because I think the code is correct?

 let marks_of_students = [100,100,40] function FindGrade(marks) { let sum = 0; for (let i = 0;i <= marks.length; i++) { sum += marks[i]; console.log(sum); } return sum; } console.log(FindGrade(marks_of_students));

I don't know why I'm seeing this NaN printing along side the sum.我不知道为什么我看到这个 NaN 在总和旁边打印。 Someone please help what did I do wrong?有人请帮助我做错了什么?

You are trying to loop over the mark_of_students array with a condition i <= marks.length which means the loop will try to find marks[3] in the last iteration which doesn't exist.您正在尝试使用条件i <= marks.length循环遍历 mark_of_students 数组,这意味着循环将尝试在最后一次迭代中找到不存在的marks[3] You need to change the condition to i < marks.length to get the desired result.您需要将条件更改为i < marks.length以获得所需的结果。

 let marks_of_students = [100, 100, 40] function FindGrade(marks) { let sum = 0; for (let i = 0; i < marks.length; i++) { sum += marks[i] } return sum } console.log(FindGrade(marks_of_students))

try to convert a object into a integer by command parseInt(object) to sum.尝试通过命令 parseInt(object) 将 object 转换为 integer 以求和。

Im javascript concatenates, NaN is not a number; Im javascript 连接,NaN 不是数字;

Try to do this:尝试这样做:

parseInt("1") + parseInt("1") parseInt("1") + parseInt("1")

instead of 1 + 1而不是 1 + 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 请告诉我此JavaScript代码有什么错误 - Please tell me what is error in this javascript code 我正在学习编码,有人可以告诉我哪里错了吗? - I am learning to code, can someone please tell me what is wrong? 请告诉我我在下面的转换为float的javascript代码中做错了什么 - please tell me where i am doing mistake in following javascript code of conversion to float 我尝试了 crypto-js 但 output 不正确,请查看我的代码并纠正我的错误 - I tried crypto-js but the output is not correct, please see my code and correct me where I am wrong 有人可以告诉我如何简化这段代码吗? - Could someone please tell me how I could simplify this code? 您能告诉我这段JavaScript代码有什么问题吗? - Can you tell me what is wrong with this javascript code? 我正在尝试使用传单javascript库可视化地图上的某些数据点。您能告诉我我的代码有什么问题吗? - I am trying to visualise some datapoint on a map using leaflet javascript library.Can you tell me whats wrong with my code? 有人能告诉我我做错了什么 - Can someone tell me what am i doing wrong Codecademy让我很难过,我做错了什么? - Codecademy is giving me a hard time, what am I doing wrong? 我正在为一堂课制作井字游戏。 有人可以看一下我的代码并告诉我我做错了什么吗? - i am building a tic tac toe game for a class. can someone look at my code and tell me what i am doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM