Typeerror: Cannot set Property '5' of Undefined

Asked

Viewed 19 times

0

I am now starting to study Javascript, and am facing the following problem, when I run the code below I get the following error:

TypeError: Cannot set property '5' of undefined

lines = ["5 2", "6 2", "5 0"]
let [m, n] = lines.shift().split(' ').map(i => Number(i))

while (m > 0 && n > 0) {
    let soma = 0
    if (n < m)
        [m, n] = [n, m]
    for (let i = m; i <= n; i++)
        console.log(i), soma += i
    console.log(soma)
    [m, n] = lines.shift().split(' ').map(i => Number(i))
}

The strange thing is, if I move the last line of the scope of while for the first, the code runs normally, but the logic to solve my problem is wrong if I do this, because this line has to be executed last in while.

lines = ["5 2", "6 2", "5 0"]
let [m, n] = lines.shift().split(' ').map(i => Number(i))

while (m > 0 && n > 0) {
    [m, n] = lines.shift().split(' ').map(i => Number(i))
    let soma = 0
    if (n < m)
        [m, n] = [n, m]
    for (let i = m; i <= n; i++)
        console.log(i), soma += i
    console.log(soma)
}

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.