0

Hey guys I'm trying to do one project and I got one problem. I generated 2 random numbers and operator - then I got the result for this equation and stored it in an array. I wanted to add other 3 randomly generated numbers to the array (all of the numbers will be displayed on buttons and user will be able to click on one and find out if its the right result). It works only for the first number in the arry, which is the right result. I didnt mange to push the other 3 numbers. Do you know how to do it? Thank you in advance!

window.addEventListener('load', function () {

    var rnum1 = generateRandomNumber1();
    var rnum2 = generateRandomNumber2();
    //pass the random numbers to the function   
    var data = generateRandomOperatorAndCorrectResult(rnum1, rnum2);
    //data=["+", [5]]
    var operator = data[0];
    var allResults = data[results];
    var mes = alert(allResults);


    document.querySelector("#text").textContent = "Kolik je " + rnum1 + operator + rnum2 + "?";
});

function generateRandomOperatorAndCorrectResult(num1, num2) {
    var operators = [{
        sign: "+",
        method: function (rnum1, rnum2) { return rnum1 + rnum2; }
    },
    {
        sign: "*",
        method: function (rnum1, rnum2) { return rnum1 * rnum2; }
    },
    {
        sign: "-",
        method: function (rnum1, rnum2) { return rnum1 - rnum2; }
    }];
    var results = [];

    var selectedOperator = Math.floor(Math.random() * operators.length);
    var randomOperator = operators[selectedOperator].sign;
    var correctResult = (operators[selectedOperator].method(num1, num2)); //pass the numbers to the methods
    results.push(correctResult);
    var randomResult = generateRandomResults(3);
    results.push(randomResult);
    //return multiple values
    return [randomOperator, [results]];
}


function generateRandomResults(nums) {
    for (var i = 0; i < nums; i++) {
        ((Math.floor(Math.random() * 400) + 1))
    }
}
Siavas
  • 4,992
  • 2
  • 23
  • 33
Lucinka
  • 9
  • 4

1 Answers1

1

Your issue is related to the generateRandomResults function, which should return the array with random numbers but at the moment only computes without storing these results anywhere.

Instead, you would need to create an array and return it once the random numbers have been generated:

function generateRandomResults(nums) {
    var randomNumbers = [];
    for (var i = 0; i < nums; i++) {
        randomNumbers.push((Math.floor(Math.random() * 400) + 1));
    }
    return randomNumbers;
}

This way, when retrieving the results of the function (var randomResult = generateRandomResults(3) in your code) the variable will store the array of the three random numbers. This means that instead of pushing the results into your results array, you will have to concatenate your randomNumbers array. So finally your code should be:

...
results.push(correctResult);
var randomResults = generateRandomResults(3);
results.concat(randomResults);
return [randomOperator, [results]];
...
Siavas
  • 4,992
  • 2
  • 23
  • 33
  • Thank you it worked! I only had to make a variable to store the concat results :-) – Lucinka Jan 21 '19 at 10:31
  • You're welcome @Lucinka. If this solution worked for you please make sure to upvote and mark it as the answer. Good luck! – Siavas Jan 21 '19 at 12:30