Skip to content

Instantly share code, notes, and snippets.

@CastonPursuit
Last active February 6, 2024 18:44
Show Gist options
  • Save CastonPursuit/4b99115fc763f9363ec623baa98d1f0a to your computer and use it in GitHub Desktop.
Save CastonPursuit/4b99115fc763f9363ec623baa98d1f0a to your computer and use it in GitHub Desktop.

Review Notes For facts.js

Solving the getLongestDinosaur Function

Step 1: Set Up the Problem

  • 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.

Step 2: Begin Iteration

  • Iterate over the array of dinosaur objects to access each dinosaur object individually.

Step 3: Access Object Properties

  • Access necessary properties for each dinosaur:
    • The dinosaur's name (`name` key).
    • The dinosaur's length in meters (`lengthInMeters` key).

Step 4: Compare and Identify

  • 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.

Step 5: Perform Calculations

  • Convert the length of the dinosaur identified as the longest from meters to feet using the conversion factor (3.281).

Step 6: Update the Result Object

  • Update a result object with the longest dinosaur's name as the key and its converted length in feet as the value.

Step 7: Complete the Iteration

  • Continue until all dinosaurs have been processed.

Step 8: Return the Result

  • Return the result object containing the longest dinosaur's name and length in feet.

Code

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

Steps to Solve

1. Loop Through the List

  • Start at the beginning of the list and look at each item one by one.

2. Examine Each Dinosaur

  • Look at the details of the dinosaur you are currently checking.

3. Find the Right Dinosaur

  • Check if the unique code you have matches the code in the dinosaur's details.

4. Create the Description

  • When you find the dinosaur with the matching code, prepare its detailed description.

5. Format the Description

  • Make sure the description is easy to read, with the dinosaur's name, a pronunciation guide, some interesting facts, and when it lived.

6. Return the Description

  • Give back the full description so it can be read and understood.

7. Handle Unmatched Cases

  • 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.

Code

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

Steps to Solve

1. Prepare the Result List

  • Start with an empty list to store the results.

2. Go Through Each Dinosaur

  • Look at the details of every dinosaur in the provided list one after another.

3. Check Dinosaur's Time Period

  • 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.

4. Decide What to Collect

  • 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.

5. Gather the Information

  • Add the prepared piece of information (detail or ID) to the results list.

6. Return the Results

  • After checking all dinosaurs, return the list of collected information.

Code

/// 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment