Skip to content

Instantly share code, notes, and snippets.

@arnonate
Created September 18, 2020 19:29
Show Gist options
  • Save arnonate/6fb4a9caeb23cb7d582e0b2dee6a08b3 to your computer and use it in GitHub Desktop.
Save arnonate/6fb4a9caeb23cb7d582e0b2dee6a08b3 to your computer and use it in GitHub Desktop.
function myConcat(separator) {
var result = ""; // initialize list
var i;
// iterate through arguments
for (i = 1; i < arguments.length; i++) {
result += arguments[i] + separator;
}
return result;
}
// returns "red, orange, blue, "
myConcat(", ", "red", "orange", "blue");
// returns "elephant; giraffe; lion; cheetah; "
myConcat("; ", "elephant", "giraffe", "lion", "cheetah");
// returns "sage. basil. oregano. pepper. parsley. "
myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");
// The rest parameter syntax allows us to represent
// an indefinite number of arguments as an array.
function multiply(multiplier, ...theArgs) {
return theArgs.map(x => multiplier * x);
}
var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment