I've managed to create a function that checks whether arrays 'a' and 'b' can 'swap' corresponding values to 'increase' - e.g.:
var a = [1,2,3,4,5] // is increasing
var a = [1,4,6,7,36] // is increasing
var a = [1,6,3,6,5] // not increasing
Some successful tests:
increaser([5,3,7,7],[1,6,6,9]) - should return 2; a = [1,3,6,7] & b = [5,6,7,9]
increaser([1,5,6],[-2,0,2]) - should return 0; already increasing
increaser([5,-3,6,4,8],[2,6,-5,1,0]) - should return -1; cannot increase
increaser([5,3,7,7,10],[1,6,6,9,9]) - should return 3; a = [1,3,6,7,9], b = [5,6,7,9,10]
increaser([2,1,2,3,4,5],[0,1,7,5,2,3]) - should return -1; cannot increase
And the function returns 0 if the values in each array are 'already increasing', and returns -1 if the values can't be swapped in order to increase.
However, I belive I've written way overcomplex code, particularly for the parts that push checks if the conditions allow the array values to swap, and pushes the new values into the array - e.g.:
if(((b[i] >= b[i+1] || b[i] <= finalB[i-1]) && (a[i] < b[i+1] && a[i] > finalB[i-1]) && (b[i] < a[i+1]) && b[i] > finalA[i-1]) && ((a[i] >= a[i+1] || a[i] <= finalA[i-1]) && (b[i] < a[i+1] && b[i] > finalA[i-1])))
So my question is, how could I have written this to be much simpler? Any advice on writing better code would also be appreciated, as I'm relatively new to JavaScript. Thanks a lot
The solution below:
function increaser(a, b) {
function isAlreadyIncreasing(c, i, arr) { // c = currentValue; i = index; arr = array
return ((i == arr.length - 1 && c > arr[i - 1]) || c < arr[i + 1]);
}
if (!(a.every(isAlreadyIncreasing) && b.every(isAlreadyIncreasing))) {
console.log('Arrays are not already increasing');
// continue with function
var finalA = [], finalB = [];
var counter = 0; // no. of changes
// for every element, check if each element needs changing
for (i = 0; i < a.length; i++) {
// for first elements, if conditions match, push elements into new array
if (i == 0) {
if ((a[0] >= a[1] && b[0] < a[1] && a[0] < b[1]) ||
(b[0] >= b[1] && a[0] < b[1] && b[0] < a[1])) {
finalA.push(b[0]);
finalB.push(a[0]);
counter++;
} else {
finalA.push(a[0]);
finalB.push(b[0]);
}
}
// for 2nd - n-1th elements
if (i > 0 && i < a.length - 1) {
if (((b[i] >= b[i + 1] || b[i] <= finalB[i - 1]) &&
(a[i] < b[i + 1] && a[i] > finalB[i - 1]) &&
(b[i] < a[i + 1]) && b[i] > finalA[i - 1]) &&
((a[i] >= a[i + 1] || a[i] <= finalA[i - 1]) &&
(b[i] < a[i + 1] && b[i] > finalA[i - 1]))) {
finalA.push(b[i]);
finalB.push(a[i]);
counter++;
} else {
finalA.push(a[i]);
finalB.push(b[i]);
}
}
// for last elements, if conditions match, push elements into new array
if (i == a.length - 1) {
if ((a[i] <= finalA[i - 1] && b[i] > finalA[i - 1] && a[i] > finalB[i - 1]) ||
(b[i] <= finalB[i - 1] && a[i] > finalB[i - 1] && b[i] > finalA[i - 1])) {
finalA.push(b[i]);
finalB.push(a[i]);
counter++;
} else {
finalA.push(a[i]);
finalB.push(b[i]);
}
}
}
console.log("Original A: " + a);
console.log("Original B: " + b);
console.log("Final A: " + finalA);
console.log("Final B: " + finalB);
console.log("Counter: " + counter);
// if counter is 0, return -1, otherwise return counter
if (counter == 0) {
console.log('But arrays cannot increase');
return -1;
} else {
return counter;
}
} else {
console.log('Arrays are already increasing');
return 0;
}
}