Sum of Range::
Before Modification
function range(start, end,step) {
var ans = [];
for (let i = start; i <= end; i++) {
ans.push(i);
}
return ans;
}
function sum(arr) {
let result = 0;
for(let c = 0; c < arr.length; c++) {
result += arr[c];
}
return result;
}
console.log(sum(range(1,10)));
Sum of Range::
After Modification::::
function range(start, end,step) {
var ans = [];
var stepOne = 1;
if (step != undefined){
stepOne = step;
}
if(start < end){
for(var i = start; i <= end; i += stepOne){
ans.push(i);
}
}
else if(start > end){
for(var i = start; i >= end; i += stepOne){
ans.push(i);
}
}
return ans;
}
function sum(arr) {
let result = 0;
for(let c = 0; c < arr.length; c++) {
result += arr[c];
}
return result;
}
console.log(range(1,10,2));
Reversing The Array :::::
var ans = [];
function reverseArray(array) {
for (var i = array.length-1; i >= 0; i–) {
ans.push(array[i]);
}
console.log(ans);
}
console.log(reverseArray([2,4,6,8]));
Reverse Array in place:::
function reverseArrayInPlace(array){
for (let i = 0; i < Math.floor(array.length ); i++) {
}
return array;
}
console.log(reverseArrayInPlace([2,6,4,5,6]));