RANGE:
function range(start, end) {
let rangeArray = []
for (let i=start; i <= end; i++) {
rangeArray.push(i)
}
return rangeArray
}
console.log(range function 1 to 10: ${range(1,10)}
)
SUM:
function sum(arr) {
let sumArr = 0
for (num of arr) {
sumArr = sumArr + num
}
return sumArr
}
console.log(sum/range function 1 to 10: ${sum(range(1,10))}
)
RANGE STEP:
function rangeStep(start, end, step=1) {
let rangeArray = []
if (step < 0){
for (let i=start; i >= end; i = i + step) {
//console.log(i)
rangeArray.push(i)
}
}
for (let i=start; i <= end; i = i + step) {
//console.log(i)
rangeArray.push(i)
}
return rangeArray
}
console.log(rangeStep function {rangeStep(1,10,2)}: ${rangeStep(1,10,2)}
)
console.log(rangeStep function {rangeStep(5,2,-1)}: ${rangeStep(5,2,-1)}
)
console.log(rangeStep function {rangeStep(1,10)}: ${rangeStep(1,10)}
)
REVERSE ARRAY:
function reverseArray(arr){
reverseArr = []
for (element of arr){
reverseArr.unshift(element)
}
return reverseArr
}
console.log(reverseArray {reverseArray[1,2,6,7]}: ${reverseArray([1,2,6,7])}
)
REVERSE ARRAY IN PLACE:
function reverseArrayInPlace(arr){
let l = arr.length
for (let i = 0; i < l; i++){
arr[l * 2 - i - 1] = arr[i]
}
for (let i = 0; i < l; i++){
arr.shift()
}
return arr
}
console.log(reverseArrayInPlace {reverseArrayInPlace([11,12,16,17])}: ${reverseArrayInPlace([11,12,16,17])}
)