-
Notifications
You must be signed in to change notification settings - Fork 0
/
babby.html
59 lines (46 loc) · 1.97 KB
/
babby.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Babby Names</title>
<script>
// Initialize the list of baby names
let babyNames = [];
// Prompt the user to enter the initial list of baby names
let numNames = prompt("Enter the number of baby names you want to compare: ");
// Validate the input to make sure it's a positive integer
while (isNaN(numNames) || numNames <= 0) {
numNames = prompt("Invalid input. Enter a positive integer for the number of baby names: ");
}
// Prompt the user to enter the baby names
for (let i = 0; i < numNames; i++) {
let name = prompt("Enter baby name #" + (i + 1) + ": ");
babyNames.push(name);
}
// Keep prompting the user to compare 3 baby names until there's only one left
while (babyNames.length > 1) {
nameList = []
while (nameList.length < 3 && nameList.length <babyNames.length) {
let name = babyNames[Math.floor(Math.random() * babyNames.length)];
if (!nameList.includes(name)) {
nameList.push(name);
}
}
// Prompt the user to select the name they like the least
let leastFavorite = prompt("Which of these names do you like the least?\n" + nameList.join("\n"));
// Validate the input to make sure it's one of the 3 names
while (nameList.indexOf(leastFavorite) == -1) {
leastFavorite = prompt("Invalid input. Please select one of the following names:\n" + nameList.join("\n"));
}
// Remove the least favorite name from the list
babyNames.splice(babyNames.indexOf(leastFavorite), 1);
}
// Display the remaining baby name
alert("The remaining baby name is: " + babyNames[0]);
</script>
</head>
<body>
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
</body>
</html>