vendredi 30 octobre 2020

Random dice roll with 6 counters using arrays

This excersise is a bit challenging. Beginner coder using Javascript console. I can't figure out how to print each number in it's own row.

Question:

Use variables, arrays and constants to show how random dice roll. Define a loop that will call the random function 10000 times. Use 6 counters to show when each number appears. Display the die number and the number of times it appeared followed by a scaled histogram that shows 1 star for each 40 appearances.

Output should look like this:

1: (1587) 11111111111111111111111
2: (1542) 222222222222222
3: (1561) 333333333333333333333333
4: (1590) 44444444444444444444444
5: (1523) 555555555555555555
6: (1509) 66666666666666666666

Here is what I have so far.

var NUM_ROLLS = 10000;

function start(){
    var rolls = diceRoll();
    printArray(rolls);
}

//I divided NUM_ROLLS by 500 because I didn't want it to crush.//
//My output is not correct.//

function diceRoll(){
    var rolls = [];
    for(var i = 0; i < NUM_ROLLS/500; i++){
        if(Randomizer.nextBoolean()){
            rolls.push([1]);
            rolls.push([2]);
            rolls.push([3]);
            rolls.push([4]);
            rolls.push([5]);
            rolls.push([6]);
        
        }
    }
    return rolls;
}

function printArray(arr){
    for(var i = 1; i < arr.length; i++){
        println(i + ": " + arr[i]);
    }
}



Aucun commentaire:

Enregistrer un commentaire