Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5362819

Browse files
committed
Completed who-wants-a-drink.js practice exercise.
1 parent 5826029 commit 5362819

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

Week1/practice-exercises/5-who-wants-a-drink.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,28 @@
1111
const drinkTypes = ['cola', 'lemonade', 'water'];
1212

1313
let drinkTray = [];
14-
const loops = 5;
14+
let loops = 5;
1515

16-
while(loops < 5){
17-
let drinkType = drinkTypes[Math.floor(Math.random()*3)];
18-
// not completed yet
19-
loops.unshift(drinkType);
20-
loops++;
16+
while(loops > 0){
17+
const drinkType = drinkTypes[Math.floor(Math.random()*3)];
18+
const maxNumOfDrinkType = 2;
19+
20+
const countDrinkType = drinkTray.filter(function(drink) {
21+
return drink === drinkType;
22+
}).length;
23+
24+
if(countDrinkType === maxNumOfDrinkType){
25+
continue;
26+
} else{
27+
drinkTray.unshift(drinkType);
28+
loops--;
29+
}
30+
}
31+
32+
if(!drinkTray.length){
33+
console.log("I didn't brought drinks.");
34+
}else{
35+
console.log(`Hey guys I brought ${drinkTray.join(', ')}`);
2136
}
37+
38+

0 commit comments

Comments
 (0)