Doubts with conversion of variables

1

I'm doing a child's game that tests your math skills. Basically the game is as follows:

The entrance page has a series of linked 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 house number and the value of the house. This same function generates an alert inviting the child to press the play data 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 and enter them into a form. At the same time that the child makes the sum, a function takes the same values and e adds the sum, counts a time and compares the results, if it is equal an alert congratulates and guides you to move on, otherwise it will be informed that the account is wrong and must do it again. Being right she is congratulated and invited to divide the result of the sum effected by the value of the house where it is (in this case here 1st house). The value of the rest of this division (modulo) will be the number of houses that it should advance. Assuming that there are 5 left, the child will be invited to advance the number corresponding to the remainder of the division made by her. The program will count the houses and compare them with the chosen house, if they are the same the child will be congratulated and invited to hit the play button again and all the process will be repeated until the house finishes game. If the chosen house does not match the expected result, the account made by the child will be wrong and it will be informed and directed to do it again.

I have two problems. I can not get the randomly generated values, put them into a variable, to compare later. Because of the code I've generated it is generating NaN, I've tried everything to do the conversion and nothing. The code is below.

The other problem is how to count the houses by the value generated by the rest of the division, so that if any chosen house 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 value. Type table 1 value 30, table 2 value 15, table 3 value 17. With one click the student starts from 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 means that the user should only scroll 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 the count he made, ie if he chooses the house that does not match the rest of the division, an alert should come warning that he must redo the bill. I'm posting here the HTML script with the tables that were created, this script is only part, because they are more than thirty tables, but already gives to have an idea. Post also function. I need to develop a function that recognizes and takes the value of the table that the user is, and divides by the value of the numeric variable resulting from the draw, and when the user that made that account also, counts the tables according to the rest of his account , and clicking on the table, if it is wrong, the program should acknowledge the error and issue an alert saying make the account again and if it is right congratulates, and starts the process again. Can you help me? I include here the html with the tables already made to be easier.

Here is the code for the data I made separate to merge from the larger script after:

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;
}
    
asked by anonymous 07.08.2015 / 01:36

1 answer

1

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

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

So, if you want nome in base 10, you 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 work and it's 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 do not know which browser you are testing your code on, but in Chrome, you can press Ctrl + Shift + J , and in the window that pops up, check the value of the variables to see if they are null if it contains a String , a number, etc.

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

Checkthatthevalueofnomewas"4", a String - and the value of nomeRes , already converted, is an integer,     

07.08.2015 / 02:43