1+ 'use strict'
2+
3+ const drinkTypes = [ 'cola' , 'lemonade' , 'water' ] ;
4+ const drinkTray = [ ] ;
5+
6+ let drinkers = 10 ;
7+
8+ for ( let i = 0 ; i <= drinkers - 1 ; i = i + 1 ) {
9+ const typeIndex = i % drinkTypes . length ;
10+ drinkTray [ i ] = drinkTypes [ typeIndex ] ; }
11+
12+ console . log ( `Hey guys, I brought a ${ drinkTray . join ( ', ' ) } !` ) ;
13+
14+ //Completed with a little help from a friend.
15+
16+
17+ /*
18+ const drinkTypes = ['cola', 'lemonade', 'water'];
19+ const drinkTray = [];
20+
21+ // let i = 0; i =< 4;i++
22+ // let i = 0; we start from number 0
23+ // i =< 4; the condition, we stop the loop as soon as the condition is no longer TRUTHY;
24+ // i++; incrementation; i += 1; i = i + 1;
25+ function countOccurences(drink, drinkTray) {
26+ let count = 0;
27+ drinkTray.forEach(drinkOnTray => {
28+ if (drinkOnTray === drink) {
29+ count++;
30+ }
31+ });
32+ return count;
33+ }
34+
35+ // Fixed solution, solving the problem but only for the specific numbers of 5
36+ for(let i = 0; i <= 4;i++) {
37+ if (i === 0 || i === 1) {
38+ drinkTray.push(drinkTypes[0]);
39+ } else if (i === 2 || i === 3) {
40+ drinkTray.push(drinkTypes[1]);
41+ } else {
42+ drinkTray.push(drinkTypes[2]);
43+ }
44+ }
45+
46+ console.log("IN DRINKTRAY", drinkTray);
47+
48+ // Dynamic solution, that will work any amount of drinks and conditions
49+ const numberPerDrink = 2;
50+ const totalDrinks = 5;
51+
52+ drinkTypes.forEach(drink => {
53+ for(let i = 0; i <= 1; i++) {
54+ if (drinkTray.length < totalDrinks) {
55+ drinkTray.push(drink);
56+ }
57+ }
58+ })
59+
60+ console.log("IN DRINKTRAY", drinkTray);
61+
62+ */
0 commit comments