Skip to content

Commit

Permalink
Solve Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
DragunWF committed Feb 3, 2025
1 parent 01b4369 commit ada48f5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions CodeWars/javascript/7_kyu/merge_two_arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// https://www.codewars.com/kata/583af10620dda4da270000c5/train/javascript

function mergeArrays(a, b) {
const output = [];
let cog = true;
let a_index = 0,
b_index = 0;
while (a_index < a.length || b_index < b.length) {
if (a_index >= a.length) {
for (let i = b_index; i < b.length; i++) {
output.push(b[i]);
}
break;
} else if (b_index >= b.length) {
for (let i = a_index; i < a.length; i++) {
output.push(a[i]);
}
break;
}
output.push(cog ? a[a_index++] : b[b_index++]);
cog = !cog;
}
return output;
}

function test() {
console.log(mergeArrays([1, 2, 3], ["a", "b", "c", "d", "e", "f"]));
}

test();

0 comments on commit ada48f5

Please sign in to comment.