1

I have a variable with number and array. For example:

var arr = [10, 13, 17];
var number = 8;

I want to check if number is less then every value in arr and if true do something like this:

var arr = [10, 13, 17];
var number = 8;
if (number < arr[]) {
// do something
} else {
// do something
}

Could you please help me to solve this issue?

Sergi Khizanishvili
  • 537
  • 1
  • 7
  • 16

7 Answers7

7

JavaScript doesn't provide a way to do that for you, you have to do it yourself.

I want to check if number is less then any all values in arr...

It would be either "any value in arr" or "all values in arr". "any all values in arr" doesn't make sense.

If you mean "number is less than all values in arr", probably the easiest way is the every function on arrays:

if (arr.every(e => number < e)) {

If you mean "number is less than any value in arr", you want some instead:

if (arr.some(e => number < e)) {

One good thing about that is they short-circuit: As soon as every/some knows the result, it stops looping. For every, that means the first time the callback returns a falsy value; for some, that means the first time the callback returns a truthy value.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
4

var arr = [10, 13, 17];
var number = 8,
  number1 = 12;

if (number < Math.min(...arr)) {
  console.log("do something");
} else {
  console.log("do something else");
}

if (number1 < Math.min(...arr)) {
  console.log("do something");
} else {
  console.log("do something else");
}
bli07
  • 673
  • 1
  • 5
  • 16
1

How about the use of min() function?

Math.min(...arr);

should return the minimum value of the array. Its an ES6 function.

andreaslangsays
  • 329
  • 4
  • 7
  • 1
    *"Its an ES6 function."* No, it isn't. `Math.min` has been in JavaScript since the beginning (it's in the ECMAScript 1st edition standard, and I'm pretty sure it predates *that*). Array spread notation is ES2015 ("ES6"), perhaps that's what you meant. In ES5 it would be `Math.min.apply(Math, arr)` (though `Math.min.apply(null, arr)` works). – T.J. Crowder Nov 10 '19 at 16:20
  • Not to nitpick, but `...` isn't (and can't be) an operator. Operators can't do what spread and rest do. :-) – T.J. Crowder Nov 11 '19 at 07:49
  • Okay, you're right it does a lot a lot more than an operator... :-) But I know several coding instances that call it a "Spread operator" ;-) – andreaslangsays Nov 11 '19 at 17:15
0

Simple approach is to check for the minimum of an array and then compare your variable against that. If your variable is the lowest number then it will be equal to the minimum of an array.

var arr = [10, 13, 17];
var number = 8;

if(number > Math.min.apply(null, arr)) {
 console.log("at least one value is lower than your number");
} else {
 console.log("no value is lower than your number");
}
Harsh
  • 1,665
  • 1
  • 10
  • 16
  • 2
    This solution should work, but won't short-circuit. For example if `arr` has a million elements, but the first one is out of range, you shouldn't need to look at the rest of them. – kaya3 Nov 10 '19 at 16:14
0

Here are two examples on how to achieve that:

var arr = [10, 13, 17];
var number = 8;

// Old school version
const isSmallest = (n, list) => {
  let hasSmaller = false

  for (let i = 0; hasSmaller === false && i < list.length; i++) {
    hasSmaller = n > list[i];
  }

  return !hasSmaller;
}

// Array.prototype.every version
const isSmallest2 = (n, list) => {
  return list.every(item => n < item)
}

console.log(isSmallest(number, arr));
console.log(isSmallest2(number, arr));
giuseppedeponte
  • 2,366
  • 1
  • 9
  • 19
0

In your case you can use for..in, for of, map and filter. Lets see 2 examples.

E.g for of

var arr = [10, 13, 17];
var number = 8;

for(const element of arr) {
    if( number < element){
        // do something
        console.log(`${number} > ${element}`)
    }
    else {
        //do something
        console.log(`${number} <= ${element}`)
    }
}

E.g E.g map

var arr = [10, 13, 17];
var number = 8;

arr.map((element) => number < element ? console.log(`${number} > ${element}`) : console.log(`${number} <= ${element}`))
Mohiuddin
  • 1
  • 2
0

Here is an example for performance;

function isMin(num){
  for (let i = 0; i < arr.length; i++) {
    if (arr[i]<num){
      return false
    }
  }

  return true
}
Zubeyir
  • 11
  • 6