Skip to content

Instantly share code, notes, and snippets.

@delonnewman
Last active August 3, 2023 18:37
Show Gist options
  • Save delonnewman/c594c8022404ac5df44cb1cd1b209b95 to your computer and use it in GitHub Desktop.
Save delonnewman/c594c8022404ac5df44cb1cd1b209b95 to your computer and use it in GitHub Desktop.
elisa
function returnCustomStepArray(num1, num2, step){
let myArray = [];
//Edge Cases
//Receive only numbers
if(typeof num1 !== "number" || typeof num2 !== "number" || typeof step !== "number"){
throw new Error("type error; numbers are expected");
}
//Round numbers down
num1 = Math.floor(num1);
num2 = Math.floor(num2);
step = Math.floor(step);
//Receive only numbers in ascending order
if(num1 >= num2){
throw new Error("numbers should be in ascending order");
}
for(let i = num1; i <= num2; i= i+step){
myArray.push(i);
}
return myArray
}
function sumNumbers(arr) {
let temp = 0;
for(let i=0; i <= arr.length-1; i++){
if(typeof arr[i] != "number"){
throw new Error("type error, an array of numbers is expected");
}else{
temp += arr[i];
}
}
return temp;
}
//EXERCISES:
// 1) calculate the sum of 1 to 100
console.log(sumNumbers(returnCustomStepArray(1, 100, 1)));
// 2) calculate the sum of the even numbers between 50 and 1,000
console.log(sumNumbers(returnCustomStepArray(50, 1000, 2)));
//3) calculate the sum of the factors of 3 between 33 and 3333
console.log(sumNumbers(returnCustomStepArray(33, 3333, 3)));
function random(max) {
return Math.floor(Math.random() * max);
}
//add a step value (number) that will control how the next value in the array is computed
function returnCustomStepArray(num1, num2, step){
let myArray = [];
//Edge Cases
//Receive only numbers
if(typeof num1 !== "number" || typeof num2 !== "number" || typeof step !== "number"){
throw new Error("type error; numbers are expected");
}
//Round numbers down
num1 = Math.floor(num1);
num2 = Math.floor(num2);
step = Math.floor(step);
//Receive only numbers in ascending order
if(num1 >= num2){
throw new Error("numbers should be in ascending order");
}
for(let i = num1; i <= num2; i= i+step){
myArray.push(i);
}
return myArray
}
console.log(returnCustomStepArray(2, 8, 2));
// 2) calculate the even numbers between 50 and 1,000
let evenNumbers = returnCustomStepArray(50, 1000, 2);
console.log(evenNumbers);
//3) calculate the factors of 3 between 33 and 3333
let factorsOfThree = returnCustomStepArray(33, 3333, 3);
console.log(factorsOfThree);
// Write a function that returns the sum of the numbers in an array
function sumNumbers(arr) {
let temp = 0;
for(let i=0; i <= arr.length-1; i++){
if(typeof arr[i] != "number"){
throw new Error("type error, an array of numbers is expected");
}else{
temp += arr[i];
}
}
return temp;
}
console.log(sumNumbers([2, 4, 7]);
// 1) calculate the sum of 1 to 100
let numbersOnetoOneHundred = [];
for(let i= 1; i<= 100; i++){
numbersOnetoOneHundred.push(i);
}
console.log(sumNumbers(numbersOnetoOneHundred));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment