Currently tackling the precourse material for a coding bootcamp and hitting a roadblock with this particular question. Despite my best efforts, I just can't seem to meet one of the 7 conditions required. Let me outline the question, my attempted solution, and the conditions below:
Question
The task is to identify the incorrectly placed fruit in an array (orchard) following a specific format:
['apple', 'apple', 'apple', 'apple', 'elppa', 'apple'] All fruit will be correctly spelled except for one!
Your function should return the index position of the odd fruit. For instance, in the provided example, the function should output 4.
Note: The fruit type may vary, but the orchard will always consist of the same kind of fruit.
findWrongWayFruit(['apple', 'apple', 'elppa']) // returns 2
findWrongWayFruit(['apple', 'elppa', 'apple']) // returns 1
findWrongWayFruit(['banana', 'ananab', 'banana', 'banana']) // returns 1
findWrongWayFruit(['apple', 'elppa']) // returns 0 as it's impossible to determine the correct order
Answer
function findWrongWayFruit(orchard) {
const firstFruit = orchard[0];
if (orchard.length < 3) {
return 0;
}
for (let i = 1; i < orchard.length; i++) {
if (firstFruit !== orchard[i]) {
return i;
}
}
}
Conditions
6 Conditions Passed 1 Condition Failed
Create a functioning function
✓ Well done!
Returned value must be a number
✓ Well done!
If array length is less than 3, return 0
✓ Well done!
Return the correct index when incorrectly placed fruit is in the middle
✓ Well done!
Return the correct index when the anomaly occurs at the beginning
✕ AssertionError: expected 1 to equal +0
Return the correct index when the misplaced fruit is at the end
✓ Well done!
Successfully return index even if the odd fruit is located randomly in the array
✓ Well done!