Skip to content

Commit 63ac0a9

Browse files
committed
added lecture notes and examples from functions lecture
1 parent c7973d3 commit 63ac0a9

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

js_functions/lectures/blackjack.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// the only cards we will be taking are: ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
2+
// picure cards are 10 points
3+
// Other cards are just equal to there number
4+
// Aces is 11. If there's double ace, they will be 12 (11 + 1)
5+
// THERE IS NO NUMBER 1 CARD (that is an ace)
6+
7+
const blackjack = function (card1, card2) {
8+
let points = 0
9+
10+
// jack, queen, king are 10 points
11+
12+
if (card1 === "jack" || card1 === "queen" || card1 === "king") {
13+
card1 = 10
14+
} else {
15+
card1
16+
}
17+
18+
if (card2 === "jack" || card2 === "queen" || card2 === "king") {
19+
card2 = 10
20+
} else {
21+
card2
22+
}
23+
24+
if ((card1 === "ace" && card2 === 10) || (card2 === "ace" && card1 === 10)) {
25+
console.log("BLACKJACK!!!")
26+
// to win, get 21 points (that is a "blackjack")
27+
} else if (card1 === "ace" && card2 === "ace") {
28+
console.log("You have 2 aces, which means you have 12 points. Would you like to draw again?")
29+
//(unless you get 2 aces. one will be 11, one will be 1. So the total is 12)
30+
} else if (card1 === "ace" && card2 < 10) { // ace is 11 points
31+
card1 = 11
32+
points = card1 + card2
33+
console.log(`You have ${points} points. Would you like to draw again?`)
34+
} else if (card2 === "ace" && card1 < 10) { // ace is 11 points
35+
card2 = 11
36+
points = card1 + card2
37+
console.log(`You have ${points} points. Would you like to draw again?`)
38+
} else { // other cards are just equal to there number
39+
points = card1 + card2
40+
console.log(`You have ${points} points. Would you like to draw again?`)
41+
// anything else, total the points and ask the player if the wanna draw
42+
}
43+
44+
}
45+
46+
// blackjack(10, 10)
47+
48+
// another example (not so DRY)
49+
50+
// const blackjack = function (card1, card2) {
51+
// if (card1 === 'ace' && card2 === 'ace') {
52+
// console.log("You have 12 points, would you like to draw again?");
53+
// } else if (
54+
// (card1 === 'jack' && card2 === 'ace') ||
55+
// (card1 === 'ace' && card2 === 'jack') ||
56+
// (card1 === 'queen' && card2 === 'ace') ||
57+
// (card1 === 'ace' && card2 === 'queen') ||
58+
// (card1 === 'king' && card2 === 'ace') ||
59+
// (card1 === 'ace' && card2 === 'king') ||
60+
// (card1 === 'ace' && card2 == 10) ||
61+
// (card1 == 10 && card2 === 'ace')
62+
// ) {
63+
// console.log('You got 21 points, so You win!');
64+
// } else if (
65+
// (card1 === 'jack' && card2 === 'jack') ||
66+
// (card1 === 'jack' && card2 === 'queen') ||
67+
// (card1 === 'jack' && card2 === 'king') ||
68+
// (card1 === 'queen' && card2 === 'jack') ||
69+
// (card1 === 'queen' && card2 === 'queen') ||
70+
// (card1 === 'queen' && card2 === 'king') ||
71+
// (card1 === 'king' && card2 === 'jack') ||
72+
// (card1 === 'king' && card2 === 'queen') ||
73+
// (card1 === 'king' && card2 === 'king')
74+
// ) {
75+
// console.log('You got 20 points. Do you want to draw again?');
76+
// } else if (card1 === 'ace' && card2 === 'ace') {
77+
// console.log("You have 2 'aces', so you got 12 points!");
78+
// } else if (
79+
// (card1 === 'ace' && card2 == 2) ||
80+
// (card1 === 'ace' && card2 == 3) ||
81+
// (card1 === 'ace' && card2 == 4) ||
82+
// (card1 === 'ace' && card2 == 5) ||
83+
// (card1 === 'ace' && card2 == 6) ||
84+
// (card1 === 'ace' && card2 == 7) ||
85+
// (card1 === 'ace' && card2 == 8) ||
86+
// (card1 === 'ace' && card2 == 9)
87+
// ) {
88+
// console.log(
89+
// `You got ${11 + Number(card2)} points. Do you want to draw again?`
90+
// );
91+
// } else if (
92+
// (card2 === 'ace' && card1 == 2) ||
93+
// (card2 === 'ace' && card1 == 3) ||
94+
// (card2 === 'ace' && card1 == 4) ||
95+
// (card2 === 'ace' && card1 == 5) ||
96+
// (card2 === 'ace' && card1 == 6) ||
97+
// (card2 === 'ace' && card1 == 7) ||
98+
// (card2 === 'ace' && card1 == 8) ||
99+
// (card2 === 'ace' && card1 == 9)
100+
// ) {
101+
// console.log(
102+
// `You got ${11 + Number(card1)} points. Do you want to draw again?`
103+
// );
104+
// } else {
105+
// console.log(
106+
// `You got ${Number(card1) + Number(card2)
107+
// } points. Do you want to draw again?`
108+
// );
109+
// }
110+
// };
111+
112+
// blackjack("ace", "10")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const aboutMe = function(){ // aboutMe is our function name
2+
console.log("Hello my name is Chingyee.")
3+
console.log("I live in Vancouver at the moment.")
4+
console.log("However, I'm from the UK.")
5+
console.log("I move to Vancouver because I like to snowbaord.")
6+
// console.logging bio to print inn terminal
7+
}
8+
9+
aboutMe(); // this is calling the function. If I don't do this, the code will not run
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
let points = 0
2+
const blackjack = function (card1, card2) {
3+
if (card1 === "jack" || card1 === "queen" || card1 === "king") {
4+
card1 = 10
5+
} else {
6+
card1
7+
}
8+
9+
if (card2 === "jack" || card2 === "queen" || card2 === "king") {
10+
card2 = 10
11+
} else {
12+
card2
13+
}
14+
15+
if ((card1 === "ace" && card2 == 10) || (card2 === "ace" && card1 == 10)) {
16+
console.log("BLACKJACK!")
17+
return points = 21
18+
} else if (card1 === "ace" && card2 === "ace") {
19+
console.log(`You have 2 aces which is currently 12 points. Would you like to draw again?`)
20+
return points = 12
21+
} else if (card1 === "ace" && card2 < 10) {
22+
card1 = 11
23+
points = card1 + card2
24+
console.log(`You have ${points} points. Would you like to draw again?`)
25+
return points
26+
} else if (card2 === "ace" && card1 < 10) {
27+
card2 = 11
28+
points = card1 + card2
29+
console.log(`You have ${points} points. Would you like to draw again?`)
30+
return points
31+
} else {
32+
points = card1 + card2
33+
console.log(`You have ${points} points. Would you like to draw again?`)
34+
return points
35+
}
36+
37+
}
38+
blackjack("jack", "ace")
39+
console.log(points)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const weatherForecast = function(weather) {
2+
if (weather === "snowy") {
3+
console.log(`It's ${weather} today, you should snowboard today!`)
4+
} else if (weather === "thundering") {
5+
console.log(`It's ${weather} today, you should stay in and watch a movie!`)
6+
} else if (weather === "rainy") {
7+
console.log(`It's ${weather} today, you should Netflix and chill!`)
8+
} else if (weather === "sunny") {
9+
console.log(`It's ${weather} today, you should go outside and eat ice cream!`)
10+
} else {
11+
console.log(`It's ${weather} today. Have a good day :)`)
12+
}
13+
}
14+
15+
weatherForecast("hot")
16+
17+
// shorthand version
18+
19+
// function weatherForecast(weather) {
20+
// if (weather === "snowy") {
21+
// console.log(`It's ${weather} today, you should snowboard today!`)
22+
// } else if (weather === "thundering") {
23+
// console.log(`It's ${weather} today, you should stay in and watch a movie!`)
24+
// } else if (weather === "rainy") {
25+
// console.log(`It's ${weather} today, you should Netflix and chill!`)
26+
// } else if (weather === "sunny") {
27+
// console.log(`It's ${weather} today, you should go outside and eat ice cream!`)
28+
// } else {
29+
// console.log(`It's ${weather} today. Have a good day :)`)
30+
// }
31+
// }
32+
33+
// weatherForecast("hot")

0 commit comments

Comments
 (0)