forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex3-tellFortune.js
More file actions
70 lines (57 loc) · 3.02 KB
/
ex3-tellFortune.js
File metadata and controls
70 lines (57 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week3#exercise-3-be-your-own-fortune-teller
Why pay a fortune teller when you can just program your fortune yourself?
1. Create four arrays, `numKids`, `partnerNames`, `locations` and `jobTitles`.
Give each array five random values that have to do with the name of
the variable.
2. Complete the function `selectRandomly`. This function should take an array
as a parameter and return a randomly selected element as its return value.
3. Complete the function named `tellFortune` as follows:
- It should take four arguments (in the order listed):
* the array with the options for the number of children,
* the array with the options for the partner's name,
* the array with the options for the geographic location and
* the array with the options for the job title.
- It should use the `selectRandomly` function to randomly select values from
the arrays.
- It should return a string: "You will be a `jobTitle` in `location`,
married to `partnerName` with `numKids` kids."
4. Call the function three times, passing the arrays as arguments. Use `
console.log` to display the results.
Note: The DRY principle is put into practice here: instead of repeating the code to
randomly select array elements four times inside the `tellFortune` function
body, this code is now written once only in a separated function.
-----------------------------------------------------------------------------*/
// This function should take an array as its parameter and return
// a randomly selected element as its return value.
function selectRandomly(array) {
const randomIndex = Math.floor(Math.random() * array.length);
return array[randomIndex];
}
export function tellFortune(
numKidsArray,
partnerNamesArray,
locationsArray,
jobTitlesArray
) {
// Select random elements from each array using selectRandomly
const numKids = selectRandomly(numKidsArray);
const partnerName = selectRandomly(partnerNamesArray);
const location = selectRandomly(locationsArray);
const jobTitle = selectRandomly(jobTitlesArray);
// Return the formatted fortune string
return `You will be a ${jobTitle} in ${location}, married to ${partnerName} with ${numKids} kids.`;
}
function main() {
const numKids = [0, 1, 2, 3, 4]; // Array of 5 possible numbers of kids
const partnerNames = ['Alice', 'Bob', 'Charlie', 'Dana', 'Eve']; // Array of 5 partner names
const locations = ['Amsterdam', 'Berlin', 'London', 'Paris', 'Rome']; // Array of 5 locations
const jobTitles = ['Engineer', 'Teacher', 'Designer', 'Chef', 'Artist']; // Array of 5 job titles
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
console.log(tellFortune(numKids, partnerNames, locations, jobTitles));
}
// ! Do not change or remove the code below
if (process.env.NODE_ENV !== 'test') {
main();
}