Error in Javascript. "prompt not set"

Asked

Viewed 55 times

0

I decided to make a project of calculation of average school with conditional, where I can perform the calculation n times, or finish if you wish.

No replit.com is running, but VS Code is giving error

undefined prompt

Where would be the mistake?


console.log('####################################')
console.log('####### Média do Aluno 2021! #######')
console.log('####################################')

let dados = true
while (dados = prompt('\nDeseja calcular a média do aluno? (s) para Sim (n) para Não: ')) {
  if (dados === 's') {
    let nome = prompt(`\nQual o nome do aluno? `)
    let n1 = parseFloat(prompt(`\nQual a primeira nota? `))
    let n2 = parseFloat(prompt(`Qual a segunda nota? `))
    let n3 = parseFloat(prompt(`Qual a terceira nota? `))
    let n4 = parseFloat(prompt(`Qual a quarta nota? `))
    let media = (n1 + n2 + n3 + n4) / 4

    console.log(`\nA média do aluno ${nome} é ${media}`)

    if (media >= 7){
      console.log(`\nAluno ${nome} [aprovado!]`)
    }
    else {
      console.log(`\nAluno ${nome} [reprovado!]`)
    }
  }
    else {
     console.log('\n##############################################\n### Obrigado por utilizar o nosso sistema! ###\n##############################################')
    break
  }
  
}

  • 1

    window.prompt(....) resolves?

  • I could not. Here was error. :/

  • 1

    prompt with while is not a good idea, should read the value for a variable and use the variable in while

  • Without wanting to abuse, could you give an example? it would be of great help to understand where I’m going wrong. I thank you already.

  • yes @Sirmarcius, can for example do so: while (true) { var dados = prompt(....); if (dados == "N") break; .... resto do código ... }

1 answer

0

The prompt in reference is a library, if you already have it installed, just import the library into your script, if not, install via NPM. In case it will install, recommend the prompt-sync for convenience, you will not need to change anything in your code since it is called via prompt(). Install it by running the command npm i prompt-sync and import it into your adding script const prompt = require('prompt-sync')(); at the very beginning of it.

When installing the library, make sure it is in the same directory.

Note that after performing the procedure, each character typed in the terminal a line will be added, this happens because of the escape character n, remove it at all prompt() to correct the behavior.

  • I was able to run via terminal. But in VS Code it seems more like something related to permissions. I’ll see if I can fix it. But it’s a breakthrough. Thank you so much for the strength.

  • 1

    Describe the problem cited in VS Code so we can analyze

  • The error in question is "Uncaught Error: ENXIO: no such device or address, open '/dev/tty'" could be because of the directory?

  • Probably try to make the oldest attempt at universal solution: end the VS Code process and open again, check the directory, if the system terminal managed to run, the expected is that in VS Code is also possible.

Browser other questions tagged

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