Wednesday, May 26, 2021

Javascript Practice 64: JS Hero - for loop

 
Question: 

Write a function addTo that accepts a number as a parameter and adds all natural numbers smaller or equal than the parameter. The result is to be returned.

Example: addTo(3) should return 1+2+3 = 6.




Answer: 

function addTo (num) {

let sum = 0;

for (let i = 1; i <= num; i++) {

  sum = sum + i;

}

return sum

}




> we declare a function addTo
> it has one parameter 'num' 
> we first declare a variable 'sum' with a value of 0
> we then open the 'for' loop 
> the 'for' statement creates a loop 
> this loop consists of 3 optional expressions
> these 3 expressions are enclosed in parenthesis
> they are separated by semi colons
> like this (let i = 0; i < 10; i++) 
> the 'for' loop is usually followed by a statement / block statement which is to be executed in the loop; also called loop code
>  the first expression is the start expression 
> it is executed once at the beginning of the loop 
> the loop variable is initialized in the first expression like this: " let i = 0; "
> the second expression is the loop condition
> it is evaluated to true or false before each loop iteration
> if this expression evaluates to true, the loop code is executed
> if this expression evaluates to false, the loop is terminated and the program execution continues after the loop 
> the third expression or final expression is executed after each loop iteration
> the loop variable ( ' i ' ) is normally increased here
> then a new loop iteration is started with a new evaluation of the loop condition
> the loop code follows the three control expressions in brackets
>> let us assume that we call the addTo function with number 3 as a 'num' parameter
>> in our solution we declare a variable sum that has a value of 0
>> first expression: let i = 1
>> second expression or condition: i <= num
>> third expression: i++ (i++ is identical to i = i + 1)
>> loop code: sum = sum + i
>> in the start expression ' i ' is initialized with a value of 1 
>> the loop condition is i <= num (3);
>> if ' i ' is less than or equal to the num then the loop code is executed (1 <= 3)
>> the sum variable shall receive the value of sum + i or (0 + 1 = 1)
>> in the final or third expression i is increased by 1 (1 + 1 = 2)
>> ' i ' thus receives the value 2
>> the second loop starts with the evaluation of the loop condition
>> the condition 2 <= 3 is fulfilled
>> the loop code is executed again and sum is now given the value (1 + 2 = 3)
>> in the final or third expression i is increased by 1 (2 + 1 = 3)
>> ' i ' thus receives the value 3
>> the second loop starts with the evaluation of the loop condition
>> the condition 3 <= 3 is fulfilled
>> the loop code is executed again and sum is now given the value (1 + 2 + 3 = 6)
>> in the final expression ' i ' is increased again and gets the value  (3 + 1 = 4)
>> the second loop starts with the evaluation of the loop condition
>> the condition 4 <= 3 is no longer fulfilled
>> the loop is terminated and the program execution continues after the loop 
>> in this case the program executes and returns the 'sum' variable to us
>> our code example has added all natural numbers smaller or equal to 3

No comments:

Post a Comment