1

I have a function here that takes the smallest number in an array.

What I did is that I filtered out only numbers using typeof property and compared the values from Infinity.

Right now, it will return 0 if the array is empty.

However if the array contains only string or other datatypes it will return Infinity.

Here's my codes:

function findSmallestNumberAmongMixedElements(arr) {

  var smallestNum = Infinity;

  if(arr.length !== 0){
   for(var i = 0; i < arr.length; i++){
     if(typeof arr[i] === 'number' && arr[i] < smallestNum){
         smallestNum = arr[i];
     } 
   }
     return smallestNum;
  } 
  return 0;
}


var output = findSmallestNumberAmongMixedElements(['sam', 3, 2, 1]);
console.log(output); // --> 4

It must return 0 as well if there are no numbers in the array.

Any idea what am I doing wrong here?

7
  • Is it possible for the array to contain negative numbers? If not, you can just initialize smallestNum to 0 instead of Infinity. Commented Sep 1, 2017 at 3:56
  • 1
    findSmallestNumberAmongMixedElements(['sam', 3, 2, 1]) is returning 1 for me. Can you provide other test cases? Commented Sep 1, 2017 at 3:59
  • Your above example returns 1 not 4. Commented Sep 1, 2017 at 4:00
  • @NafiulIslam that's what it should return.. the smallest number among mixed elements. His issue was with input ['a','b'] returning infinity instead of 0. Commented Sep 1, 2017 at 4:06
  • 1
    Change return smallestNum; to return (smallestNum < Infinity) ? smallestNum : 0; Commented Sep 1, 2017 at 4:13

4 Answers 4

2

There are probably some more elegant ways to solve this. but this fixes your bug.

function findSmallestNumberAmongMixedElements(arr) {

  var smallestNum = Infinity;
  var numberFound = false

   for(var i = 0; i < arr.length; i++){
     if(typeof arr[i] === 'number' && arr[i] < smallestNum){
         smallestNum = arr[i];
         numberFound = true
     } 
   }
    if(numberFound)
      return smallestNum;

  return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

What does arr[i] < smallestNum actually mean here? and why it is required?
@brk that checks if the current number is smaller than anything else we've seen.
0

The problem is that you are special-casing the empty-array case, with the line

if(arr.length !== 0){

Remove that. Then, if you want to force a result of Infinity to 0, do that at the end.

function findSmallestNumberAmongMixedElements(arr) {

  var smallestNum = Infinity;

  for(var i = 0; i < arr.length; i++){
    if(typeof arr[i] === 'number' && arr[i] < smallestNum){
       smallestNum = arr[i];
    } 
  }

  return isFinite(smallestNum) ? smallestNum : 0;
}

However, it is simpler to just filter out the non-numbers using filter, and calculate the minimum using Math.min. This also makes easier to fix a "bug" in the code above, which is that it will yield 0 for inputs such as [Infinity, "foo", Infinity]. I'm not sure whether you prefer to return 0 or Infinity in that kind of case. Assuming you do want to return 0, then

function findSmallestNumberAmongMixedElements(arr) {
  var nums = ...arr.filter(elt => typeof elt === 'number');

  return nums.length ? Math.min(...nums) : 0;
}

2 Comments

This solution is incorrect. The expected output for input: [Math.pow(10, 1000)] should be Infinity and this returns 0.
@gidim Thanks for your comment. It's not clear to me what the OP intends in that case. I've left a comment on his question asking him to clarify that. In the meantime, I've fixed the second solution using Math.min to operate as you suggest it should.
0
function findSmallestNumberAmongMixedElements(arr) {

  var smallestNum = Infinity;

  if(arr.length !== 0){
   for(var i = 0; i < arr.length; i++){
     if(typeof arr[i] === 'number' && arr[i] < smallestNum){
         smallestNum = arr[i];
     } 
   }
     return smallestNum == Infinity? 0 : smallestNum; // if smallest doesn't change return 0
  } 
  return 0;
}


var output = findSmallestNumberAmongMixedElements(['sam', 3, 2, 1]);
console.log(output);

2 Comments

this returns the wrong answer for [Math.pow(10, 1000)]. The correct answer is Infinity and your function returns 0...
Why is the correct answer Infinity? The function is supposed to return the smallest number in the array, how is Infinity the smallest number in Array(Math.pow(10, 1000))?
-1

You could use an odd way of using Array#reduce and Array#filter

First, filter out non-numeric

Second reduce this filtered array, with an initial value of 0 - if the array is zero length, reduce will return 0

function findSmallestNumberAmongMixedElements(arr) {
    var smallestNum = Infinity;
    return arr.filter(item => typeof item == 'number').reduce((min,item) => {
        if(item < smallestNum) smallestNum = item;
        return smallestNum;
    }, 0);
}
console.log(findSmallestNumberAmongMixedElements([]));
console.log(findSmallestNumberAmongMixedElements(['1','2','3']));
console.log(findSmallestNumberAmongMixedElements([1,2,3]));
console.log(findSmallestNumberAmongMixedElements(['1',2,3]));

1 Comment

Would be rather polite if the downvoter explained the downvote on code that produces the required output as per the request in the question

Your Answer

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