Monday-Tuesday: Loops & TDD Copy

Introduction to Loops

Programming languages are very useful for completing repetitive tasks. Fortunately computers are able to solve problems that involve lot of calculations multiple times. Something that will take a human hours to complete, computers can do that in a few seconds. As expected the speed depends on the complexity of the calculation as well as the computer itself. A way to make a computer to do a repetitive task is using a loop.

Learning Objectives

  • Explain what loops are
  • Why are loops useful?
  • Different kinds of loops
  • Loops structure
  • Nesting loops
  • Break & Continue statement
  • Infinite loops
  • Introduction to Test Driven Development

Study

What are loops?

Loops are a programming element that repeat a portion of code a specific number of times until you reach the desired result.

As you will see repetitive tasks are common in programming ,for that Loops offer a quick and easy way to solve this problem, which will save us a lot of time and help us write more manageable and organized code, which means less(minimize errors).

Different kinds of loops

JavaScript supports different kinds of loops:

  • for-loop repeats until a specified condition evaluates to false.
for (initialExpression; conditionExpression; incrementExpression){
  //statement
}
  • while-loop loops through a block of code as long as the given condition is true
while (condition){
// statement
}
Breaking the loops || Continue to the next iteration

Normally, a loop will keep running until the condition become false , but what if we want to stop -force exit- the loop and get out of it at a specific point, is that possible? For sure yes… and for this you can use the break statement, which jumps out of the loop.

In the other hand ,in some cases you will find that you need to run the loops except at a specific condition for example you need to print all numbers from 1 to 5 except 3, how can we do this?

For such cases you can use continue statement that will breaks one iteration (in the loop), if a specified condition occurs. also you can use Javascript Break and Javascript Continue Together read for more information.

Infinite loops in JavaScript

An infinite loop is a piece of code that keeps running forever as the terminating condition is never reached. An infinite loop can crash your program or browser and freeze your computer. To avoid such incidents it is important to be aware of infinite loops so that we can avoid them.

Resources

  1. Check this MDN article: Looping Code. make sure you tackle the ‘Active Learning’ sections at the bottom of the page.

  2. Loops examples in javaScript

  3. JavaScript Break and Continue

  4. Loops and iteration MDN

Introduction to Test Driven Development

Test Driven Development (TDD) refers to the practice of writing automated tests before completing the entire code. In other words, you write some code and then you test it before you write more code.

For example let’s say you want to write a function that multiplies two numbers and returns their product. With the TDD approach you should first write a test that uses that function and gives the expected output. As expected the test will fail before you write the code and when the tests pass then you know that your code works correctly.

In the example above if we had a simple function such as multiply(2, 2) it wouldn’t be a big deal to check the function ourselves. We would have to run the code over and over again plugging in different numbers until we were sure that it was working. However what would have happened if we had a much more complicated function, for example a function that checks whether or not someone has won a game of tic tac toe gameWin(["o", null,"x",null,"x",null,"x", "o", "o"])? We would have to play multiple games against ourselves to check that the function is working correctly!

For this reason TDD is a much more productive than writing code without tests. Later in this course we will teach you how to write these tests.

In the exercises we have the tests already written out for you. All you have to do is read the specs and write the code that makes them pass! The very first exercise (01-helloWorld) is intentionally very simple and walks you through the process of running the tests and making them pass.

Want MORE practice?

You really can’t spend too much time reviewing and practicing the fundamentals that we’ve covered here and luckily for you there are several websites that offer just this type of thing. Seriously, the better you grasp and the more fluently you can use the fundamental building blocks of JavaScript, the easier the hard stuff is going to come to you when we get to it. Take a minute to check out the following links.

  • Exercism – Uses a neat command-line interface to download and submit exercises and allows you to give and get feedback on your solutions.

  • CodeWars – A neat gamified system with leveled exercises. As you complete more exercises you level up and access more difficult ones.

  • HackerRank – Similar in setup to CodeWars, but this site is more focused on interview question type tasks. Some companies even use this site to interview candidates.

Leave a Reply