Skip to content

Instantly share code, notes, and snippets.

@akinjide
Created July 27, 2019 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akinjide/e02cc23248adb9d17f7c4c4831bc0cf3 to your computer and use it in GitHub Desktop.
Save akinjide/e02cc23248adb9d17f7c4c4831bc0cf3 to your computer and use it in GitHub Desktop.
/**
Write a function that takes three arguments, 2 arrays (a and b) and 1 integer (c)
then returns one array (d) which is created by inserting all elements in b into a at indexes
which are spaced by the integer value.
Consider all possible corner cases and print out the final array (d).
e.g
a = ["a", "b", "c", "d", "e", "f"]
b = ["1", "2", "3"]
c = 2
d = ["a", "b", "1", "c", "d", "2", "e", "f", "3"]
*/
const a = ['a', 'b' , 'c', 'd', 'e', 'f', 'g', 'h'];
const b = ['1', '2', '3', '4'];
const c = 2;
let d = [];
for (let i = 0; i < b.length; i++) {
let j = ((i + 1) - 1) * c;
console.log(a.slice(j, j + c), j, j + c);
/// d = [...d, ...a.slice(j, j + c)] /// spread operator
d = [].concat(d, (a.slice(j, j + c)));
d.push(b[i]);
}
console.log(d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment