This question already has answers here:
Creating an array consisting of the largest values of each sub-array does not work as expected
(4 answers)
Closed 6 years ago.
Return Largest Numbers in Arrays
//my code
function largestOfFour(arr) {
var largest=[];
for(var i = 0; i < arr.length; i++){
for (var j = 0; j < arr[i].length; j++) {
if(arr[i][j] > largest){
largest[i]= arr[i][j];
}
}
}
return largest;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
//exp o/p
[27,5,39,1001]
//i m getting
[5,13]
I would do it in this way.
var largest=[];
a = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]
for(var i = 0; i < a.length; i++){
largest.push(a[i].sort(function(a, b){return b-a})[0])
}
console.log(largest);
if(arr[i][j]>largest)
should be
if(arr[i][j]>largest[i])
Initialize your array too with some MIN value like 0
function largestOfFour(arr) {
var largest=[];
for(var i = 0; i < arr.length; i++){
largest[i] = 0;
for (var j = 0; j < arr[i].length; j++) {
if(arr[i][j] > largest[i]){
largest[i]= arr[i][j];
}
}
}
return largest;
}
Related
I'm trying to return the largest number in each of the sub-arrays - but this is returning the first two values of each sub-array. Seems pretty simple and yet I can't find where I go wrong.
function largestOfFour (arr) {
let maxVal = 0
let newArr = []
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
if (arr[i][j] > maxVal) {
maxVal = arr[i][j]
newArr.push(maxVal)
}
}
}
return newArr
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]))
This code returns the following: [ 4, 5, 13, 27, 32, 35, 37, 39, 1000, 1001 ]. Where am I going wrong? I don't want to use .sort()
The problem is, you push a new value whenever a new max value is found and not deleting the previously pushed value from the current set, you need to store the maxVal until the end of the inner loop and push the value afterwards, so you only push one value for each array, which is actually the biggest one
Your function should look similar to this:
function largestOfFour (arr) {
let maxVal = 0;
let newArr = [];
for (i = 0; i < arr.length; i++) {
// reset maxVal, for new set of numbers
maxVal = 0;
for (j = 0; j < arr[i].length; j++) {
if (arr[i][j] > maxVal) {
// new largest number found
maxVal = arr[i][j];
}
}
// push highest number found in set
newArr.push(maxVal);
}
return newArr
}
I don't know whether this is allowed, but it will get you the result:
const arr=[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]
console.log(arr.map(a=>Math.max(...a)))
I prefer #CarstenMassmann's answer; however, just so you can understand how your code was in error, but very close, see this edit to the original attempt.
That inner conditional doesn't find the max of the sub-array, if finds the interim max. Don't push to your results inside the condition, finish iterating the sub-array and then push the best interim max...
function largestOfFour (arr) {
let maxVal = 0
let newArr = []
for (i = 0; i < arr.length; i++) {
for (j = 0; j < arr[i].length; j++) {
if (arr[i][j] > maxVal) {
maxVal = arr[i][j]
// don't push here. maxVal is just the max *for now*
}
}
newArr.push(maxVal) // push here
}
return newArr
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]))
I am a beginner in Javascript and I should write a code to solve this problem: Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays. and write this code can u tell me what is the problem him thnx:
function largestOfFour(arr) {
var pos = 0;
var max = 0;
var add = 0;
var sum = [];
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < arr[i].length; j++) {
if (j === 4) {
sum[i] = add;
} else {
add = add + arr[i];
}
}
for (var i = 0; i < arr.length; i++) {
if (sum[i] > max) {
max = sum[i]
pos = i;
}
}
return arr[pos];
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
If you have no restrictions, you could take
Array#map for getting a new array with
Math.max for getting a maximum number and take
spread syntax ... for converting an array to parameters.
const largestOfFour = array => array.map(a => Math.max(...a));
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
For a more classic style, you could separate the functions, one for processing the outer array and one for getting a maximum value.
function max(array) {
var max = array[0];
for (let i = 1; i < array.length; i++) {
if (max < array[i]) max = array[i];
}
return max;
}
function largestOfFour(array) {
var result = [];
for (let a of array) result.push(max(a));
return result;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
I'm trying to figure out how to get the below code to push the biggestNumber variable to the largestNumbers variable so that the largest number in each sub-array of largestOfFour will be returned as an array of largestNumbers.
I see it mentally and thought that I was writing the code correctly. This is my thought process:
1) largestNumbers is set up as an empty array.
2) create a for loop that will pull each sub-array during a loop and store it as PulledSubArray.
3) I need to loop through the sub-array(pulled
SubArray) with a for loop.
4) biggestNumber variable is set-up to store the biggest number in the sub-array and an if statement is set-up to determine which number is the biggest and then push that number to the largestNumbers array.
5) break the inner for loop and then repeat steps for second sub-array.
Without giving the answer to the entire challenge, could you tell me what syntax or elements I'm missing to achieve my end result
function largestOfFour(arr) {
var largestNumbers = [];
for (var i = 0; i > arr.length; i++){
var pulledSubArray = arr[i];
for (var n = 0; n > pulledSubArray.length; n++){
var biggestNumber = 0;
if (pulledSubArray[n] > biggestNumber){
biggestNumber = pulledSubArray[n];
} else if (pulledSubArray[n] < biggestNumber){
largestNumbers.push(biggestNumber);
break;
}
}
}
return largestNumbers;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
for (var i = 0; i > arr.length; i++){
^
Wrong condition
Your loop constructs are wrong. Correct them. A simple version of your function looks like (ECMAScript6)
const largestOfFour = (arr) => arr.map(el => Math.max(...el));
In case you want to stick to your thought
function largestOfFour(arr) {
var largestNumbers = [];
for (var i = 0; i < arr.length; i++) { // Correct loop condition
var pulledSubArray = arr[i];
var biggestNumber = pulledSubArray[0];
for (var n = 1; n < pulledSubArray.length; n++) { // Correct loop condition
if (pulledSubArray[n] > biggestNumber) {
biggestNumber = pulledSubArray[n];
}
}
largestNumbers.push(biggestNumber); // Push biggest number outside the inner loop
}
return largestNumbers;
}
console.log(largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
]));
Issue is with Code:
Logic:
if (pulledSubArray[n] > biggestNumber) {
biggestNumber = pulledSubArray[n];
} else if (pulledSubArray[n] < biggestNumber) {
largestNumbers.push(biggestNumber);
break;
}
For an array [4, 5, 1, 3], for second iteration, biggestNumber holds 4 and pulledSubArray[n] will hold 5. For this case, is you would push the value and break the loop. But you are not checking the entire array. So you will not get accurate result.
Loop condition:
for (var i = 0; i > arr.length; i++){
This will never enter the loop. It should be i < arr.length
Variable declaration:
for (var n = 0; n > pulledSubArray.length; n++) {
var biggestNumber = 0;
if (pulledSubArray[n] > biggestNumber) {
Variable should be declared outside the loop. You are currently setting its value to 0 in every iteration. So you will eventually get the last value as if (pulledSubArray[n] > biggestNumber) will always return true.
Updated Code:
function largestOfFour(arr) {
var largestNumbers = [];
for (var i = 0; i < arr.length; i++) {
var pulledSubArray = arr[i];
var biggestNumber = 0;
for (var n = 0; n < pulledSubArray.length; n++) {
if (pulledSubArray[n] > biggestNumber) {
biggestNumber = pulledSubArray[n];
}
}
largestNumbers.push(biggestNumber)
}
return largestNumbers;
}
var list = largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
]);
console.log(list)
If you want to get largest number in each sub array:
var data = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
function largestOfFour(data) {
return data.map(function(d) {
return Math.max.apply(null, d)
})
}
console.log(largestOfFour(data))
Or even shorter (es6):
let data = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]];
const largestOfFour = data => data.map(d => Math.max.apply(null, d))
console.log(largestOfFour(data))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Iam trying to sort a multidimension array and put the highest numbers of each sub-array in an array with a double loop but iam getting an infinite loop error for some reason:
The array to be sorted:
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
The code:
function largestOfFour(arr) {
var temp;
for (var i = 0; i < arr.length; i++) {
var largestNumber = 0;
for (var j = 0; i < arr[i].length; j++) {
if (largestNumber < arr[i][j]) {
largestNumber = arr[i][j];
}
}
temp[i] = largestNumber;
}
return temp;
}
You need two changes, one for the right type of the variable to assign the result, and the check in the for loop with the right variable.
function largestOfFour(arr) {
var temp = []; // necessary
for (var i = 0; i < arr.length; i++) {
var largestNumber = 0;
for (var j = 0; j < arr[i].length; j++) {
// ^^ j instead of i
if (largestNumber < arr[i][j]) {
largestNumber = arr[i][j];
}
}
temp[i] = largestNumber;
}
return temp;
}
console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
I'm trying to find the maximum values of a multidimensional array.
Input:
[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]
Result:
[5, 27, 39, 1001]
What I've tried:
var largestArr = [];
function largestOfFour(arr) {
for (var i = 0; i < arr.length; i++) {
var largest = 0;
var currentArr = arr[i];
var stop = currentArr.length;
for (var j = 0; j < stop; j++) {
if (currentArr[0] > largest) {
largest = currentArr[0];
largestArr.push(largest);
largestArr.shift();
}
}
return largestArr;
}
}
largestOfFour([
[4, 5, 1, 3],
[13, 27, 18, 26],
[32, 35, 37, 39],
[1000, 1001, 857, 1]
]);
Can anyone help me understand why this isn't working?
The problem is here
if (currentArr[0]>largest){
largest = currentArr[0];
//
}
This should be done like below:
if (currentArr[j]>largest){
largest = currentArr[j];
//
}
Why this is wrong?
This is wrong because you always compare the first item of each array with the largest and not all the items in each array one after the other.
Furthermore, you have to make a few more corrections.
Specifficaly, there isn't any need to use the shift method. You have only to push the largest value of each array after the end of each processing in the largestArr. Last, but not least you have to move the return statement of the largestArr out of the for loops.
var largestArr = [];
function largestOfFour(arr) {
for (var i=0; i<arr.length; i++) {
var largest = 0;
var currentArr = arr[i];
var stop = currentArr.length;
for (var j=0; j<stop; j++){
if (currentArr[j]>largest){
largest = currentArr[j];
}
}
largestArr.push(largest);
}
return largestArr;
}
alert(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
Since the answer has been provided here's some code can do the same job in a smaller number of lines that you might find useful in the future using reduce.
var out = arr.reduce(function (p, c) {
return p.concat(Math.max.apply(null, c));
}, []);
DEMO
Christos' answer is correct and should the selected as answer.
Additionally though, you should not pollute the global namespace by declaring largestArray outside of the function, considering that you are returning said array anyway.
function largestOfFour(arr) {
var largestArr = []; // put this line here instead of making it global
for (var i=0; i<arr.length; i++) {
var largest = 0;
var currentArr = arr[i];
var stop = currentArr.length;
for (var j=0; j<stop; j++){
if (currentArr[j]>largest){
largest = currentArr[0];
}
}
largestArr.push(largest);
}
return largestArr;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);