Reputation:
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?
Upvotes: 1
Views: 1866
Reputation: 2323
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;
}
Upvotes: 2
Reputation:
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;
}
Upvotes: 0
Reputation: 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]));
Upvotes: -1
Reputation: 1181
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);
Upvotes: 0