Introduction to Loops
Introduction to loops in JavaScript.
We'll cover the following...
Background
There are certain parts of a program where you want to repeat an instruction or set of instructions. Take for example the following piece of code.
Press + to interact
var first = 0; // initialise first numbervar second = 1; // initialise second numbervar temp = NaN; // a temporary variable for swapping// calculate the third fibinacci numbertemp = second;second = first + second;first = temp;// calculate the fourth fibinacci numbertemp = second;second = first + second;first = temp;// calculate the fifth fibinacci numbertemp = second;second = first + second;first = temp;console.log(`first: ${first}, second:${second}`);
The above code calculates the fifth number in a Fibonacci sequence. A Fibonacci sequence is where each term ...