Doubts with conversion of variables

Asked

Viewed 114 times

1

I’m making a child game that tests your mathematical knowledge. Basically the game is as follows:

The entry page has a series of interconnected houses, where each house has its own numerical value.

At first the child will click on the first house that generates an event that calls a function that records the number of the house and the value of the house. This same function generates an Alert inviting the child to press the dice throw button. This button calls a function that generates two random numbers and displays them on the screen and displays a message directing the child to add the two values together and type them into a form. At the same time that the child makes the sum, a function takes the same values and and proceeds the sum, counts a time and compares the results, if it is equal to an Alert congratulates and guides to move forward, otherwise it will be informed that the account is wrong and that it shouldthere again. Being certain she is congratulated and invited to divide the result of the sum made by the value of the house where she is ( in the case here 1st house). The value of the rest of this division (module) shall be the number of houses which it shall advance. Assuming there are 5 left, the child will be invited to advance the number corresponding to the leftover division made by it. The program will count the houses and compare with the chosen house, if they are equal or the child will be congratulated and invited to press the button throw the dice again and every process will be repeated until the end of the game home. If the chosen house does not match the expected result, the account held by the child will be wrong and she will be informed and directed to perform it again.

I have two problems. I cannot take the randomly generated values, put them in a variable, to compare later. By the code I made it generating Nan, I’ve tried everything to make the conversion and nothing. The code follows below.

The other problem is how to count the boxes by the value generated by the rest of the division, so that if any chosen box does not match the correct count of an error. Sorry the text is long but I tried to express what I am trying to do. But let me be more explicit in this second problem reported in the question. In this game I have to go through a series of tables and each one has its number and a value. Type table 1 value 30, table 2 value 15, table 3 value 17. With one click, the student starts with table 1. The value of this table will be divided by the sum of the draw. The rest of this division (module) will be the number of tables that the user will go through, that is, if the rest 5, it will mean that the user should only go through five tables and click on the fifth. So far so good, but the program must recognize that the user has made the right account, by counting what he did, that is, if he chooses the house that does not correspond to the rest of the division, an alert should appear warning that he should redo the account. I’m posting here the HTML script with the tables that were created, this script is only part, because there are more than thirty tables, but you can already get an idea. Post also function. I need to develop a function that recognizes and takes the value of the table that the user is in, and divides it by the value of the numeric variable resulting from the draw, and when the user who made this account also counts the tables according to the rest of his account, and click on the table, if it is wrong, the program should recognize the error and issue an alert saying do the account again and if it is right congratulations, and starts the whole process again. Can you help me? I include html with the tables already linked to make it easier.

Follow the data code I did separately to join from the larger script later:

function executar() {
    document.getElementById(1).innerHTML = rnd(0, 6);
    document.getElementById(2).innerHTML = rnd1(0, 6);
    var res1 = parseInt(document.getElementById('1') value);
    var res2 = parseInt(document.getElementById('2') value);
    /*pelo teste ele não reconhece os valores como numero*/
    alert(eval(res1.value) + eval(res2.value));
    do {

        nome = prompt("Some os valores sorteados Digite o resultado aqui.");

    } while (nome == null || nome == "");

    ok = confirm("Tem certeza que o resultado é " + nome);
    if (ok == 1) {
        setTimeOut("SomarValores();", 3000);

    } else {
        alert("Jogue os dados novamente");
    }

}

function rnd(min, max) {
    var valor = Math.floor(Math.random(parseInt) * max) + 1;
    dadoValor = valor;
    return dadoValor;
    dadoValor = valor;

}

function rnd1(min, max) {
    var valor1 = Math.floor(Math.random(parseInt) * max) + 1;
    dadoValor1 = valor1;
    return dadoValor1;
}

1 answer

1


According to the website of Mozilla Foundation, the function parseInt(); should be written like this:

parseInt(String, radix); // em português, base.

So if you want nome in base 10, would have to write like this:

parseInt(nome, 10); // nome na base 10.

I found some other syntax errors, so I modified the code to make it work and be very concise:

var numero;
var nomeRes;
var res1;
var res2;

function executar() {

    document.getElementById('1').innerHTML = rnd(0, 6);
    document.getElementById('2').innerHTML = rnd(0, 6);
    res1 = document.getElementById('1').innerHTML;
    res2 = document.getElementById('2').innerHTML;

    nome = prompt("Some os valores sorteados Digite o resultado aqui.");

    numero = parseInt(res1, 10) + parseInt(res2, 10);
    nomeRes = parseInt(nome, 10);

    if (nomeRes == numero) {
        alert("Esta certo!");
    } else {
        alert("Jogue os dados novamente");
    }

}

function rnd(min, max) {
    return Math.floor(Math.random() * (max - min) + min);
}

I don’t know which browser you’re testing your code on, but in Chrome, you can tap Ctrl + Shift + J and, in the window that appears, check the value of the variables, to know if they are null, if it contains a String, a number, etc..

I moved the variables out of the function precisely to be able to access them at any time during the execution of the program.

Console Chrome

Check that the value of nome was "4", a String - and the value of nomeRes, already converted, is an integer, 4.

  • Thanks bro that’s what I needed. You’d have to help me in the second problem?

  • @Isaelt.Costa Maybe! You’ve already posted it here?

  • I already posted yes, but I had not posted the code, but now I posted. if you can help me I will be very grateful

  • @Isaelt.Costa Oh, I had not seen that you had updated the question. I ask one thing: Create a second question, with the text you wrote and if possible, edit this and revert to the original. This is important as your questions can help future visitors (and, by breaking down, you can get a faster response!)

  • Thanks, thanks for the guidance

Browser other questions tagged

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