Does the prompt command change the value?

Asked

Viewed 89 times

0

Making a simple code to calculate how much I need to reach my college average.

The problem that I cannot receive the values on the keyboard, I use the command prompt but the result is not the same.

I put this code and calculate normally.

<script>
    var ap1 = 4.2;
    var ap2 = 9.4;
    var ap3nota = 5 - ((ap1 + ap2) * 0.3);
    var ap3questoes = (ap3nota / 0.16);
    var nota = ap3questoes * 0.4;

    document.write(nota);
    document.write(Math.round(ap3questoes));
</script>

But with the prompt the result gets wrong.

<script>
    var ap1 = prompt("Nota da ap1");
    var ap2 = prompt("Nota da ap2");
    var ap3nota = 5 - ((ap1 + ap2) * 0.3);
    var ap3questoes = (ap3nota / 0.16);
    var nota = ap3questoes * 0.4;

    document.write(nota);
    document.write(Math.round(ap3questoes));
</script>
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

2

No, it does not change, there is no way it can change. In fact the silver question of a wrong premise.

Whatever you’re going to do in a code, you need to understand everything that’s going on. In particular when using functions you need to read the documentation in full before using it. In the case of function prompt() )is not a command) the documentation says that its return is a data of the type string. This makes all the difference. If the return is a text and you try to make an arithmetic account with it it will not happen what you expect. So your first code is completely different from the second, one calculates numbers and the other calculates texts.

As you still want to operate in numbers need to convert the result of prompt() in numbers. Assuming that you use decimal point values and that no accuracy is required in the calculation, you can use the function parseFloat(). What will you do before you use it?

The code could go like this:

var ap3questoes = (5 - (parseFloat(prompt("Nota da ap1")) + parseFloat(prompt("Nota da ap2"))) * 0.3) / 0.16;
document.write(ap3questoes * 0.4);
document.write(Math.round(ap3questoes));

So it’s solved? No, there are still problems. As I said it only works if you don’t want accuracy, test different numbers and you will see that there are cases that do not give the number you want. And it has nothing to do with prompt(), rather with the guy float which is not suitable for accuracy, then gives error even with the direct number. Most programmers ignore this and have their codes wrong without noticing for years.

There is an error of encapsulation of the information presented on screen in the two codes, it is secondary, but it is good to make clear that there is.

Finally, what if the person types an invalid format? A correct code cannot require the right person to type in to work, unless they report it after validating. Of course, it does not miracle, it will not fix the problem, but your code needs to identify that there was a typo, as for example a letter have been typed, or a wrong point. It would be something like that:

function teste() {
    var t1 = prompt("Nota da ap1");
    if (isNaN(t1)) return;
    var t2 = prompt("Nota da ap2");
    if (isNaN(t2)) return;
    var ap3questoes = (5 - (parseFloat(t1) + parseFloat(t2)) * 0.3) / 0.16;
    document.write(ap3questoes * 0.4);
    document.write(" - ");
    document.write(Math.round(ap3questoes));
}
teste();

I put in the Github for future reference.

0

Must convert values passed via prompt for float:

    var ap1 = parseFloat(prompt("Nota da ap1"));
    var ap2 = parseFloat(prompt("Nota da ap2"));
    var ap3nota = 5 - ((ap1 + ap2) * 0.3);
    var ap3questoes = (ap3nota / 0.16);
    var nota = ap3questoes * 0.4;

    document.write(nota);
    document.write(Math.round(ap3questoes));

PS: You must enter the values with . (point) as decimal separator. Ex.: 1.2 instead of 1,2.

Browser other questions tagged

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