Skip to content

Instantly share code, notes, and snippets.

@pradeeprjth
Created August 24, 2021 11:36
Show Gist options
  • Save pradeeprjth/6d71fdd68dc96e4c50cecbb9e7b157a9 to your computer and use it in GitHub Desktop.
Save pradeeprjth/6d71fdd68dc96e4c50cecbb9e7b157a9 to your computer and use it in GitHub Desktop.
function sum(...theArgs) {
return theArgs.reduce((previous, current) => {
return previous + current;
});
}
console.log(sum(1, 2, 3));
// expected output: 6
console.log(sum(1, 2, 3, 4));
// expected output: 10
// --------------------------------------------------------------------
function myFun(a, b, ...manyMoreArgs) {
console.log("a", a)
console.log("b", b)
console.log("manyMoreArgs", manyMoreArgs)
}
myFun("one", "two", "three", "four", "five", "six")
// Console Output:
// a, one
// b, two
// manyMoreArgs, ["three", "four", "five", "six"]
// --------------------------------------------------------------------
const mSum = (num1, num2, ...args)=>{
console.log(num1);
console.log(num2);
console.log(args);
let sum = num1 + num2;
for(let i=0; i<args.length; i++){
sum += args[i]
}
}
mSum (1,2,3,4,5,6);
// ------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment