Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Did My Best! :( #241

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions src/01-dinosaur-facts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,31 @@ const exampleDinosaurData = require("../data/dinosaurs");
* getLongestDinosaur(dinosaurs);
* //> { Brachiosaurus: 98.43 }
*/
function getLongestDinosaur(dinosaurs) {}
function getLongestDinosaur(dinosaurs) {
if (dinosaurs.length === 0) {
return {} // if there is no dinosaurs, return an empty object
}

let longestDino = dinosaurs[0] // equivalent to first index


for (let i = 1; i < dinosaurs.length; i++) {

if (dinosaurs[i].lengthInMeters > longestDino.lengthInMeters) {
console.log(true)
longestDino = dinosaurs[i] // setting a longest to be equal to dinosaurs ar index i

}

} // end loop

console.log(longestDino)
let finalDino = {} // created an empty object to be able to return value
finalDino[longestDino.name] = longestDino.lengthInMeters * 3.281 // setting final dino to longest dino (converting lengthinMeters to longest feet) to return the finaldino
return finalDino

} // end of function


/**
* getDinosaurDescription()
Expand All @@ -44,7 +68,16 @@ 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++) { // for loop was declared to go through the full length of the array of dinosaurs (all objects)
if (dinosaurs[i].dinosaurId === id) // checking if the dino index 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 this format, providing info using dot notation
}
return `A dinosaur with an ID of '${id}' cannot be found.` // if dino index i != the id we return to the statement finds the id.

} // end of function


/**
* getDinosaursAliveMya()
Expand All @@ -71,7 +104,31 @@ function getDinosaurDescription(dinosaurs, id) {}
* getDinosaursAliveMya(dinosaurs, 65, "unknown-key");
* //> ["WHQcpcOj0G"]
*/
function getDinosaursAliveMya(dinosaurs, mya, key) {}
function getDinosaursAliveMya(dinosaurs, mya, key) {
let dino = [] // create an empty array
for (let i = 0; i < dinosaurs.length; i++) { // for loop
if (dinosaurs[i].mya.length === 1) { //checking for the value of 1
if (dinosaurs[i].mya[0] === mya || dinosaurs[i].mya - 1 === mya ) {
if (key) {
dino.push(dinosaurs[i][key]) //push the index of the key
} else {
dino.push(dinosaurs[i].dinosaurId) //pushing into the array to get the dino Ids
}
}


} else if (dinosaurs[i].mya[0] >= mya && dinosaurs[i].mya[1] <= mya) { //comparing to each other (range)
if (key) {
dino.push(dinosaurs[i][key]) //push the index of the key
} else {
dino.push(dinosaurs[i].dinosaurId) //pushing into the array to get the dino Ids

}
}

} // end of loop
return dino
} //end of function

module.exports = {
getLongestDinosaur,
Expand Down
38 changes: 36 additions & 2 deletions src/02-room-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
for (let i = 0; i < dinosaurs.length; i++) {
if (dinosaurs[i].name === dinosaurName) { //access the dinosaurs inside of the room [i]
dinoFound = true
dino = dinosaurs[i].dinosaurId
}
}
if (!dinoFound) {
return `Dinosaur with name '${dinosaurName}' cannot be found.`
} // outside of the for loop
for (let i = 0; i < rooms.length; i++) {
if (rooms[i].dinosaurs.includes(dino)) {
return rooms[i].name //returns name of room if the dino is found
}
} return `Dinosaur with name '${dinosaurName}' cannot be found in any rooms.`
}

/**
* getConnectedRoomNamesById()
Expand All @@ -49,7 +66,24 @@ function getRoomByDinosaurName(dinosaurs, rooms, dinosaurName) {}
"Kit Hopkins Education Wing"
]
*/
function getConnectedRoomNamesById(rooms, id) {}
function getConnectedRoomNamesById(rooms, id) {
let found = false //declare variable
let errorMessage = "" //declare variable
for (let i = 0; i < rooms.length; i++) // start of for loop
if (rooms[i].roomID === id){ // looping through the index of rooms to match/see if it's true to ID
found = true

} //end of for loop

}
let isConnectedRoom = [] //set isConnectedRoom as an empty array
for (let i = 0; i < rooms.length; i++) { // for loop begins
if (rooms[i].roomId === id) {
isConnectedRoom = rooms[i].push(connectsTo) //property, has the name of the rooms
}
}return isConnectedRoom
if (!found) {
}return `Room with ID of '${id}' could not be found.` //end of function

module.exports = {
getRoomByDinosaurName,
Expand Down
163 changes: 160 additions & 3 deletions src/03-ticket-calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,129 @@ 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 - be able to get priceInCents
ticketData[ticketInfo.ticketType] //this is referencing an array first

if (ticketInfo.ticketType !== "general" && ticketInfo.ticketType !== "membership") { //no valid ticket type is present
return `Ticket type '${ticketInfo.ticketType}' cannot be found.`
} else if (ticketInfo.entrantType !== "child" && ticketInfo.entrantType !== "adult" && ticketInfo.entrantType !== "senior") {
return `Entrant type '${ticketInfo.entrantType}' cannot be found.`
} else if (!ticketInfo.extras.includes("movie") && !ticketInfo.extras.includes("education") && !ticketInfo.extras.includes("terrace") && ticketInfo.extras.length > 0) { //check if that array is empty
return `Extra type '${ticketInfo.extras}' cannot be found.`

}
else if (ticketInfo.ticketType === "general" && ticketInfo.entrantType === "child" && // created a strict equality operator for the elements in the array for ticketType & entrantType
ticketInfo.extras.length === 0) {
return ticketData.general.priceInCents.child;
} else if (ticketInfo.ticketType === "general" && ticketInfo.entrantType === "adult" && ticketInfo.extras.length === 0) { // boolean for ticketType and entrant
return ticketData.general.priceInCents.adult;
} else if (ticketInfo.ticketType === "general" && ticketInfo.entrantType === "senior" && ticketInfo.extras.length === 0) {
return ticketData.general.priceInCents.senior;
}
else {
if (ticketInfo.ticketType === "membership" && ticketInfo.entrantType === "child" && // boolean created for different ticketType, entrantType remains the same
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;
}
}

if (ticketInfo.ticketType === "general") {
if (ticketInfo.entrantType === "child" && ticketInfo.extras.includes('movie') && !ticketInfo.extras.includes('terrace') && !ticketInfo.extras.includes('education')) { //extras are not valid in this array, checking for the entrantType
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 //result of total price
} else if (ticketInfo.entrantType === "child" && !ticketInfo.extras.includes('movie') && ticketInfo.extras.includes('terrace') && ticketInfo.extras.includes('education')) { //extras are valid in this array
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.terrace.priceInCents.child + ticketData.extras.education.priceInCents.child + ticketData.extras.movie.priceInCents.child
return totalPrice
}
}
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.terrace.priceInCents.adult + ticketData.extras.education.priceInCents.adult + ticketData.extras.movie.priceInCents.adult
return totalPrice
}
}
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.terrace.priceInCents.senior + ticketData.extras.education.priceInCents.senior + ticketData.extras.movie.priceInCents.senior
return totalPrice
}
}
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.terrace.priceInCents.child + ticketData.extras.education.priceInCents.child + ticketData.extras.movie.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.terrace.priceInCents.adult + ticketData.extras.education.priceInCents.adult + ticketData.extras.movie.priceInCents.adult
return totalPrice
}
}
if (ticketInfo.ticketType === "membership") {
if (ticketInfo.entrantType === "senior" && ticketInfo.extras.includes('movie') && !ticketInfo.extras.includes('terrace') && !ticketInfo.extras.includes('education')) { //extras not valid in array
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.terrace.priceInCents.senior + ticketData.extras.education.priceInCents.senior + ticketData.extras.movie.priceInCents.senior
return totalPrice //final price
}
}
} //end of function


