Solving the getLongestDinosaur
Function
- Understand the data structure: an array of objects, each representing a dinosaur with various properties.
- Identify the goal: Find the dinosaur with the longest length and convert that length from meters to feet.
- Iterate over the array of dinosaur objects to access each dinosaur object individually.
- Access necessary properties for each dinosaur:
- The dinosaur's name (`name` key).
- The dinosaur's length in meters (`lengthInMeters` key).
- Compare the current dinosaur's length with a stored value for the longest dinosaur.
- Update the stored value if the current dinosaur's length is greater.
- Convert the length of the dinosaur identified as the longest from meters to feet using the conversion factor (3.281).
- Update a result object with the longest dinosaur's name as the key and its converted length in feet as the value.
- Continue until all dinosaurs have been processed.
- Return the result object containing the longest dinosaur's name and length in feet.
function getLongestDinosaur(dinosaurs) {
let longestLength = 0;
let finalObj = {};
for(const {name, lengthInMeters} of dinosaurs) {
if(lengthInMeters > longestLength){
longestLength = lengthInMeters;
finalObj = {[nameVal]: lengthInMeters * 3.281};
}
}
return finalObj;
}
//// Native array method
function getLongestDinosaur(dinosaurs) {
const finalObj = dinosaurs.reduce((longest, current) => {
if (current.lengthInMeters > longest.lengthInMeters) {
return {
name: current.name,
lengthInMeters: current.lengthInMeters,
};
}
return longest;
}, { name: "", lengthInMeters: 0 });
return {
[finalObj.name]: finalObj.lengthInMeters * 3.281,
};
}
Solving the getDinosaurDescription
Function
- Start at the beginning of the list and look at each item one by one.
- Look at the details of the dinosaur you are currently checking.
- Check if the unique code you have matches the code in the dinosaur's details.
- When you find the dinosaur with the matching code, prepare its detailed description.
- Make sure the description is easy to read, with the dinosaur's name, a pronunciation guide, some interesting facts, and when it lived.
- Give back the full description so it can be read and understood.
- If you reach the end of the list and haven't found the dinosaur, let the reader know that the code didn't match any dinosaurs in the list.
function getDinosaurDescription(dinosaurs, id) {
for(let obj of dinosaurs){
const {name, dinosaurId, info, pronunciation, period, mya } = obj;
if(id === dinosaurId){
return`${name} (${pronunciation})\n${info} It lived in the ${period} period, over ${mya[mya.length-1]} million years ago.`;
}
}
return `A dinosaur with an ID of '${id}' cannot be found.`
}
Solving the getDinosaursAliveMya
Function
- Start with an empty list to store the results.
- Look at the details of every dinosaur in the provided list one after another.
- For each dinosaur, see if their time on Earth matches the specified 'mya'.
- Include a one-year leeway if the dinosaur has a single 'mya' value.
- If a specific detail key is given and it exists for the dinosaur, prepare to collect that detail.
- If no key is given or the key doesn't exist, prepare to collect the dinosaur's ID.
- Add the prepared piece of information (detail or ID) to the results list.
- After checking all dinosaurs, return the list of collected information.
/// My solution
function getDinosaursAliveMya(dinosaurs, mya, key) {
let arr = [];
for (let dino of dinosaurs) {
let oneDateBool = dino.mya.length === 1 && (dino.mya[0] === mya || dino.mya[0] - 1 === mya);
let twoDatesBool = dino.mya.length === 2 && mya >= dino.mya[1] && mya <= dino.mya[0];
if (oneDateBool) {
pushToArray(arr, key, dino)
}
else if (twoDatesBool) {
pushToArray(arr, key, dino)
}
}
return arr;
}
/// Helper Function
function pushToArray(arr, key, dino){
if (!key || !(key in dino)) {
arr.push(dino.dinosaurId);
}
else {
arr.push(dino[key]);
}
}
/// Alternative solution:
function getDinosaursAliveMya(dinosaurs, mya, key) {
let result = []; // Initialize an empty array to store the results
for (let i = 0; i < dinosaurs.length; i++) {
let dino = dinosaurs[i];
let myaRange = dino.mya;
let isInTimeRange = false;
// Check if the dinosaur lived during the single or range mya value
if (myaRange.length === 1) {
// Allows for the mya value to be equal or one less
isInTimeRange = mya === myaRange[0] || mya === myaRange[0] - 1;
} else if (myaRange.length === 2) {
// Checks if the mya falls within the range
isInTimeRange = mya >= myaRange[1] && mya <= myaRange[0];
}
if (isInTimeRange) {
if (key && key in dino) {
result.push(dino[key]); // Push the value of the specified key
} else {
result.push(dino.dinosaurId); // Default to dinosaur ID
}
}
}
return result;
}