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

Final Dino Project #245

Open
wants to merge 4 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
46 changes: 43 additions & 3 deletions src/01-dinosaur-facts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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,
Expand Down
54 changes: 52 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 = "" // 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()
Expand All @@ -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,
Expand Down
Loading