THE SUM OF A RANGE:
I managed to get it solved but after looking at the answer I didn’t quite understood the difference and basically how the ternary operator worked there. I leave the solution that is given in the book down below, hoping that someone can clarify this for me.
My solution:
function range(start, end, optional){
var rangeArray = [];
if (optional === undefined){
for(var i = start; i <= end;i++){
rangeArray.push(i);
}}
else if(optional > 0){
for(var i = start; i <= end; i += optional){
rangeArray.push(i);
}} else if(optional < 0 ){
for(var i = start; i >= end; i += optional){
rangeArray.push(i);
}}
return rangeArray;
}
function sum(array){
var addition = 0;
for(var i = 0; i < array.length; i++){
addition += array[i];
}
return addition;
}
The book’s solution:
function range(start, end, step = start < end ? 1 : -1) {
let array = [];
if (step > 0) {
for (let i = start; i <= end; i += step) array.push(i);
} else {
for (let i = start; i >= end; i += step) array.push(i);
}
return array;
}
function sum(array) {
let total = 0;
for (let value of array) {
total += value;
}
return total;
}
Basically I didn’t quite understand how is it possible that once that the condition checks either true or false with the ternary operator, after it assigns a value to the “step” parameter, why in the code it runs a different given number, is the new value overriding the one that is given after the condition is checked? I hope that someone can help me with this.
REVERSING AN ARRAY:
function reverseArray(array){
var newArray = [];
for(var i = array.length -1; i >= 0; i--){
newArray.push(array[i]);
}
return newArray;
}
function reverseArrayInPlace(array){
for(var i = 0; i < Math.floor(array.length/2); i++){
let swap = array[i];
array[i] = array[array.length -1 - i ];
array[array.length - 1 - i] = swap;
}
return array;
}
The first function of this exercise takes a given array, loops through each value backwards and pushes each value in a new array.
On the other hand, the second one is a a bit more complex. It requires a swap between the first value with the last, the second value with the second last and so forth and also avoid overwrite values that will be needed. It is due to this reason that me must iterate only through the first half of the array and then swap the value.
The loop iterates through each i
value of the array until it reaches half its length to avoid overwriting values. It then creates a local variable to which the i
is assigned, then this value is swapped with its mirror value. This is achieved by subtracting i
to the last value of the array, this will give the mirror index of the i
value.
Example
var array = [1, 2, 3, 4, 5, 6, 7]
If i = 0 then array[i] = 1
if we subtract i
(which in this case is 0
) to the array[array.length - 1
] (the last value of the array) that would give us its mirror value.
array[0] = array[6-0] or array[0] = array[6]
array[1] = array[6-1] or array[1] = array[5]
array[2] = array[6-2] or array[2] = array[4]
and so on...
Once the first value is assigned with last value then we need to assign the last value to the first.
array[6-0] or array [6] = localVariable
The last array is assigned again to the local variable which was previously assigned with the first value.
Once the iteration is over the function returns the array with its swapped values.