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
73 lines (57 loc) · 2.74 KB
/
ex3-tellFortune.js
File metadata and controls
73 lines (57 loc) · 2.74 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
/*------------------------------------------------------------------------------
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(choices) {
return choices[Math.floor(Math.random() * choices.length)];
}
export function tellFortune(kids, partners, places, jobs) {
const kidCount = selectRandomly(kids);
const partner = selectRandomly(partners);
const place = selectRandomly(places);
const job = selectRandomly(jobs);
return `You will be a ${job} in ${place}, married to ${partner} with ${kidCount} kids.`;
}
function main() {
const numKids = [
1, 2, 3, 4, 5
];
const partnerNames = [
"Anna", "Dasha", "Vlad", "Kostya", "Oleg"
];
const locations = [
"Rivne", "Kyiv", "Odessa", "Ternopil", "Dnipro"
];
const jobTitles = [
"Frontend Developer", "Backend Developer", "Designer", "Tester", "Manager"
];
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();
}