Name: | Number of slices: | Topping Preference: |
---|---|---|
Jane | 2 | Mushroom, onion, peppers, olives |
Lisa | 3 | Pepperoni, ham, pinapple |
Taylor | 3 | Extra cheese, pepperoni, sausage, bacon |
Chris | 2 | Mushroom, sausage, bacon, ham, onion, peppers |
Alyssa | 1 | Pepperoni, bacon |
Will | 2 | Extra cheese, sausage, bacon, onion, peppers, olives |
Jessica | 2 | Pepperoni, bacon, ham, pineapple, onion, peppers |
The body tag should contain "body onload="dinnerCheck()" function Person(slices, toppings) { this.slices = slices; this.toppings = toppings; } var Jane = new Person(2, ["mushroom","onion","peppers","olive"]); var Lisa = new Person(3, ["pepperoni","ham","pineapple"]); var Taylor = new Person(3, ["extra cheese","pepperoni","sausage","bacon"]); var Chris = new Person(2, ["mushroom","sausage","bacon","ham","onion","peppers"]); var Alyssa = new Person(1, ["pepperoni","bacon"]); var Will = new Person(2, ["extra cheese","sausage","bacon","onion","peppers","olive"]); var Jessica = new Person(2, ["pepperoni","bacon","ham","pineapple","onion","pepper"]); function arrayCount(array, what) { var count = 0; for (var i = 0; i < array.length; i++) { if (array[i] === what) { count++; } } return count; } function dinnerCheck(){ var question = prompt("Who is coming over to our dinner party?", "Jane, Taylor, Will"); if (question !== null && question != "") {//check to see if something is typed in the box. var people = question.split(", ");//People should be entered with a comma between them, this will create an array out of the strings between commas. var slices = 0; var tempArray = [];//tempArray will hold all of the toppings that everyone likes. for (i = 0; i < people.length; i++) {//for each person listed, add their slices together, and add their toppings to the tempArray switch (people[i]) { case "Jane": slices += Jane.slices; tempArray = tempArray.concat(Jane.toppings); break; case "Lisa": slices += Lisa.slices; tempArray = tempArray.concat(Lisa.toppings); break; case "Taylor": slices += Taylor.slices; tempArray = tempArray.concat(Taylor.toppings); break; case "Chris": slices += Chris.slices; tempArray = tempArray.concat(Chris.toppings); break; case "Alyssa": slices += Alyssa.slices; tempArray = tempArray.concat(Alyssa.toppings); break; case "Will": slices += Will.slices; tempArray = tempArray.concat(Will.toppings); break; case "Jessica": slices += Jessica.slices; tempArray = tempArray.concat(Jessica.toppings); break; default: alert(people[i] + "can't make it!\n\nMake sure you wrote the names like this:\nName1, Name2, Name3...\n\nThe names are cAsE-SeNsItIvE!"); delete people[i]; }; }; for (a = 0; a < people.length; a++) { if (people[a] === undefined) { people.splice(a, 1);//splice will remove a--; }; }; if (people.length == 0) { return; };//nobody coming, you have no friends var sortArray = tempArray.slice().sort();//create an array of ALL of the Toppings everyone likes. var toppings = []; if (people.length > 1) { for (x = 0; x < sortArray.length; x++) {//count the number of times each topping appears in the list of liked toppings, and IF it matches the number of people coming, then everyone likes that topping, you should add it to the pizza. if (arrayCount(sortArray, sortArray[x]) == people.length) { toppings.push(sortArray[x]); sortArray.splice(x, 1); }; }; } else { toppings = tempArray; }; if (toppings.length == 0) {//if no toppings were decided toppings[0] = "plain cheese"; }; document.getElementById("output").innerHTML = "You will need " + slices + " slices of " + (toppings.join(", ")) + " pizza."; }; } </script> <br> <a href="https://jarldavidson.glitch.me">back to index</a> </body> </html>