I would like to write a function that checks whether or not an array would be incremental if we were to remove one element from the array. So for example an array like
[1,3,4,2,8]
Would return true because if we remove 2, the array would be incremental ie [1,3,4,8]
To solve this I created a function that iterates through the array, checks whether n is less than n 1 creating an array of true and false values depending on whether the element we are iterating through meets the condition stated.
In cases where n 1 is less than or equal to n; I then have another condition that checks whether n 1 is less than n-1, returning 'very false' in this instance.
I then want to return false if my new Array indicates that removing one element will not leave me with an array of incremental values. I check for by checking whether; i.) my new array contains more than one false value ii.) my new array contains the value 'very false' only in instances if very false is not the last element in my array iii.) my array contains both very false and false
Struggling to get this logic to work. I added a few test cases I am struggling to get right
function solution(sequence) {
let newArr = [];
for(let i = 0; i < sequence.length - 1; i ) {
sequence[i] < sequence[i 1] ? newArr.push(true)
: sequence[i 1] <= sequence[i-1] ? newArr.push('very false')
:newArr.push(false)
}
return (newArr.filter(i => i === false).length) > 1 || ( (newArr.includes('very false')) && (newArr[newArr.length - 1] !== 'very false')) || ( newArr.includes(false) && newArr.includes('very false')) ? false : true;
}
//TEST CASES
let one = [1, 2, 3, 4, 5, 3, 5, 6] //I get this right
let two = [40, 50, 60, 10, 20, 30] //I get this right
let three = [1, 2, 3, 4, 3, 6] //I get this wrong
let four = [1, 4, 10, 4, 2] // I get this wrong
//EXPECTED OUTCOMES
console.log(solution(one)) => false;
console.log(solution(two)) => false;
console.log(solution(three)) => true;
console.log(solution(four)) => false;
FYI: This is not for an assignment or test
CodePudding user response:
Creating a new array seems overkill. I would first define a function that verifies at which index a given array violates being incremental.
Then check if there is a second violation after that. If so, return false.
With just one violation, return true when it occurs between the first two or last two values in the array.
In all other cases, verify that removing either of the two involved values resolves the violation.
Code:
// Return the index where the value is not greater than its predecessor
function violation(a, start=0) {
for (let i = start 1; i < a.length; i ) {
if (a[i - 1] >= a[i]) return i;
}
return 0;
}
function solution(a) {
let i = violation(a);
return !i || !violation(a, i) && (i==1 || i==a.length-1
|| a[i-2] < a[i] || a[i-1] < a[i 1]);
}
//TEST CASES
let tests = [
[1, 2, 3, 4, 5, 3, 5, 6],
[40, 50, 60, 10, 20, 30],
[1, 2, 3, 4, 3, 6],
[1, 4, 10, 4, 2]
];
for (let test of tests) {
console.log(solution(test));
}
CodePudding user response:

