Question 1.
Write a function that sorts an arbitrary, but unique,
list of numbers in a zig-zag function such that “a < b > c < d” etc..
Assumptions:
1) The list can be of any length
2) The list is only integers
3) Each element is unique
Examples:
[3, 6, 5, 9, 4] -> [4, 9, 3, 6, 5]
[1, 0, 3, 2, 4] -> [3, 4, 1, 2, 0]
[ ↓, ↑, ↓, ↑, ↓]
input [0,1,2,3,4] ---> output [0,2,1,3,4] , [0,4,1,3,2]
input [3, 4, 5, 6, 9] ---> output [3,5,4,9,6], [3,9,4,6,5]
Question 2.
What is the output for the below code
const arr = [10, 12, 15, 21] // 4
for (var i = 0; i < arr.length; i++) {
setTimeout(() => console.log(`Index: ${i}, element: ${arr[i]}`), 1000)
}
Question 3.
// What is the output for the following use:
import logging
var counter = function () {
var _counter = 3
return {
add: (num) => _counter += num,
retrieve: () => `the value of counter is currently: ${ _counter}`
}
}
var count=counter();
count.add(9);
count.add(5);
console.log(count.retrieve())
Question 4.
// What is the output for the following use:
var A = function () {
var B = [];
return function(C) {
for (D in B) {
if (B[D] === C) {
return true;
}
}
B.push(C);
return false;
}
}();
console.log(A(1));
console.log(A(2));
console.log(A(1));