我需要对同一对象的2个属性(myTodos)执行算术加法 喜欢 让metleft = myTodos.meetings-myTodos.meetDone。
我希望得到一些数字,但实际结果显示为'Nan'
我首先尝试为metleft声明一个值,然后也使用parseInt进行尝试,但结果仍显示为“ NaN”
让metleft = 0 Meetleft = parseInt(myTodos.meetings-myTodos.meetDone)
但这也不起作用。
let myTodos = {
day:'monday',
meetings: 0,
meetdone: 0,
}
let addMeetings = function(todo,meet=0) {
todo.meetings = todo.meetings+meet
}
let meetDone = function(todo,meet=0) {
todo.meetdone = todo.meetdone-meet
}
let restDay = function(todo) {
todo.meetings=0
todo.meetDone=0
}
let getSummaryOfDay = function(todo) {
let meetleft = todo.meetings + todo.meetDone
console.log(meetleft)
}
addMeetings(myTodos,4)
addMeetings(myTodos,2)
meetDone(myTodos,5)
getSummaryOfDay(myTodos)
上述代码iam预期结果为1,但实际结果为NaN
答案 0 :(得分:0)
请注意meetdone
和meetDone
属性。对象属性区分大小写。
这是工作示例:
let myTodos = {
day:'monday',
meetings: 0,
meetdone: 0,
}
let addMeetings = function(todo,meet=0) {
todo.meetings = todo.meetings+meet
}
let meetDone = function(todo,meet=0) {
todo.meetdone = todo.meetdone-meet
}
let restDay = function(todo) {
todo.meetings=0
todo.meetdone=0
}
let getSummaryOfDay = function(todo) {
let meetleft = todo.meetings + todo.meetdone
console.log(meetleft)
}
addMeetings(myTodos,4)
addMeetings(myTodos,2)
meetDone(myTodos,5)
getSummaryOfDay(myTodos)
答案 1 :(得分:0)
正确的代码如下;您的代码中有错别字 D 一个vs见面 d 一个(小写)
let myTodos = {
day:'monday',
meetings: 0,
meetdone: 0
}
let addMeetings = function(todo,meet=0) {
todo.meetings = todo.meetings+meet;
}
let meetDone = function(todo,meet=0) {
todo.meetdone = todo.meetdone-meet;
}
let restDay = function(todo) {
todo.meetings=0;
todo.meetDone=0;
}
let getSummaryOfDay = function(todo) {
let meetleft = todo.meetings + todo.meetdone;
console.log(meetleft);
}
addMeetings(myTodos,4);
addMeetings(myTodos,2);
meetDone(myTodos,5);
getSummaryOfDay(myTodos);
作为一种习惯,我也尝试以;
结尾的行,这是一种更好的编写代码的方式:)希望对您有所帮助