JSFiddle - React, Tailwind, and code Playground

by Alexander Novikov

HTML

<script src="https://getfirebug.com/firebug-lite-debug.js"></script>

JavaScript

/*
1. Given 2 arrays “a” and “b”, how would you: 
a) append all elements of “b” to “a”
b) prepend all elements of ”b” to ”a”
c) put all elements of ”b” in the middle of ”a” (assuming ”a” contains even number of elements)
In all points of this question (a, b, c) your code should modify existing array “a”, not create a new one. Try to find the shortest code in pure JavaScript to solve this task.
*/
(function testA() {
    
    var a = [1,2,3,4],
        b = ['a', 'b'];
    
    Array.prototype.push.apply(a, b);
    
    console.log('a)');
    console.log(a);
    
})();
(function testB() {
    
    var a = [1,2,3,4],
        b = ['a', 'b'];
    
    Array.prototype.unshift.apply(a, b);
    
    console.log('b)');
    console.log(a);
    
})();
(function testC() {
    
    var a = [1,2,3,4],
        b = ['a', 'b'];
    
    Array.prototype.splice.apply(a, [a.length/2, 0].concat(b) );
    
    console.log('c)');
    console.log(a);
    
})();