Answer the question
In order to leave comments, you need to log in
How is the condition incorrectly fulfilled?
go()
function go(){
if (i < body["car"].length){
if(body["car"][i].hasOwnProperty('class') == false){
i++
go()
}
console.log('ok')
i++
go()
} else if (i>= body["car"].length){
console.log('exit')
}
}
Answer the question
In order to leave comments, you need to log in
Checking a slightly supplemented
body = {"car": [
'asd',
'ddddd',
{
class: 'asd'
},
'dddd'
]}
i = 0
function go(level = 1) {
console.log(i, level)
if (i < body["car"].length) {
if (body["car"][i].hasOwnProperty('class') == false) {
i++
go(level + 1)
}
console.log('ok')
i++
go(level + 1)
}
else if (i >= body["car"].length) {
console.log('exit')
}
}
0 1
1 2
2 3
ok
3 4
4 5
exit
ok
5 5
exit
ok
6 3
exit
ok
7 2
exit
function go(level = 1) {
console.log(i, level)
if (i < body["car"].length) {
if (body["car"][i].hasOwnProperty('class') == false) {
i++
return go(level + 1)
}
console.log('ok')
i++
return go(level + 1)
}
else if (i >= body["car"].length) {
console.log('exit')
}
}
0 1
1 2
2 3
ok
3 4
4 5
exit
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question