2020.05.30

  • Solved it!
  • The key is using the recursive function. I diagrammed what was happening with the recursive function, and understood why it worked. The function decreases the number, calls itself, and keeps going until the logic check. Once it hits that point it returns the array up one level. Then the process starts of pushing the current number to the array and returning that up unti you have your list.
  • I think the whole function wasn't working yesterday because the else statement wasn't returning anything yet, making the recursive function just stop at the bottom.
function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum){
    return []
  } else {
      const countArray = rangeOfNumbers(startNum, endNum-1);
      countArray.push(endNum);
      console.log(countArray);
      return countArray;
    }
}
rangeOfNumbers(1, 5)
/*
create a list of numbers given two numbers
  if current number is less than end number
    stop
  else decrease current number
    push to array
    return current number
*/

2020.05.29

  • Trying to work through more recursive logic problems to create a list based on two starting numbers. Harder when there's an additional variable.
  • Stuck on the else, statement, since I'm now calling a separate function within the inital rangeOfNumbers function
function rangeOfNumbers(startNum, endNum) {
  if (endNum < startNum){
    return []
  } else {
      console.log("run countUp");
      // countup(endNum);
      function countup(endNum){
        console.log("run countUp function");
        var countArray = countup(endNum-1);
        console.log(countArray);        
      }
    }
}
rangeOfNumbers(5, 10)


// function countup(n) {
//   if (n < 1) {
//     return [];
//   } else {
//     const countArray = countup(n - 1);
//     countArray.push(n);
//     return countArray;
//   }
// }
// console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]
freeCodeCamp.org
Learn to code. Build projects. Earn certifications.Since 2015, 40,000 graduates have gotten jobs at tech companies including Google, Apple, Amazon, and Microsoft.