Reputation: 19
the challenge is giving an array of subarrays like this one:
[[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]
the expected result should be [27, 5, 39, 1001]
for this example.
It wants me to return the largest number of each array. I tried the following solution. I'd appreciate if anyone tells what's wrong with it.
function largestOfFour(arr) {
const result = [];
for(let i = 0; i < arr.length; i++){
for(let a = 0; a < arr[i].length; a ++){
var highestNumber = 0;
if(arr[i][a] > highestNumber){
highestNumber = arr[i][a];
}
}
result.push(highestNumber)
highestNumber = 0;
}
return result
}
Thanks in advance!
Upvotes: 0
Views: 62
Reputation: 35501
The problem in your code is where you're initializing the highestNumber
in the inner loop. This means that your max value gets reset for each element in the inner array.
You should instead initialize it outside of the inner loop.
If you want to handle negative values, you should also initialize highestNumber
to the lowest possible negative value, instead of 0
.
function largestOfFour(arr) {
const result = [];
for(let i = 0; i < arr.length; i++){
var highestNumber = Number.MIN_SAFE_INTEGER; // <-- initialize here to lowest possible value
for(let a = 0; a < arr[i].length; a ++){
// var highestNumber = 0; // <-- move this up
if(arr[i][a] > highestNumber){
highestNumber = arr[i][a];
}
}
result.push(highestNumber)
highestNumber = 0;
}
return result
}
let arrays = [ [17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10] ];
console.log(largestOfFour(arrays))
A solution with less code is to use Math.max
to save you some typing:
let arrays = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]];
let result = arrays.map(arr => Math.max(...arr));
console.log(result); // [27, 5, 39, 1001]
Upvotes: 2
Reputation: 1188
var arr = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]];
var result = [];
for (var i=0; i < arr.length; i++){
result.push(Math.max(...arr[i]));
}
console.log(result);
Upvotes: 0
Reputation: 4519
The reason why your code wasn't working is because highestNumber
is inside the loop. so in each iteration the previous value is discarded by assigning 0 to it so you should move your highestNumber
variable outside the loop
arr=[[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]
var highestNumber = 0;
function largestOfFour(arr) {
const result = [];
for(let i = 0; i < arr.length; i++){
for(let a = 0; a < arr[i].length; a ++){
if(arr[i][a] > highestNumber){
highestNumber = arr[i][a];
}
}
result.push(highestNumber)
highestNumber = 0;
}
return result
}
console.log(largestOfFour(arr))
Alternatively there is a simpler and easier way to do the same thing using Math.max
let array = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]];
let mxel = array.map(el => Math.max(...el));
console.log(mxel)
Upvotes: 0
Reputation: 135
var arr = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]
console.log(largestOfFour(arr));
function largestOfFour(arr) {
const result = [];
for(let i = 0; i < arr.length; i++){
var highestNumber = 0;
for(let a = 0; a < arr[i].length; a ++){
if(arr[i][a] > highestNumber){
highestNumber = arr[i][a];
}
}
result.push(highestNumber)
highestNumber = 0;
}
return result
}
This should be outside the second for loop.
var highestNumber = 0;
Or.. You can do the easy way by using Math.max and map each element
let arr = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]];
let result = arr.map(eachInnerArr => Math.max(...eachInnerArr));
console.log(result)
Upvotes: 0
Reputation: 12949
You can use map
to achieve this:
let array = [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]];
let max_elements = array.map(el => Math.max(...el));
This works because you are expanding each sub-array in the Math.max
function, which will find the max of all its parameters
Upvotes: 1