M7 IA1 Task1 Exercise4 Irene Arroyo

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>

Exercise 4 | Ace Of Hearts

M7 IA1 Task1 Exercise3 Irene Arroyo

In this exercise we have to create a game called ‘ Heads or Tails ‘, which implies a coin flipped 3 times and 2 players, one is heads and the other one is tails. Wins the player who have more times ‘heads’ or ‘tails’ on the 3 flip round.

This program requires a control flow, 2 prompts that ask the names of the two players, a loop, I decided to do it with the for loop for the coin flips, and an if else statement to define who won, based on how many times heads have appeared.

Code

<html>
<head>
<script>
let nameHead = prompt (‘What is the name of the heads player?’)
let nameTail = prompt (‘What is the name of the tails player?’)

let headThrows = 0;
let tailThrows = 0; ;

for ( let i = 0; i<3; i++)

{ let coin = (Math.floor(Math.random() * 2));

if ( coin === 1) { headThrows++;
alert ( ‘Player ‘ + nameHead + ‘ has won ‘ + headThrows);}

else { tailThrows++;
alert (‘Player ‘ + nameTail + ‘ has won ‘ + tailThrows); }
}

if (headThrows< 2) { alert (‘Congratulations ‘ + nameTail + ‘! Tails win!’) }
else {alert ( ‘Congratulations ‘ + nameHead + ‘! Heads win!’); }

</script>
</head>
<body>
</body>
</html>

Exercise 3 | Heads or Tails

M7 IA1 Task1 Exercise2 Irene Arroyo

In this exercise, we must use functions to develop the activity, which implies creating a program that throws a dice and gives you a number from 1 to 6.

The following code is the one used in the exercise

Code

<html>
<head>
<script>

let dice = 0;
confirm(‘Do you want to throw the dice?’);

let throwDice = () => {
dice = Math.floor(Math.random() * 6 )+ 1;

alert (dice);}

throwDice();

window.location.href = window.location.href

</script>
</head>
<body>
</body>
</html>

Exercise 2 | Dice

M7 IA1 Task1 Exercise1 Irene Arroyo

This exercise is about control pass.

We must do a pop up box asking the user name and the age, if the user is 18 or older, the page allow the user to see a website, if it’s under 18 the website is not going to show.

Code

<html>
<html>
<head>
<script>

let name = prompt(“Please enter your name”);

let age = prompt ( “How old are you?” );

if ( age >= 18) {

location.href = ‘https://playmax.mx/it-f42042-dc=o4c7c8t5s6m8’;
alert (” Okay ” + name + “, get ready to play!” ); }

else { alert ( ” Sorry ” +  name + “. You aren’t old enough to play this game!”); }

</script>
</head>
<body>
</body>

Exercise 1 | Control Pass