1

I'm working on some coderbyte code, and noticed that when I try to get the max item in an array of equal values undefined is returned. When logging the min value is logs 80 and not undefined. Why is this?

Updated Code:

function noRepeat(arr) {
    tmp = []
    if (arr.length === 2 && arr[0] === arr[1]) {
        return arr;
    }
    for (var i = 0;i<arr.length;i++) {
        if (tmp.indexOf(arr[i]) === -1) {
            tmp.push(arr[i])
        }
    }
    return tmp
}
function SecondGreatLow(arr) {
    arr = noRepeat(arr).sort(function (a,b) {return a-b;});
    var low = arr[arr.indexOf(Math.min.apply(Math,arr))+1];
    console.log("low",low);
    var high = arr[arr.indexOf(Math.max.apply(Math,arr))-1];
    console.log("high",high);
    return low +" "+high;
}
console.log(SecondGreatLow([80,80]));

Output:

"low" 80 
"high" undefined
"80 undefined"
4
  • 1
    Ahhh I literally figured this out right after posting... So the max method is working. It's returning the first instance of 80. When it tries to subtract 1 from index 0 the result is undefined because there is no value. :) Commented Jun 4, 2015 at 3:44
  • Exactly. Why are you trying to get the next \ previous item? Commented Jun 4, 2015 at 3:46
  • arr is sorted, why not use arr[0] and arr[arr.length - 1] to get low/high ? Commented Jun 4, 2015 at 3:48
  • Here is the problem set: Have the function SecondGreatLow(arr) take the array of numbers stored in arr and return the second lowest and second greatest numbers, respectively, separated by a space. For example: if arr contains [7, 7, 12, 98, 106] the output should be 12 98. The array will not be empty and will contain at least 2 numbers. It can get tricky if there's just two numbers! Commented Jun 4, 2015 at 3:49

1 Answer 1

1

That's, actually, ok. How do you want to find the second largest \ smallest number in an array of two similar numbers? It should output "no solution" or something else. Just like there is no solution for an empty array.

function SecondGreatLow(arr) 
{
    arr = noRepeat(arr).sort(function (a,b) {return a-b;});
    if (arr.length < 2)
        return "No solution";
    console.log("low ", arr[1]);
    console.log("high ", arr[arr.length - 2]);
    return low + " " + high;
}

You don't need Math min and max functions as your array is sorted and values are unique. You need to take the second from beginning and the second from the end.

Also, you don't need this part as it is calculated right by algorithm.

if (arr.length === 2) 
{
    return arr[1] + " " + arr[0];
}

For example, you have an array [1, 1, 2].
You remove repetitions and get [1, 2].
Now your algorithms returns low = arr[1] = 2 and high = arr[2 - 2] = arr[0] = 1.
The answer is correct - 2 is the second minimum number and 1 is the second largest.

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

1 Comment

Got it. That makes sense. I forgot to add that sometimes the array could be 2 values that are different. So if it were [12,39] I'd need the if statement "if (arr.length === 2 && arr[0] === arr[1])" for it to work as intended. Thanks for your help.

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.