2

I'm going through freeCodeCamp and am having trouble with their "Return Largest Numbers in Arrays" problem. The issue seems to be with my code for finding every fourth element in the second "for" loop. It looks like it's including the commas from the array "decArray". Any ideas on how to fix this or reorient my thinking process? I appreciate it!

The Instructions:

  • Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.
  • Remember, you can iterate through an array with a simple for loop, and access each member with array syntax arr[i].
  • largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return an array.
  • largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27,5,39,1001].
  • largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].

Here's My Code:

function largestOfFour(arr) {
  var decArray = [];    // initialize variable to sort each subarray in descending order
  var finalArray = [];  // initialize variable for final array
  
  // sort array values in descending numerical order instead of by alphabetical order
  function sortNumber(a,b) {
    return a - b;
  }
  
  // loop through initial array to sort each subarray in descending order
  for (var i = 0; i < arr.length; i++) {
    decArray += arr[i].sort(sortNumber).reverse() + ",";
  }
  
  // loop through decArray to find every fourth element
  for (var j = 0; j < decArray.length; j += 4) {
    finalArray.push(decArray[j]);
  }
  
  // return the final array
  return finalArray;
}

// test array
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

3
  • 1
    On line 12, what is ... + "," supposed to accomplish? Commented Mar 15, 2018 at 20:14
  • 2
    function largestOfFour (arr) { return arr.map(a => Math.max(...a)); } Commented Mar 15, 2018 at 20:14
  • @PatrickRoberts Good solution! Commented Mar 15, 2018 at 20:17

2 Answers 2

3

decArray is an array so you are supposed to push items there instead of append string to it.

replace below line

decArray += arr[i].sort(sortNumber).reverse() + ",";

with this

decArray.push(...arr[i].sort(sortNumber).reverse());

Let me know if this works for you.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, decArray.push(...arr[i].sort(sortNumber).reverse()); otherwise decArray won't be flat. Good explanation. This is the correct answer.
@null Your answer worked! Thanks for the help; I didn't even think about the fact that I was appending a string by adding that comma. That makes sense though. I appreciate it!
@LukeOrth I am glad this helped.
1

Actually the problem with your code is that when you are doing this decArray += arr[i].sort(sortNumber).reverse() + ","; you are converting array to string and at the end decArray will become string with info 5,4,3,1,...., so when you doing decArrayj it will return 3 as it is the fifth element in string, so you getting wrong answer. To solve this either do this finalArray.push(arr[i].sort(sortNumber).reverse()[0]) and remove thrid for loop. or try below code

function largestOfFour(arr){
  var finalArray = [];
	for(i = 0, i2 = arr.length; i<i2; i++) {
      finalArray.push(Math.max.apply(null, arr[i]));
  }
  return finalArray;
}

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

1 Comment

I didn't downvote this, but I will point out while this is correct information, this does not answer the question about the originally attempted implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.