|
| 1 | +--- |
| 2 | +id: 66c06d618d075c7f7f1b890a |
| 3 | +title: Build a Fortune Teller |
| 4 | +challengeType: 14 |
| 5 | +dashedName: build-a-fortune-teller |
| 6 | +--- |
| 7 | + |
| 8 | +# --description-- |
| 9 | + |
| 10 | +In this lab, you will define five fortunes and randomly select one of them to display the fortune to the user. |
| 11 | + |
| 12 | +**Objective:** Fulfill the user stories below and get all the tests to pass to complete the lab. |
| 13 | + |
| 14 | +**User Stories:** |
| 15 | + |
| 16 | +1. You should initialize the five variables `fortune1`, `fortune2`, `fortune3`, `fortune4`, and `fortune5` with a string value of your choice. You can use the below options if you like: |
| 17 | + |
| 18 | + - `"Your cat will look very cuddly today."` |
| 19 | + - `"The weather will be nice tomorrow."` |
| 20 | + - `"Be cautious of your new neighbours."` |
| 21 | + - `"You will find a new hobby soon."` |
| 22 | + - `"It would be wise to avoid the colour red today."` |
| 23 | + |
| 24 | +2. You should select a random number between 1 and 5, inclusive, and assign it to the variable `randomNumber`. |
| 25 | + |
| 26 | +3. You should create a `selectedFortune` variable and assign the appropriate fortune based on these rules: |
| 27 | + |
| 28 | + - If `randomNumber` is 1, assign the value of `fortune1` to `selectedFortune`. |
| 29 | + - If `randomNumber` is 2, assign the value of `fortune2` to `selectedFortune`. |
| 30 | + - If `randomNumber` is 3, assign the value of `fortune3` to `selectedFortune`. |
| 31 | + - If `randomNumber` is 4, assign the value of `fortune4` to `selectedFortune`. |
| 32 | + - If `randomNumber` is 5, assign the value of `fortune5` to `selectedFortune`. |
| 33 | + |
| 34 | +4. You should log the `selectedFortune` to the console. |
| 35 | + |
| 36 | +# --hints-- |
| 37 | + |
| 38 | +You should initialize `fortune1` with a string value. |
| 39 | + |
| 40 | +```js |
| 41 | +assert.isNotNull(fortune1); |
| 42 | +assert.isString(fortune1); |
| 43 | +``` |
| 44 | + |
| 45 | +You should initialize `fortune2` with the a string value. |
| 46 | + |
| 47 | +```js |
| 48 | +assert.isNotNull(fortune2); |
| 49 | +assert.isString(fortune2); |
| 50 | +``` |
| 51 | + |
| 52 | +You should initialize `fortune3` with a string value. |
| 53 | + |
| 54 | +```js |
| 55 | +assert.isNotNull(fortune3); |
| 56 | +assert.isString(fortune3); |
| 57 | +``` |
| 58 | + |
| 59 | +You should initialize `fortune4` with a string value. |
| 60 | + |
| 61 | +```js |
| 62 | +assert.isNotNull(fortune4); |
| 63 | +assert.isString(fortune4); |
| 64 | +``` |
| 65 | + |
| 66 | +You should initialize `fortune5` with a string value. |
| 67 | + |
| 68 | +```js |
| 69 | +assert.isNotNull(fortune5); |
| 70 | +assert.isString(fortune5); |
| 71 | +``` |
| 72 | + |
| 73 | +You should use the `Math.random()` method to generate a random number. |
| 74 | + |
| 75 | +```js |
| 76 | +assert.match(__helpers.removeJSComments(code), /Math\.random\(\)/); |
| 77 | +``` |
| 78 | + |
| 79 | +You should generate a random number between 1 and 5, inclusive, and assign it to the variable `randomNumber`. |
| 80 | + |
| 81 | +```js |
| 82 | +assert.isNotNull(randomNumber); |
| 83 | +assert.include([1,2,3,4,5], randomNumber); |
| 84 | +``` |
| 85 | + |
| 86 | +You should have a `selectedFortune` variable that is assigned a value based on the value of `randomNumber`. |
| 87 | + |
| 88 | +```js |
| 89 | +assert.isNotNull(selectedFortune); |
| 90 | +``` |
| 91 | + |
| 92 | +The `randomNumber` should correspond to its fortune. For example, if `randomNumber` is 1, the `selectedFortune` should be equal to `fortune1` and so on. |
| 93 | + |
| 94 | +```js |
| 95 | +const condition1 = randomNumber === 1 && selectedFortune === fortune1; |
| 96 | +const condition2 = randomNumber === 2 && selectedFortune === fortune2; |
| 97 | +const condition3 = randomNumber === 3 && selectedFortune === fortune3; |
| 98 | +const condition4 = randomNumber === 4 && selectedFortune === fortune4; |
| 99 | +const condition5 = randomNumber === 5 && selectedFortune === fortune5; |
| 100 | + |
| 101 | +assert(condition1 || condition2 || condition3 || condition4 || condition5); |
| 102 | +``` |
| 103 | + |
| 104 | +You should output the `selectedFortune` to the console. |
| 105 | + |
| 106 | +```js |
| 107 | +assert.match(__helpers.removeJSComments(code), /console\s*\.\s*log\s*\(\s*selectedFortune\s*\)\s*;?/); |
| 108 | +``` |
| 109 | + |
| 110 | +# --seed-- |
| 111 | + |
| 112 | +## --seed-contents-- |
| 113 | + |
| 114 | +```js |
| 115 | + |
| 116 | +``` |
| 117 | + |
| 118 | +# --solutions-- |
| 119 | + |
| 120 | +```js |
| 121 | +const fortune1 = "Your cat will look very cuddly today."; |
| 122 | +const fortune2 = "The weather will be nice tomorrow."; |
| 123 | +const fortune3 = "Be cautious of your new neighbours."; |
| 124 | +const fortune4 = "You will find a new hobby soon."; |
| 125 | +const fortune5 = "It would be wise to avoid the colour red today."; |
| 126 | + |
| 127 | +let randomNumber = Math.floor(Math.random() * 5) + 1; |
| 128 | + |
| 129 | +let selectedFortune; |
| 130 | + |
| 131 | +if (randomNumber === 1) { |
| 132 | + selectedFortune = fortune1; |
| 133 | +} else if (randomNumber === 2) { |
| 134 | + selectedFortune = fortune2; |
| 135 | +} else if (randomNumber === 3) { |
| 136 | + selectedFortune = fortune3; |
| 137 | +} else if (randomNumber === 4) { |
| 138 | + selectedFortune = fortune4; |
| 139 | +} else { |
| 140 | + selectedFortune = fortune5; |
| 141 | +} |
| 142 | + |
| 143 | +console.log(selectedFortune); |
| 144 | +``` |
0 commit comments