Take a two-dimensional array (an array with an array as an element) as input and return an array with the location information of 'B' as an element.
function findBugInApples(arr) {
let result = [];
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
if (arr[i][j] === 'B') {
result.push(i, j)
}
}
}
return result;
}
I had some left over time so gave myself another algorithm question challenge which was part of my assignment from the bootcamp.
The requirement was to take an array of elements as input and return whether each element is greater than the sum of the elements before it.
My current 'failed' code is as follows:
function superIncreasing(arr) {
let temp = arr[0]
let result;
for (i = 2; i < arr.length; i++) {
for (j = 1; j < i; j++) {
temp = temp + arr[i-j]
}
if (arr[i] > temp) {
result = true
} else if (arr[i] <= temp) {
result = false
}
}
return result;
};
This code failed 2 test out of 5 test which basically means some part of the algorithm I came up with fails to cover some part of the requirement. The failed part is when an array with only 2 index is given to go through the function. The current code I wrote starts from temp to equal the 0th index of the array. Then as the loop goes the temp adds the indexes before the arr[i] which is the current index that is used to compare with the sum of the previous index of the array which is 'temp.'
Tomorrow I will give it a try to maybe pull down the temp part of the code to go below the comparison conditional statements.