Created
July 27, 2019 10:25
-
-
Save akinjide/e02cc23248adb9d17f7c4c4831bc0cf3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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