Now that you have the basics of JavaScript and working with JavaScript in the web down, we wanted to revisit some fundamental concepts and give you a deeper understanding of how to use them effectively in your programs.
These exercises will take a closer look at looping and conditional branching in JavaScript.
- Using Visual Studio Code, open the folder
flexpath-revisit-looping-and-cond-branch-exercises
wherever you saved it on your device. - Then open the
flexpath-revisit-looping-and-cond-branch-exercises/exercises
folder. Inside are a collection of folders for each exercise. Exercise instructions are provided to you in .txt files in these folders. - To test your code, you will open up your terminal, and navigate to the specific
exercise_[number]
folder inside of the largerexercises/
folder where you are working in, using the change directory commandcd
. - You will then run the command
node [filename].js
to run whichever file in the folder you are working with, replacing[filename]
in the command with the file's actual name.
Exercise solutions are in the /solution
folder
Note:
For the first few exercises in this repository, we have included example JS code you can use to test your solutions.
But, after the first few exercises we don't provide it anymore.
As a Software Developer, you will have to both solve a problem given to you and come up with code on your own to test that your solution to that problem actually works.
This is an important skill to build, and it is vital to being able to do this job.
Therefore, we want you to start getting comfortable writing JavaScript code to test that your solutions work as intended.
-
Looping Constructs:
- Classic For Loop: Understanding initialization, condition, and iteration. Common pitfalls such as off-by-one errors.
- For-Of Loop: Iterating over arrays and other iterable objects. Differences between
let
andconst
usage in loops. - For-In Loop: Iterating over object properties and understanding the nuances compared to
for-of
. - While and Do-While Loops: Usage and typical errors, like infinite loops and index mismanagement.
- ForEach Loop: Using
forEach
on arrays and understanding how it handles sparse arrays differently from traditional loops.
-
Advanced Iteration:
- Handling Sparse Arrays: Differences between regular loops and
forEach
. - Using Functional Methods: Methods like
filter
,map
,reduce
,find
,some
, andevery
for array manipulation. - Performance Considerations: Best practices for optimizing loop performance and when to use break or early exit conditions.
- Handling Sparse Arrays: Differences between regular loops and
-
Branching Constructs:
- If-Else Statements: Proper usage with and without braces and common errors to avoid.
- Switch Statements: Understanding fall-through behavior, best practices, and grouping case statements.
- Ternary Operator: Writing concise conditional logic.
- Falsy and Truthy Values: How JavaScript handles falsy and truthy values and common pitfalls.
-
Nullish Coalescing:
- Nullish Coalescing Operator (
??
): Simplifying checks for null or undefined values and using default values efficiently.
- Nullish Coalescing Operator (
-
Practical Applications:
- Scraping data from HTML elements using DOM methods.
- Interacting with data structures like Maps, Sets, and HTMLCollections.
- Error handling in loops and preventing infinite loops.