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
84 lines (68 loc) · 2.81 KB
/
ex3-tellFortune.js
File metadata and controls
84 lines (68 loc) · 2.81 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*------------------------------------------------------------------------------
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(arr) {
const randomIndex = Math.floor(Math.random() * arr.length)
return arr[randomIndex]
}
export function tellFortune(numKids, partnerNames, locations, jobTitles) {
const kids = selectRandomly(numKids)
const partner = selectRandomly(partnerNames)
const location = selectRandomly(locations)
const job = selectRandomly(jobTitles)
return `You will be a ${job} in ${location}, married to ${partner} with ${kids} kids.`
}
function main() {
const numKids = [1, 2, 3, 4, 5];
const partnerNames = [
'Sharon',
'Anna',
'Veronika',
'Stacy',
'Jennie'
];
const locations = [
'Amsterdam',
'Haarlem',
'Paris',
'Rotterdam',
'Groningen'
];
const jobTitles = [
'Front dev',
'Back dev',
'DevOps',
'Q&A',
'Cleaner'
];
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();
}