The exercise 4 is about a game with a deck of cards, the program will check if we get a Ace of Hearts playing cards. If the user get the Ace of hearts with 30 cards he wins.
The program must have a control flow and use arrays.
Code
<html>
<head>
<script>
for(i = 1; i; i++){
let Suit = [‘Hearts’,’Diamonds’,’Clubs’,’Spades’];
let Rank = [‘Ace’,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’10’,’Jack’,’Queen’,’King’];
let aSuit = Math.floor(Math.random()*4);
let aRank = Math.floor(Math.random()*13);
if ( i <= 30 && Suit[aSuit] === ‘Hearts’ && Rank[aRank] === ‘Ace’) {alert (‘You won! You found the Ace Of Hearts : ‘ + i + ‘/30’);
break;
}
else if (i >= 31 && Suit[aSuit] === ‘Hearts’ && Rank[aRank] === ‘Ace’){ alert(‘You lost. Keep trying and find the Ace Of Hearts! : ‘ + i + ‘/30’);
break;
}
}
</script>
</head>
<body>
</body>
</html>