Alright, so I feel like I’m close but for some reason I’am unable to return the largest of the 4 sub-arrays. I feel like the placeholder value is incorrect or I am doing something wrong in the if statement. Any suggestions? Thanks!
function largestOfFour(arr) {
let placeholder=0;
let newArr=[];
for (let i=0;i<arr.length;i++) {
for (let j=0;j<arr[i].length;j++) {
if (arr[i][j]>placeholder) {
placeholder=arr[i][j];
newArr.push(placeholder)
}
}
}
return newArr;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 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/84.0.4147.125 Safari/537.36.
This is how I tried to do that. But because the [i][j] loops are after the placeholder is declared globally I tried arr[0][0] which gives the same wrong array which is: [ 5, 13, 27, 32, 35, 37, 39, 1000, 1001 ]
function largestOfFour(arr) {
let placeholder=arr[0][0];
let newArr=[];
for (let i=0;i<arr.length;i++) {
for (let j=0;j<arr[i].length;j++) {
if (arr[i][j]>placeholder) {
placeholder=arr[i][j];
newArr.push(placeholder)
}
}
}
return newArr;
}
Like this? It shows [ 5, 13, 27, 18, 26, 32, 35, 37, 39, 1000, 1001, 857 ]
function largestOfFour(arr) {
let newArr=[];
for (let i=0;i<arr.length;i++) {
for (let j=0;j<arr[i].length;j++) {
let placeholder=arr[0][0];
if (arr[i][j]>placeholder) {
placeholder=arr[i][j];
newArr.push(placeholder)
}
}
}
return newArr;
}
Well I gave it a shot, if this isn’t what you meant I’ll probably figure it out if its just the positioning of where I put it lol
function largestOfFour(arr) {
let newArr=[];
for (let i=0;i<arr.length;i++) {
let placeholder=arr[0][0];
for (let j=0;j<arr[i].length;j++) {
if (arr[i][j]>placeholder) {
placeholder=arr[i][j];
newArr.push(placeholder)
}
}
}
return newArr;
}
Same as GhostRoboXt said.
You put placeholder at the wrong place. Move placeholder into sub-loop and find the largest number.
function largestOfFour(arr) {
let newArr=[];
for (let i=0;i<arr.length;i++) {
// Assume your array is not empty
let placeholder=arr[i][0];
for (let j=1;j<arr[i].length;j++) {
if (arr[i][j]>placeholder) {
placeholder=arr[i][j];
}
}
newArr.push(placeholder)
}
return newArr;
}
function largestOfFour(arr) {
let newArr=[];
for (let i=0;i<arr.length;i++) {
let placeholder=arr[i][0];
for (let j=0;j<arr[i].length;j++) {
if (arr[i][j]>placeholder) {
placeholder=arr[i][j];
}
}
newArr.push(placeholder)
}
return newArr;
}
I put the placeholder value into the subloop and I saw that you moved the push statement. I moved the push statement and it passed. That is one thing I hate about programming is how much positioning matters. I feel like that is something I should have figured out on my own but I was assuming the push statement was in the right place the whole time. But thanks for pointing it out!
It relates to the logic thinking. As you see, our old is starting from 0, 1 and then increase to 20 or older. Same as you put the code right position or not.
Just practicing more and you will find out that it is not so much difficult.