-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (53 loc) · 1.83 KB
/
script.js
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
let prompts = {
uno: [
"line weight",
"shape and form construction",
"composition",
"values and light",
"depth and perspective",
],
dos: [
"materials and textures",
"clothes and drapery",
"scenery and background",
"human anatomy",
"animal anatomy",
],
tres: ["character design", "environments"],
};
function chooseRandomly() {
let arr = [];
Array.from(
document.querySelectorAll('input[type="checkbox"]:checked')
).forEach((item) => {
arr = [...arr, ...prompts[item.getAttribute("id")]];
});
if (arr.length === 0) return alert("You must select an option!");
var randomNumber = Math.floor(Math.random() * arr.length);
document.getElementById("result").textContent = arr[randomNumber];
}
/////////// same function but using a ternary operator
// function chooseRandomly() {
// let arr = [];
// arr = arr.concat(document.getElementById("uno").checked ? uno : []);
// arr = arr.concat(document.getElementById("dos").checked ? dos : []);
// arr = arr.concat(document.getElementById("tres").checked ? tres : []);
//
// if (arr.length === 0) return alert("You must select an option!");
// var randomNumber = Math.floor(Math.random() * arr.length);
// document.getElementById("result").textContent = arr[randomNumber];
// }
////////////////// same function but using the Array.reduce()
// function chooseRandomly() {
// let arr = [];
// arr = Array.from(document.querySelectorAll('input[type="checkbox"]')).reduce(
// (arr, item) => {
// if (!item.checked) return arr;
// return [...arr, ...prompts[item.getAttribute("id")]];
// },
// []
// );
// if (arr.length === 0) return alert("You must select an option!");
// var randomNumber = Math.floor(Math.random() * arr.length);
// document.getElementById("result").textContent = arr[randomNumber];
// }