Answers for "heads tails js code"

1

heads tails js code

function headsTails(bet) {
  //First of all, let's create a random number between 1 and 10.
    let casual = Math.floor(Math.random() * 10) + 1;
  //Then we define a result string that contains the result of the bet.
    let result = '';
  //Now we will define if the result is heads or tails:
  //if casual is more than 5, then it's heads;
    if(casual > 5) {
        result = 'heads';
    } else {
  //if casual is less than 5, then it's tails.
        result = 'tails'
    };
  //Now we chek if the user input a bet that isn't heads or tails.
    if(bet !== 'heads' && 'tails') {
  //If so, then we warn the user with a message in the console.
            console.log("Please choose between heads or tails and put your choice as parameter of the function. I.e. headsTails('heads')/headsTails('tails').");
    } else if(bet.toLowerCase() == result) {
  //It's time to let know to the user if he won or not.
        console.log('You won!');
    } else {
        console.log('You lost!')
    };
};
//Enjoy!!!
Posted by: Guest on September-26-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language