Search code examples
javascriptfunctionmathrandom

Unable to generate and show randomly generated integers


I'm a student and have been attempting to create a program in JavaScript to create a function and generate a random integer (whole number) between 1 and 9. I have created two different programs for this in my testing but while using Visual Studio Code it doesn't print the generated number.

Please see code below and if you have any questions please let me know.

function random_num() {
    let random_num = Math.floor(Math.random() * 9) +1;
    return random_num;
}
console.log(random_num);
function random_num(min, max) {
    min = Math.ceil(1);
    max = Math.floor(9);
    return Math.floor(Math.random() * (max - min)) + min;
}
console.log(random_num);

Thanks in advance!


Solution

  • Always save your result in variable then print it
    

    function random_num(min, max) {
        min = Math.ceil(1);
        max = Math.floor(9);
        return Math.floor(Math.random() * (max - min)) + min;
    }
    
    const randomNumber1 = random_num()
    console.log(randomNumber1);

    enter image description here

    'use strict';
    
    function random_num() {
        let random_num = Math.floor(Math.random() * 9) +1;
        return random_num;
    }
    
    const randomNumber = random_num()
    console.log(randomNumber);