heads tails js code

Solutions on MaxInterview for heads tails js code by the best coders in the world

showing results for - "heads tails js code"
Julian
29 Jun 2017
1function headsTails(bet) {
2  //First of all, let's create a random number between 1 and 10.
3    let casual = Math.floor(Math.random() * 10) + 1;
4  //Then we define a result string that contains the result of the bet.
5    let result = '';
6  //Now we will define if the result is heads or tails:
7  //if casual is more than 5, then it's heads;
8    if(casual > 5) {
9        result = 'heads';
10    } else {
11  //if casual is less than 5, then it's tails.
12        result = 'tails'
13    };
14  //Now we chek if the user input a bet that isn't heads or tails.
15    if(bet !== 'heads' && 'tails') {
16  //If so, then we warn the user with a message in the console.
17            console.log("Please choose between heads or tails and put your choice as parameter of the function. I.e. headsTails('heads')/headsTails('tails').");
18    } else if(bet.toLowerCase() == result) {
19  //It's time to let know to the user if he won or not.
20        console.log('You won!');
21    } else {
22        console.log('You lost!')
23    };
24};
25//Enjoy!!!