Nan in javascript tabulated algorithm

Asked

Viewed 86 times

-1

function tabuada() {
    let num = document.getElementById('txtn')
    let tab = document.getElementById('seltab')
    if (num.value.length == 0) {
        window.alert('Por favor,digite um número!')
    } else {
        let n = Number(num.value)
        let c = 1;
        tab.innerHTML = ''
        while (c <= 10) {
            let item = document.createElement('option')
            item.text =`${n} x ${c} = ${num*c}`
            tab.appendChild(item)
            c++
        }
    }
}
body{
    background: rgb(151, 158, 46);
    font: normal 15pt Arial;
}
header{
    color:white;
    text-align: center;
}
section{
    background: white;
    border-radius:10px;
    padding: 15px;
    width: 500px;
    margin: auto;
    box-shadow: 5px 5px 10px rgb(233, 230, 72);

}
footer{
    color:white;
    text-align: center;
    font-style: italic;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Tabuada online em js</title>
    <link rel="stylesheet" href="estiloTabuada.css"><!--presionar ctrl e clicar para gerar a sugestão de criar um arquivo css-->
    
</head>
<body>
    <header>
        <h1>Tabuada!</h1>
    </header>
    <section>
        <div>
            <p>Número: <input type="number" name="num" id="txtn"> 
            <input type ="button" value="Gerar Tabuada" onclick="tabuada()">
            </p>
        </div>
        <div>
            <select name="Tabuada" id="seltab"></select>
            <option>Digite um número acima</option>
        </div>
        	
    </section>
    <footer>
        <p>&copy;Yvan.</p>
    </footer>
    <script src="scriptTabuada.js"></script>
    
</body>
</html>

  • And what would be your doubt?

2 answers

1


This line is incorrect brother:

item.text =`${n} x ${c} = ${num*c}`

Change to:

item.text =`${n} x ${c} = ${n*c}`
  • 1

    thanks buddy. tmj

0

Algorithm below:

function multiplicar(multiplicando,multiplicador){
  const resultado = []
  return function(multiplicador){
    while(multiplicador >= 0){
      resultado.push(multiplicando * multiplicador)
      multiplicador = multiplicador-1
    }
    return resultado.reverse()
  }

}
console.log( multiplicar(3)(10) )

Browser other questions tagged

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