Skip to content

Instantly share code, notes, and snippets.

@yclim95
Last active December 12, 2020 11:29
Show Gist options
  • Select an option

  • Save yclim95/b1b056f0dbdc26bb0df59ba3c296c7a6 to your computer and use it in GitHub Desktop.

Select an option

Save yclim95/b1b056f0dbdc26bb0df59ba3c296c7a6 to your computer and use it in GitHub Desktop.
Debugging: FreeCodeCamp

Debugging

1. Debugging: Use the JavaScript Console to Check the Value of a Variable

let a = 5;
let b = 1;
a++;
// Only change code below this line
console.log(a); //Debug purpose

let sumAB = a + b;
console.log(sumAB);

2. Debugging: Understanding the Differences between the freeCodeCamp and Browser Console

// Open your browser console.
let output = "Get this to log once in the freeCodeCamp console and twice in the browser console";
// Use console.log() to print the output variable.
console.log(output);
// Run the tests to see the difference between the two consoles.

// Now, add console.clear() before your console.log() to clear the browser console, and pass the tests.
console.clear();
console.log(output);

3. Debugging: Use typeof to Check the Type of a Variable

let seven = 7;
let three = "3";
console.log(seven + three);
// Only change code below this line
console.log(typeof seven);
console.log(typeof three);

4. Debugging: Catch Misspelled Variable and Function Names

let receivables = 10;
let payables = 8;
let netWorkingCapital = recievables - payable;
console.log(`Net working capital is: ${netWorkingCapital}`);

5. Debugging: Catch Unclosed Parentheses, Brackets, Braces and Quotes

let myArray = [1, 2, 3;
let arraySum = myArray.reduce((previous, current =>  previous + current);
console.log(`Sum of array values is: ${arraySum}`);

6. Debugging: Catch Mixed Usage of Single and Double Quotes

let innerHtml = "<p>Click here to <a href='#Home'>return home</a></p>";
console.log(innerHtml);

7. Debugging: Catch Use of Assignment Operator Instead of Equality Operator

let x = 7;
let y = 9;
let result = "to come";
/*
if(x = y) {
  result = "Equal!";
} else {
  result = "Not equal!";
}
*/
if(x == y) {
  result = "Equal!";
} else {
  result = "Not equal!";
}

console.log(result);

8. Debugging: Catch Missing Open and Closing Parenthesis After a Function Call

function getNine() {
  let x = 6;
  let y = 3;
  return x + y;
}

// let result = getNine;
let result = getNine();
console.log(result);

9. Debugging: Catch Arguments Passed in the Wrong Order When Calling a Function

function raiseToPower(b, e) {
  return Math.pow(b, e);
}

let base = 2;
let exp = 3;
// let power = raiseToPower(exp, base);
let power = raiseToPower(base, exp);
console.log(power);

10. Debugging: Catch Off By One Errors When Using Indexing

function countToFive() {
  let firstFive = "12345";
  let len = firstFive.length;
  // Only change code below this line
  /*
  for (let i = 1; i <= len; i++) {
    console.log(firstFive[i]);
  }
  */
  for (let i = 0; i < len; i++) {
    console.log(firstFive[i]);
  }
}

countToFive();

11. Debugging: Use Caution When Reinitializing Variables Inside a Loop

function zeroArray(m, n) {
  // Creates a 2-D array with m rows and n columns of zeroes
  let newArray = [];
  let row = [];
  for (let i = 0; i < m; i++) {
    // Adds the m-th row into newArray

    for (let j = 0; j < n; j++) {
      // Pushes n zeroes into the current row to create the columns
      row.push(0);
    }
    // Pushes the current row, which now has n zeroes in it, to the array
    newArray.push(row);
  }
  return newArray;
}

let matrix = zeroArray(3, 2);
console.log(matrix);
function zeroArray(m, n) {
  // Creates a 2-D array with m rows and n columns of zeroes
  let newArray = [];
  for (let i = 0; i < m; i++) {
    // Adds the m-th row into newArray
    let row = [];
    for (let j = 0; j < n; j++) {
      // Pushes n zeroes into the current row to create the columns
      row.push(0);
    }
    // Pushes the current row, which now has n zeroes in it, to the array
    newArray.push(row);
  }
  return newArray;
}

let matrix = zeroArray(3, 2);
console.log(matrix);

12. Debugging: Prevent Infinite Loops with a Valid Terminal Condition

function myFunc() {
  for (let i = 1; i != 4; i += 2) {
    console.log("Still going!");
  }
}
function myFunc() {
  for (let i = 1; i <= 4; i += 2) {
    console.log("Still going!");
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment