0

So I have question where I am given an array of arrays like this.

Array = [ [2,5,1], [1,23], [3], [22,16,8] ]

I have to order the sub arrays from smallest to largest based on the largest value in the array. So that top array becomes:

[ [3], [2,5,1], [22,16,8], [1,23] ]

I have to create two functions, one to find the max value of an array, and one that gets put into the following method.

x.sort(method) would result in the answer above.

I created a method to find the max value in an array which is here.

function fidMax(array){
  copy = array.sort(function(a,b){return a-b})
  return copy[copy.length-1]
}

It just sorts the array, and returns the last element.

Now I want to make my second method which uses reduce.

function compare(array){
  array.reduce(findMax)
}

I have that, but it is NOT working, it won't even compile, and I am not sure why. I have to use the findMax() method in the compare() method, and the compare() method MUST use reduce().

Any ideas on what to do?

Thanks.

10
  • You don't need .reduce there at all: just sort by the largest value. Commented Jul 10, 2017 at 1:14
  • I must use reduce in the compare method. It's an exercise. Commented Jul 10, 2017 at 1:14
  • What compare function is supposed to return? (you have not explained it anywhere) Commented Jul 10, 2017 at 1:15
  • I'm not sure if it is suppose to return anything, but the compare function gets put into x.sort(compare) and that would return the answer. Compare function must use reduce, and the max function which I already created. Commented Jul 10, 2017 at 1:16
  • What was the original task text? What you explained and provided makes very little sense. Commented Jul 10, 2017 at 1:17

2 Answers 2

2

Sorting mutates the array and is probably not what you want to do. I won't do the whole exercise for you, but I can help you find the max value in an array using reduce.

> const foo = [3, 5, 2];
undefined
> foo.reduce((max, currentValue) => Math.max(max, currentValue))
5
Sign up to request clarification or add additional context in comments.

8 Comments

Math.max(...foo)
True, but the exercise says to use reduce. I don't see how else you'd use reduce.
"True, but the exercise says to use reduce" --- and that's the problem: they did not provide the exact exercise text. And the OP only mentioned that it's comparator should use .reduce, not the function that searches for the maximum value: "and the compare method MUST use reduce"
Here is the exact question.
Part A.) Write a callback function to help determine the max value in an array of integers, the call back function will be passed into the reduce function you will use for part B below. Part B.) Write the compare function, which will be passed into the sort function, the compare function must utilize the reduce function, which will determine the value of each sub array.
|
0

I have to use the findMax method in the compare method, and the compare method MUST use reduce.

Given these specifications, the only possible solution that makes any sense is extremely inefficient since it does not pre-compute the max of each sub-array before sorting, but regardless, here it is:

function compare(a, b) {
  return a.reduce(findMax, 0) - b.reduce(findMax, 0)
}

function findMax(max, curr) {
  return Math.max(max, curr)
}

var x = [ [2,5,1], [1,23], [3], [22,16,8] ]

x.sort(compare)

console.log(JSON.stringify(x))

Now, this only works when all your sub-arrays contain non-negative integers. If you must also sort with negative integers, I recommend replacing the 0 in the second argument of each .reduce() call with -Infinity.

Comments

Your Answer

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