Tell us what’s happening:
So I am pretty sure I got my algorithm correct however, the exercise returns an error for some of its test cases when I try to submit it.
I’ve been using playcode.io to practice from scratch and the console seems to return what the exercise is looking for in every case.
Knowing that my code was correct, I decided to look up the solution and when I copied/pasted the websites’ solution into playcode.io, it returns a completely different answer than what the exercise is looking for…
Now I am really confused as to why and how two separate editors that are for the same language have different syntax… or at the very least… one editor assumes the other is incorrect.
I think my code was foolproof not just because playcode editor said so, but also, in the bootcamp editor, I even checked my work by using console.log(arr) towards the very end of my function and the console displays the exact answers that this exercise was looking for.
Suffice to say, based on the syntax I have covered so far from this bootcamp, I thought I was certain and yet, the bootcamp testers seem to betray its own curriculum.
if someone can explain why playcode editor and even the exercise itself displays the correct answer while the submission link is telling me I am wrong I would greatly appreciate it.
Also… one thing I noticed was that for my second loop:
for(let j=0; j<=arr[i].length; j++)
the condition should be length-1 however, my code does not work unless the length condition is equal to the actual length of the array which is odd because when I wrote out each loop manually, each subarray will be reduced to the largest number in length-1 times. but my code will not work for certain cases unless it has one extra loop.
Your code so far
let b23 = [[223, 222, 244, 123], [-72, -3, -17, -10], [25, 48, 21, -3]];
function tester(arr){
for(let i=0; i<=arr.length-1; i++){
let a2 = arr[i];
for(let j=0; j<=arr[i].length; j++){
let beg = a2[0];
let ender = a2.length-1;
let end = a2[ender];
if(beg > end){
let endIndex = a2.indexOf(end);
a2.splice(endIndex, 1);
}
else if(end > beg){
a2.splice(0, 1);
}
}
arr[i] = a2.toString();
}
console.log(arr)
}
tester(b23);
function largestOfFour(arr) {
// You can do this!
let result = [];
for(let i=0; i<=arr.length-1; i++){
let largestNum = arr[i][0];
for(let j=1; j<=arr[i].length; j++){
if(arr[i][j] > largestNum){
largestNum = arr[i][j];
}
}
result[i] = largestNum;
}
return result;
}
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
.
Link to the challenge: