diff --git a/src/01-dinosaur-facts.js b/src/01-dinosaur-facts.js index 90e0fc5..31cb0e7 100644 --- a/src/01-dinosaur-facts.js +++ b/src/01-dinosaur-facts.js @@ -22,7 +22,20 @@ const exampleDinosaurData = require("../data/dinosaurs"); * getLongestDinosaur(dinosaurs); * //> { Brachiosaurus: 98.43 } */ -function getLongestDinosaur(dinosaurs) {} +function getLongestDinosaur(dinosaurs) { + if (dinosaurs.length === 0) { // checking if theres no dinosaurs + return {} //if there is no dinosaurs , we are returning an empty object + } + let longestDino = dinosaurs[0] // we are letting L.D equal dinosaurs at the first index + for (let i = 1; i < dinosaurs.length; i++) { //starting our for loop at 1 since we are already starting at 0 + if (dinosaurs[i].lengthInMeters > longestDino.lengthInMeters) { // are condition her is testing if dino at postion [i] is greater than lonestDino at length in meters + longestDino = dinosaurs[i] // here we are setting longest to be equal to dinosaurs at index i + } + } + let finalDino = {} // here we are creating an empty object to be able to return our value in + finalDino[longestDino.name] = longestDino.lengthInMeters * 3.281 //here we are setting up the in the proper order , finding the the name of the dino = to the length in meters * 3.381 to covert it to feet + return finalDino // returning the longest dino +} /** * getDinosaurDescription() @@ -44,7 +57,14 @@ function getLongestDinosaur(dinosaurs) {} * getDinosaurDescription(dinosaurs, "incorrect-id"); * //> "A dinosaur with an ID of 'incorrect-id' cannot be found." */ -function getDinosaurDescription(dinosaurs, id) {} +function getDinosaurDescription(dinosaurs, id) { + for (let i = 0; i < dinosaurs.length; i++) { // here i created a for loop to go thur the array of dino + if (dinosaurs[i].dinosaurId === id) //checking if dino at index i is equal to the id + return `${dinosaurs[i].name} (${dinosaurs[i].pronunciation})\n${dinosaurs[i].info} It lived in the ${dinosaurs[i].period} period, over ${dinosaurs[i].mya[dinosaurs[i].mya.length - 1]} million years ago.` // if the condition is true we return the dino in the this format, providing information thur dot notiation + } + return `A dinosaur with an ID of '${id}' cannot be found.` // if the dino at index i != the the id we want to return a statment saying the dino was not found with the id. we do this outside the for loop +} + /** * getDinosaursAliveMya() @@ -71,7 +91,27 @@ function getDinosaurDescription(dinosaurs, id) {} * getDinosaursAliveMya(dinosaurs, 65, "unknown-key"); * //> ["WHQcpcOj0G"] */ -function getDinosaursAliveMya(dinosaurs, mya, key) {} +function getDinosaursAliveMya(dinosaurs, mya, key) { + let dino = [] // here i set dino to an empty array since we see that the return is in an array + for (let i = 0; i < dinosaurs.length; i++) { // creata a for loop in order to compare the value at index i to the set mya + if (dinosaurs[i].mya.length === 1) { // here im checking if the dinosaur only has a single value for `mya` + if (dinosaurs[i].mya[0] === mya || dinosaurs[i].mya[0] - 1 === mya) { // i did a nested if statment, where we already determined that there is one single value. I am checking to see if dino. at the random index of i mya at the first index is equaly to mya or a dino. at index i - 1 is equal to mya + if (key) { // another nested if statment to check if the key is included + dino.push(dinosaurs[i][key]) // if key is included i pushed using .push() , dino. at index i and the key properitys into empty array of dino + } else { + dino.push(dinosaurs[i].dinosaurId) // The else statment is doing the same as the code above but put dino. at index i ,dino.id inside the empty if the is statments above are false + } + }// 2nd if statment + } else if (dinosaurs[i].mya[0] >= mya && dinosaurs[i].mya[1] <= mya) { //finding range. checking to see if the dino at index i , mya at the first and second idex is greater than and equal to and less than or equal to mya + if (key) { + dino.push(dinosaurs[i][key]) + } else { + dino.push(dinosaurs[i].dinosaurId) + } + } + }//end of loop + return dino // returning dino after the for loop +}//end of function module.exports = { getLongestDinosaur, diff --git a/src/02-room-details.js b/src/02-room-details.js index a431888..858b4e3 100644 --- a/src/02-room-details.js +++ b/src/02-room-details.js @@ -25,7 +25,24 @@ const exampleRoomData = require("../data/rooms"); * getRoomByDinosaurName(dinosaurs, rooms, "Pterodactyl"); * //> "Dinosaur with name 'Pterodactyl' cannot be found." */ -function getRoomByDinosaurName(dinosaurs, rooms, dinosaurName) {} +function getRoomByDinosaurName(dinosaurs, rooms, dinosaurName) { + let dinoFound = false // accumulator + let dino = "" // setting dino to an emtpy array + for (let i = 0; i < dinosaurs.length; i++) { + if (dinosaurs[i].name === dinosaurName) { // if the dino name being looped, so dino at idex i equals DN + dinoFound = true // dino is found , no we change the value to true + dino = dinosaurs[i].dinosaurId // here im setting my empty string to dino. at index i . dinorasaurId. + } + } if (!dinoFound) { // i am creating a condition to check if the dino is not found so we can return the the correct erro message + return `Dinosaur with name '${dinosaurName}' cannot be found.` + } + for (let i = 0; i < rooms.length; i++) { + if (rooms[i].dinosaurs.includes(dino)) { // + return rooms[i].name + } + } + return `Dinosaur with name '${dinosaurName}' cannot be found in any rooms.` +} /** * getConnectedRoomNamesById() @@ -49,8 +66,41 @@ function getRoomByDinosaurName(dinosaurs, rooms, dinosaurName) {} "Kit Hopkins Education Wing" ] */ -function getConnectedRoomNamesById(rooms, id) {} +function getConnectedRoomNamesById(rooms, id) { + let found = false + let error = "" // creating an empty string + for (let i = 0; i < rooms.length; i++) { + if (rooms[i].roomId === id) { // checking to see if room at index i within room id is equal to id + found = true // now we are setting the variable found to true in order to create another if statment where found is not true + } + } + if (!found) { // here we are checking to see if found is false then return the following statment + return `Room with ID of '${id}' could not be found.` + } + let isConnectedRoom = [] // creating an emtpy array + for (let i = 0; i < rooms.length; i++) { + if (rooms[i].roomId === id){ + isConnectedRoom.push(rooms[i].connectsTo) // pushing room at index i in connectsTo into the empty arrat + } + } + isConnectedRoom = [].concat.apply([], isConnectedRoom) // using the concat method to merge the two arrays and using .apply to spread the the elements it found and putting it inside it's own index. + for (let i = 0; i < isConnectedRoom.length; i++) { + let roomFound = rooms.find(room => isConnectedRoom[i] === room.roomId) + if (!roomFound) { // checking if rooms are not found + return `Room with ID of '${isConnectedRoom[i]}' could not be found.` + } else { + isConnectedRoom[i] = roomFound + } + } + isConnectedRoom = isConnectedRoom.map(room => room.name) + return isConnectedRoom +} + +// let isRoomConnected = [].concat.apply([], isRoomConnected) +// if (!rooms.roomId === id) { +// return `Room with ID of '${id}' could not be found.` +// } module.exports = { getRoomByDinosaurName, getConnectedRoomNamesById, diff --git a/src/03-ticket-calculator.js b/src/03-ticket-calculator.js index 648d29a..dc2ed3c 100644 --- a/src/03-ticket-calculator.js +++ b/src/03-ticket-calculator.js @@ -54,8 +54,358 @@ const exampleTicketData = require("../data/tickets"); calculateTicketPrice(tickets, ticketInfo); //> "Entrant type 'kid' cannot be found." */ -function calculateTicketPrice(ticketData, ticketInfo) {} +function calculateTicketPrice(ticketData, ticketInfo) { + let totalPrice = 0; // accumalator to refence to later + ticketData[ticketInfo.ticketType]; // ticket data is an array so we are accessing the array first + if ( + ticketInfo.ticketType !== "general" && + ticketInfo.ticketType !== "membership" + ) { + // creating a condition to check for tickets types are not general or membership and then returning an error message + return `Ticket type '${ticketInfo.ticketType}' cannot be found.`; + // on the following line of code we are checking if the ticket entrant type is not an adult,child, or sr + } else if ( + ticketInfo.entrantType !== "child" && + ticketInfo.entrantType !== "adult" && + ticketInfo.entrantType !== "senior" + ) { + return `Entrant type '${ticketInfo.entrantType}' cannot be found.`; + // here we are checking if no extras are included and if the length is greater than 0 + } else if ( + !ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("education") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.length > 0 + ) { + return `Extra type '${ticketInfo.extras}' cannot be found.`; + } // the following is code is determing the base pay with no extra cost , general ticket + if ( + ticketInfo.ticketType === "general" && + ticketInfo.entrantType === "child" && + ticketInfo.extras.length === 0 + ) { + return ticketData.general.priceInCents.child; + } else if ( + ticketInfo.ticketType === "general" && + ticketInfo.entrantType === "adult" && + ticketInfo.extras.length === 0 + ) { + return ticketData.general.priceInCents.adult; + } else if ( + ticketInfo.ticketType === "general" && + ticketInfo.entrantType === "senior" && + ticketInfo.extras.length === 0 + ) + return ticketData.general.priceInCents.senior; + // the following code is checking the memebership base pay with no extra for each one + if ( + ticketInfo.ticketType === "membership" && + ticketInfo.entrantType === "child" && + ticketInfo.extras.length === 0 + ) { + return ticketData.membership.priceInCents.child; + } else if ( + ticketInfo.ticketType === "membership" && + ticketInfo.entrantType === "adult" && + ticketInfo.extras.length === 0 + ) { + return ticketData.membership.priceInCents.adult; + } else if ( + ticketInfo.ticketType === "membership" && + ticketInfo.entrantType === "senior" && + ticketInfo.extras.length === 0 + ) + return ticketData.membership.priceInCents.senior; + // the follwing code is checking the general base pay for child with the extras included. I used the . includes to check if the specific extra is include. I used the "!" not to still check for the extra but make sure its not includeded. I did this for the rest of the code. + if (ticketInfo.ticketType === "general") { + if ( + ticketInfo.entrantType === "child" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + !ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.child + + ticketData.extras.movie.priceInCents.child; + return totalPrice; + } else if ( + ticketInfo.entrantType === "child" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.child + + ticketData.extras.movie.priceInCents.child + + ticketData.extras.education.priceInCents.child; + return totalPrice; + } else if ( + ticketInfo.entrantType === "child" && + !ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.child + + ticketData.extras.terrace.priceInCents.child + + ticketData.extras.education.priceInCents.child; + return totalPrice; + } else if ( + ticketInfo.entrantType === "child" && + ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.child + + ticketData.extras.movie.priceInCents.child + + ticketData.extras.education.priceInCents.child + + ticketData.extras.terrace.priceInCents.child; + return totalPrice; + } + } // the follwing code is checking the general base pay for adult with the extras included. one by one by using the .includes meathod and ! + if (ticketInfo.ticketType === "general") { + if ( + ticketInfo.entrantType === "adult" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + !ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.adult + + ticketData.extras.movie.priceInCents.adult; + return totalPrice; + } else if ( + ticketInfo.entrantType === "adult" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.adult + + ticketData.extras.movie.priceInCents.adult + + ticketData.extras.education.priceInCents.adult; + return totalPrice; + } else if ( + ticketInfo.entrantType === "adult" && + !ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.adult + + ticketData.extras.terrace.priceInCents.adult + + ticketData.extras.education.priceInCents.adult; + return totalPrice; + } else if ( + ticketInfo.entrantType === "adult" && + ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.adult + + ticketData.extras.movie.priceInCents.adult + + ticketData.extras.education.priceInCents.adult + + ticketData.extras.terrace.priceInCents.adult; + return totalPrice; + } + } // the follwing code is checking the general base pay for sr with the extras included. one by one + if (ticketInfo.ticketType === "general") { + if ( + ticketInfo.entrantType === "senior" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + !ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.senior + + ticketData.extras.movie.priceInCents.senior; + return totalPrice; + } else if ( + ticketInfo.entrantType === "senior" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.senior + + ticketData.extras.movie.priceInCents.senior + + ticketData.extras.education.priceInCents.senior; + return totalPrice; + } else if ( + ticketInfo.entrantType === "senior" && + !ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.senior + + ticketData.extras.terrace.priceInCents.senior + + ticketData.extras.education.priceInCents.senior; + return totalPrice; + } else if ( + ticketInfo.entrantType === "senior" && + ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.general.priceInCents.senior + + ticketData.extras.movie.priceInCents.senior + + ticketData.extras.education.priceInCents.senior + + ticketData.extras.terrace.priceInCents.senior; + return totalPrice; + } + } // one the rest of the code we are doing the same thing as the top but with the memnership pay. we are checking each one by ticket type and each extra one included. + if (ticketInfo.ticketType === "membership") { + if ( + ticketInfo.entrantType === "child" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + !ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.child + + ticketData.extras.movie.priceInCents.child; + return totalPrice; + } else if ( + ticketInfo.entrantType === "child" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.child + + ticketData.extras.movie.priceInCents.child + + ticketData.extras.education.priceInCents.child; + return totalPrice; + } else if ( + ticketInfo.entrantType === "child" && + !ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.child + + ticketData.extras.terrace.priceInCents.child + + ticketData.extras.education.priceInCents.child; + return totalPrice; + } else if ( + ticketInfo.entrantType === "child" && + ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.child + + ticketData.extras.movie.priceInCents.child + + ticketData.extras.education.priceInCents.child + + ticketData.extras.terrace.priceInCents.child; + return totalPrice; + } + } + if (ticketInfo.ticketType === "membership") { + if ( + ticketInfo.entrantType === "adult" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + !ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.adult + + ticketData.extras.movie.priceInCents.adult; + return totalPrice; + } else if ( + ticketInfo.entrantType === "adult" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.adult + + ticketData.extras.movie.priceInCents.adult + + ticketData.extras.education.priceInCents.adult; + return totalPrice; + } else if ( + ticketInfo.entrantType === "adult" && + !ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.adult + + ticketData.extras.terrace.priceInCents.adult + + ticketData.extras.education.priceInCents.adult; + return totalPrice; + } else if ( + ticketInfo.entrantType === "adult" && + ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.adult + + ticketData.extras.movie.priceInCents.adult + + ticketData.extras.education.priceInCents.adult + + ticketData.extras.terrace.priceInCents.adult; + return totalPrice; + } + } + if (ticketInfo.ticketType === "membership") { + if ( + ticketInfo.entrantType === "senior" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + !ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.senior + + ticketData.extras.movie.priceInCents.senior; + return totalPrice; + } else if ( + ticketInfo.entrantType === "senior" && + ticketInfo.extras.includes("movie") && + !ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.senior + + ticketData.extras.movie.priceInCents.senior + + ticketData.extras.education.priceInCents.senior; + return totalPrice; + } else if ( + ticketInfo.entrantType === "senior" && + !ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.senior + + ticketData.extras.terrace.priceInCents.senior + + ticketData.extras.education.priceInCents.senior; + return totalPrice; + } else if ( + ticketInfo.entrantType === "senior" && + ticketInfo.extras.includes("movie") && + ticketInfo.extras.includes("terrace") && + ticketInfo.extras.includes("education") + ) { + totalPrice = + ticketData.membership.priceInCents.senior + + ticketData.extras.movie.priceInCents.senior + + ticketData.extras.education.priceInCents.senior + + ticketData.extras.terrace.priceInCents.senior; + return totalPrice; + } + } +} + +// if (ticketInfo.extras.includes('education')) { +// return ticketData.extras.education.priceInCents.child +// } else { +// return(ticketData.extras.movies.priceInCents.chold + ticketData.general.priceInCents.child) /** * purchaseTickets() * --------------------- @@ -109,7 +459,16 @@ function calculateTicketPrice(ticketData, ticketInfo) {} purchaseTickets(tickets, purchases); //> "Ticket type 'discount' cannot be found." */ -function purchaseTickets(ticketData, purchases) {} +function purchaseTickets(ticketData, purchases) { + let finalPrice = 0 + let final = [] + let finalReceipt = 'Thank you for visiting the Dinosaur Museum!\n-------------------------------------------' // + for (let i = 0; i < purchases.length; i++) { + if(!(ticketData.hasOwnProperty(purchases[i].ticketType))) { + return `Ticket type '${purchases[i].ticketType}' cannot be found.` + } + } +} // Do not change anything below this line. module.exports = {