0

For learning purpose, I would like to know how to check the empty numbers (or replaced with letters) from random numbers.

I put numbers and letters into an array:

var numb = [1, 7, 3, a, 4];

In this case we don't know the highest number or the lowest number.

I want to check the empty numbers starting from number 2 to the highest (result 2, 5, and 6).

5
  • 3
    I don't understand what you mean by "empty" numbers. Commented Oct 4, 2014 at 1:32
  • why don't you replace 'your empty number' (OR MAY BE UNDEFINED VALUE) from the array? then you can get the highest value Commented Oct 4, 2014 at 1:35
  • In this case I want to get number 2, 5, and 6 from the above array. Commented Oct 4, 2014 at 1:35
  • 1
    loop the array, check if the element/item is a 'number' or not, if not replace it! Commented Oct 4, 2014 at 1:36
  • A small suggestion: Next time try to show what you've tried (some code) to accomplish your task! Don't expect people to write code for you! ;) Happy coding! Commented Oct 4, 2014 at 1:52

1 Answer 1

3

Loop over your Array to find the maximum max, then iterate max - 1 - minumum times testing for the existence of numbers in the Array.

function foo(arr) {
    var bar = [],
        i,
        max = -Infinity;
    for (i = 0; i < arr.length; ++i) // loop 1
        if (arr[i] === +arr[i]) // simple check if number, throw away NaN
            if (arr[i] > max)
                max = arr[i];
    while (max-- > 2) // loop 2, you said 2 is minimum
        if (arr.indexOf(max) === -1)
           bar.unshift(max);
    return bar;
}

foo([1, 7, 3, 'baz', 4]); // [2, 5, 6]

I also assumed all your numbers will be integers.

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

2 Comments

max = -Infinity; why - ? Why Infinity at all
@RokoC.Buljan any number (except NaN and itself) is greater than -Infinity. Also, Math.max(); // -Infinity. Any loop where -Infinity is the highest iteration count will immediately stop.

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.