/**
* purchaseTickets()
Expand Down Expand Up @@ -97,7 +219,7 @@ function calculateTicketPrice(ticketData, ticketInfo) {}
];
purchaseTickets(tickets, purchases);
//> "Thank you for visiting the Dinosaur Museum!\n-------------------------------------------\nAdult General Admission: $50.00 (Movie Access, Terrace Access)\nSenior General Admission: $35.00 (Terrace Access)\nChild General Admission: $45.00 (Education Access, Movie Access, Terrace Access)\nChild General Admission: $45.00 (Education Access, Movie Access, Terrace Access)\n-------------------------------------------\nTOTAL: $175.00"

* EXAMPLE:
const purchases = [
{
Expand All @@ -109,7 +231,42 @@ 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 receipt = 'Thank you for visiting the Dinosaur Museum!\n-------------------------------------------' // start the receipt
for(let i = 0; i< purchases.length; i++){
let extra1 = []
if(!(ticketData.hasOwnProperty(purchases[i].ticketType))){//error handling
//Checking if ticket data does not have the correct purchase type.
return `Ticket type '${purchases[i].ticketType}' cannot be found.`
}
if(!(ticketData[purchases[i].ticketType].priceInCents.hasOwnProperty(purchases[i].entrantType))){// error handling. Checking if the entrant type is incorrect
return `Entrant type '${purchases[i].entrantType}' cannot be found.`
}
let price = calculateTicketPrice(ticketData, purchases[i])/100 //calculate price of ticket
if(typeof price === 'string'){
return price
}
let ticketType = purchases[i].ticketType; //type of ticket
let entrantType = purchases[i].entrantType; // entrant type
receipt += `\n${entrantType[0].toUpperCase()}${entrantType.slice(1)} ${ticketType[0].toUpperCase()}${ticketType.slice(1)} Admission: $${price.toFixed(2)}`// formatting receipt
for (let j = 0; j < purchases[i].extras.length; j++){//loop for extras
if(ticketData.extras.hasOwnProperty(purchases[i].extras[j])){ //seeing if object has specified property
extra1.push(ticketData.extras[purchases[i].extras[j]].description)//pushing the description in array if extra exists
} else {
return `Extra type '${purchases[i].extras[j]}' cannot be found.`
}
}// ending [j] for loop
if(extra1.length !== 0){
receipt += ` (${extra1.join(', ')})`//if no extras
}
finalPrice += price
}//ending [i] for loop
receipt += `\n-------------------------------------------\nTOTAL: $${finalPrice.toFixed(2)}`// last costs to receipt
return receipt
}// end of function



// Do not change anything below this line.
module.exports = {
Expand Down