You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
1. Debugging: Use the JavaScript Console to Check the Value of a Variable
leta=5;letb=1;a++;// Only change code below this lineconsole.log(a);//Debug purposeletsumAB=a+b;console.log(sumAB);
2. Debugging: Understanding the Differences between the freeCodeCamp and Browser Console
// Open your browser console.letoutput="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
letseven=7;letthree="3";console.log(seven+three);// Only change code below this lineconsole.log(typeofseven);console.log(typeofthree);
4. Debugging: Catch Misspelled Variable and Function Names
letreceivables=10;letpayables=8;letnetWorkingCapital=recievables-payable;console.log(`Net working capital is: ${netWorkingCapital}`);
5. Debugging: Catch Unclosed Parentheses, Brackets, Braces and Quotes
letmyArray=[1,2,3;letarraySum=myArray.reduce((previous,current=>previous+current);console.log(`Sum of array values is: ${arraySum}`);
6. Debugging: Catch Mixed Usage of Single and Double Quotes
letinnerHtml="<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
letx=7;lety=9;letresult="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
functiongetNine(){letx=6;lety=3;returnx+y;}// let result = getNine;letresult=getNine();console.log(result);
9. Debugging: Catch Arguments Passed in the Wrong Order When Calling a Function
functionraiseToPower(b,e){returnMath.pow(b,e);}letbase=2;letexp=3;// let power = raiseToPower(exp, base);letpower=raiseToPower(base,exp);console.log(power);
10. Debugging: Catch Off By One Errors When Using Indexing
functioncountToFive(){letfirstFive="12345";letlen=firstFive.length;// Only change code below this line/* for (let i = 1; i <= len; i++) { console.log(firstFive[i]); } */for(leti=0;i<len;i++){console.log(firstFive[i]);}}countToFive();
11. Debugging: Use Caution When Reinitializing Variables Inside a Loop
functionzeroArray(m,n){// Creates a 2-D array with m rows and n columns of zeroesletnewArray=[];letrow=[];for(leti=0;i<m;i++){// Adds the m-th row into newArrayfor(letj=0;j<n;j++){// Pushes n zeroes into the current row to create the columnsrow.push(0);}// Pushes the current row, which now has n zeroes in it, to the arraynewArray.push(row);}returnnewArray;}letmatrix=zeroArray(3,2);console.log(matrix);
functionzeroArray(m,n){// Creates a 2-D array with m rows and n columns of zeroesletnewArray=[];for(leti=0;i<m;i++){// Adds the m-th row into newArrayletrow=[];for(letj=0;j<n;j++){// Pushes n zeroes into the current row to create the columnsrow.push(0);}// Pushes the current row, which now has n zeroes in it, to the arraynewArray.push(row);}returnnewArray;}letmatrix=zeroArray(3,2);console.log(matrix);
12. Debugging: Prevent Infinite Loops with a Valid Terminal Condition