Warning message in Javascript does not work

Asked

Viewed 263 times

0

I am trying to create a warning message on my site using the window.Alert command, but this warning message does not appear, nor at least in the browser source code appears the command I typed.This is the code I am referring to:

<!DOCTYPE html>
<html lang="pt-br">
<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>meu primeiro programa...</title><!--ISSO É UM COMENTARIO em html..-->
    <style>
        body {
            background-color: rgb(95, 95, 180);
            color: white; /* Comentario em CSS*/
            font: normal 25pt arial;
        }
        h1 {
            color: rgba(255, 255, 0, 0.589) ; 
        }
    </style>
</head>
<body>
    <h1>revendedora autorizada ferreira</h1>
    <p> tudo para seu automóvel novo e semi-novo.</p>
    <script>
        var n1 = Number.parseInt(window.prompt(`digite um número: `))
        var n2 = Number.parseInt(window.prompt(`digite outro número:`))
        var s = n1 + n2
        window.alert(`a soma dos valores é :` + s)
        //(number + number) + para adição
        // (string + string)+ para concatenação
    </script>
</body>
</html>

As I said, the window.Alert line doesn’t appear, I don’t know why. thanks in advance.

  • You have to put the Alert text in quotes. And the tb prompt.

  • I put it in quotes, so I knew it could be a mistake for not having put among crases, but even so of the same problem, does not run in my browser.

  • the quotation marks have already been placed on all, even so, without effect.

  • 2

    is using wrong quotes for this context. ' is not the same as ```

1 answer

3

You were using the String template of wrong way the correct to concatenate is within the string put the dollar between brackets and the variable, example: a soma dos valores é : ${s}

var n1 = Number.parseInt(window.prompt(`digite um número: `))
var n2 = Number.parseInt(window.prompt(`digite outro número:`))
var s = n1 + n2
window.alert(`a soma dos valores é : ${s}`);

that String template is part of the ES6/ES2015 thus not compatible with all browsers (the old ones).

To do it in an ideal way follow the example below:

var n1 = Number.parseInt(window.prompt('digite um número: '))
var n2 = Number.parseInt(window.prompt('digite outro número: '))
var s = n1 + n2
window.alert('a soma dos valores é : ' + s);

thus compatible with browsers.

Browser other questions tagged

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