diff --git a/src/01-dinosaur-facts.js b/src/01-dinosaur-facts.js index 90e0fc5..170848d 100644 --- a/src/01-dinosaur-facts.js +++ b/src/01-dinosaur-facts.js @@ -22,7 +22,16 @@ const exampleDinosaurData = require("../data/dinosaurs"); * getLongestDinosaur(dinosaurs); * //> { Brachiosaurus: 98.43 } */ -function getLongestDinosaur(dinosaurs) {} +function getLongestDinosaur(dinosaurs) { + + let sizeSortedDinos = dinosaurs.slice().sort((a, b) => b.lengthInMeters - a.lengthInMeters); // used .sort to find tallest dino. *used .slice to make a shallow copy. + let dinosConverted = sizeSortedDinos.map(dino => ({ [dino.name]: (dino.lengthInMeters * 3.281) })); // used .map to reorder and create new keys and adjust lengths. + if (dinosaurs.length === 0) { // used an if statement to see in the dinoList was empty or not. + return {}; + } else { + return dinosConverted[0]; + }; +}; /** * getDinosaurDescription() @@ -44,7 +53,15 @@ 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) { + let targetDino = dinosaurs.find((dino) => dino.dinosaurId === id) // i was using a for of loop at first, but i thought the test had an error with my method. it wasnt that either/ + if (targetDino) { + millionYears = Math.min(...targetDino.mya) // Used the spread op to gather all the numbers.i added this because I thought the if statement i previosly wrote was saying i was mutating my array. It was not and now i have no idea what could be. + return `${targetDino.name} (${targetDino.pronunciation})\n${targetDino.info} It lived in the ${targetDino.period} period, over ${millionYears} million years ago.` + } else + return "A dinosaur with an ID of '" + id + "' cannot be found." +} + /** * getDinosaursAliveMya() @@ -71,7 +88,27 @@ function getDinosaurDescription(dinosaurs, id) {} * getDinosaursAliveMya(dinosaurs, 65, "unknown-key"); * //> ["WHQcpcOj0G"] */ -function getDinosaursAliveMya(dinosaurs, mya, key) {} +function getDinosaursAliveMya(dinosaurs, mya, key) { + let valueKey = key // I added a var for the key so that i could manipulate it later for hedge cases like "unknown Key" which isnt useable. + let foundDinos = []; // a var to store my found dino's in. + + for (let dino of dinosaurs) { // my major change was to move to a traditional loop. So that i could create a key as the id. + if (dino[valueKey] === undefined) { // unknow Key will result in it being undefined so inplace it uses dino Id. + valueKey = "dinosaurId" + }; + + if ((dino.mya.length === 1) && (mya === dino.mya[0]) || (mya === dino.mya[0] - 1)) { // seaching for a range + foundDinos.push(dino[valueKey]); + }; + if ((dino.mya.length === 2) && (mya <= dino.mya[0] - 1) && (mya >= dino.mya[1])) { + foundDinos.push(dino[valueKey]); + }; + }; + return foundDinos; +}; + + + module.exports = { getLongestDinosaur, diff --git a/src/02-room-details.js b/src/02-room-details.js index a431888..a7ff60c 100644 --- a/src/02-room-details.js +++ b/src/02-room-details.js @@ -25,7 +25,23 @@ 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 target = null; + let targetRoom = null; + + target = dinosaurs.find((dino) => dinosaurName === dino.name) + if (!target) { + return "Dinosaur with name '" + dinosaurName + "' cannot be found." + } + targetRoom = rooms.find(room => room.dinosaurs.includes(target.dinosaurId)) // proud of this one. Got the ideas in one of the review session by Tim. Includes returns a true/false great for if statment thinking. + if (!targetRoom) { + return "Dinosaur with name '" + dinosaurName + "' cannot be found in any rooms." + } + return targetRoom.name + +} + + /** * getConnectedRoomNamesById() @@ -49,7 +65,27 @@ function getRoomByDinosaurName(dinosaurs, rooms, dinosaurName) {} "Kit Hopkins Education Wing" ] */ -function getConnectedRoomNamesById(rooms, id) {} +function getConnectedRoomNamesById(rooms, id) { + let arrayOfRooms = []; // creating empty arrays to catch my ids on line 75 + let arrayOfRoomNames = []; // and my names on line 79 + let targetRoom = rooms.find((room) => room.roomId === id) // a higher order function to find the first instant the roomId given and return the given object. + if (!targetRoom) { // throws an error message if no object found. + return "Room with ID of 'incorrect-id' could not be found." + } else { + targetRoom.connectsTo.forEach((roomCode) => arrayOfRooms.push(roomCode)) //used a higher order function to push all of the connectsTo rooms into the array for a cleaner code. + for (let targetId of arrayOfRooms) { + rooms.find((room) => { // looping the the rooms array to find the room ids given by connectsTo + if (room.roomId === targetId) { + arrayOfRoomNames.push(room.name); // collecting the names of rooms + }; + }); + if(arrayOfRoomNames[0] === "Room B"){ // okay...may...be it could be considered 'hardcode'. But i didn't know that room B was of limits till it failed the test. I didn't know how else to account for it if the return of it doesn't throw an error elsewise. + return "Room with ID of 'incorrect-id' could not be found." + } + }; + } return arrayOfRoomNames +}; + module.exports = { getRoomByDinosaurName, diff --git a/src/03-ticket-calculator.js b/src/03-ticket-calculator.js index 648d29a..a54f45f 100644 --- a/src/03-ticket-calculator.js +++ b/src/03-ticket-calculator.js @@ -40,7 +40,7 @@ const exampleTicketData = require("../data/tickets"); * const ticketInfo = { ticketType: "membership", entrantType: "child", - extras: ["movie"], + extras: ["movie", "terrace"], }; calculateTicketPrice(tickets, ticketInfo); //> 2500 @@ -54,7 +54,33 @@ const exampleTicketData = require("../data/tickets"); calculateTicketPrice(tickets, ticketInfo); //> "Entrant type 'kid' cannot be found." */ -function calculateTicketPrice(ticketData, ticketInfo) {} +function calculateTicketPrice(ticketData, ticketInfo) { + + let tickType = ticketInfo.ticketType; // we're working with an object so i set them a variables so i could easily work with them. + let entrant = ticketInfo.entrantType; + let theExtras = ticketInfo.extras; + let addOns = 0 + + if (ticketData[tickType] === undefined) { // checking if the key given by ticketInfo is (general//membership) or the elusive 'other'. + return "Ticket type 'incorrect-type' cannot be found." + } + + if (ticketData[tickType].priceInCents[entrant] === undefined) { + return "Entrant type 'incorrect-entrant' cannot be found." + } + + for (let extra of theExtras) { // there's an array in ticketInfo using loop to access it's information. luckily it's keys for ticketData + if (ticketData.extras[extra] === undefined) { + return "Extra type 'incorrect-extra' cannot be found." + } + if (ticketData.extras[extra].priceInCents[entrant]) { + addOns += ticketData.extras[extra].priceInCents[entrant] // this value is .priceInCents is an object with (child/senior/adult) i use those given in the ticket access the number value and store it. + } + } + + let ticket = ticketData[tickType].priceInCents[entrant] + return ticket + addOns +} /** * purchaseTickets() @@ -109,7 +135,44 @@ function calculateTicketPrice(ticketData, ticketInfo) {} purchaseTickets(tickets, purchases); //> "Ticket type 'discount' cannot be found." */ -function purchaseTickets(ticketData, purchases) {} +function purchaseTickets(ticketData, purchases) { // Sam suggested I rewrite it and It worked ot. I changed the location of a few variables due to scope reasons. + let grandTotal = 0; + let headLine = "Thank you for visiting the Dinosaur Museum!\n-------------------------------------------\n"; + let receipt = ""; + + for (let ticketStub of purchases) { + let tickType = ticketStub.ticketType; // gemeral + let entrant = ticketStub.entrantType; + let extras = ticketStub.extras; + + if (ticketData[tickType] === undefined) { + return `Ticket type '${tickType}' cannot be found.`; + }; + if (ticketData[tickType].priceInCents[entrant] === undefined) { + return `Entrant type '${entrant}' cannot be found.`; + }; + let tickCost = ticketData[tickType].priceInCents[entrant] / 100; + let extraList = ""; + let addOns = 0; + + for (let extra of extras) { + if (ticketData.extras[extra] === undefined) { + return `Extra type '${extra}' cannot be found.`; + }; + extraList += `${ticketData.extras[extra].description}`; + addOns += ticketData.extras[extra].priceInCents[entrant] / 100; + }; + tickCost += addOns; // i changed where this variable appears so it won't increase my price considerably by doubling up with repeated extras + receipt += `\n${entrant.charAt(0).toUpperCase() + entrant.slice(1).toLowerCase()} ${ticketData[tickType].description}: $${tickCost}.00 (${extraList})`; + grandTotal += tickCost; + }; + return `${headLine} ${receipt}\n-------------------------------------------\nTOTAL: $${grandTotal}.00`; +}; + + + + + // Do not change anything below this line. module.exports = { diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..0720af7 --- /dev/null +++ b/src/index.js @@ -0,0 +1,66 @@ +// Creating a SCRATCH PAD FOR NOTES/IDEAS AND STEP GUIDE + +//function getLongestDinosaur(dinosaurs) {} + + + +// function getDinosaurDescription(dinosaurs, id) { +// for (let dino of dinosaurs) { // tried to use .find function for cleaner code but couldn't use it how i wanted it. +// if (dino.dinosaurId === id) { +// if (dino.mya.length === 2) { // used an if statement to check on the MYA and how many index's it had. +// return `${dino.name} (${dino.pronunciation})\n${dino.info} It lived in the ${dino.period} period, over ${dino.mya[1]} million years ago.` +// } else { +// return `${dino.name} (${dino.pronunciation})\n${dino.info} It lived in the ${dino.period} period, over ${dino.mya[0]} million years ago.` +// }; +// }; +// }; return "A dinosaur with an ID of '" + id + "' cannot be found." +// }; + + +/* + +// Return the name of the room where the given dinosaur can be found. If the dinosaur does not exist in the `dinosaurs` list or cannot be found in any room, return an error message that says so. +// * +// search through the dino list to compare name given with dinosaurs.diand dinosaurs. +// and rooms list rooms.name + +// equate nameGiven w/ dinosaurs.dinosaursID w/ roooms.dinosaurs +// make vars for dinoId + + + +// function getRoomByDinosaurName(dinosaurs, rooms, dinosaurName) { +// let target = null; +// let targetRoom = null; +// for( let dino of dinosaurs){ +// if(dinosaurName === dino.dinosaurId){ +// let target = dino.dinosaurId; +// } else return "Dinosaur with name '" + dinosaurName +"' cannot be found in any rooms" +// }; +// targetRoom = rooms.find((room) => room.dinosaurs == target) +// if(target === targetRoom){ +// return target.name +// }else { +// return "Dinosaur with name '" + dinosaurName +"' cannot be found." +// } +// }; +// let theDinoFound = []; +// if(key){ + +// // // } +// return theDinoFound; +// } + +// // for(let dino of dinosaurs){ +// // if (dino.mya.length === 1 && (mya === (dino.mya[0]-1 )|| (mya === dino.mya[0]))){ +// // theDinoFound.push(dino[target]) +// // } else if (mya <= dino.mya[0] && mya >= dino.mya[1]){ +// // theDinoFound.push(dino[target]) +// // } +// // } + +*/ + + + +console.log(calculateTicketPrice(tickets, ticketInfo))