Create an Array with the numbers typed by the user

Asked

Viewed 100 times

0

Hello, everyone. I’m a beginner in the study of Javascript and came across a problem that may be relatively simple. The question is this: I cannot get the numbers sent from the variable "num" to be placed inside the vector "numbers". Always when I type console.log(numbers) the typed numbers appear on the console as "Undefined". My code is incomplete and I can’t move forward without first solving this problem. Note: I left out the code in HTML and CSS, but if you want me to increase it for better view I’ll be posting later.

let numeros = []
function adicionar () {
            let num = Number(document.getElementById('cNum').value)
            let respF = document.getElementById('respFinal')
            
            if (Number(num) < 1 || Number(num) > 100) {
                window.alert('Número Inválido')
            } else {
                let respNum = document.getElementById('cText')
                respNum.innerHTML += `${num}\n`
                numeros.push(Number(num).value)

            }

1 answer

2


It seems to me that your only problem is this line: numeros.push(Number(num).value)

You don’t have to Number(X).value, that .value is not a valid attribute, so returns undefined.

But beyond that, num is already of the type number, then you don’t need to reconvert it.

That would be enough: numeros.push(num)

I think that solves.

  • It worked. Thank you very much.

Browser other questions tagged

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