Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
javascript if solution if x is greater than any of the array numbers

need a little help please for a solution I have an array numbers and 2 vars. if x is greater than any of the array numbers,then y becomes the next number...

var x = 9
var y = 0
var array = [8,12,16,20,24,28,32]

ex
x = 9
y = 12

ex
x = 17
y = 20

ex
is equal...
x = 24
y = 24
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Here is a one-line function variable that returns what you want or 0 if no no value in the array is greater than x.

const array = [8 ,12 ,16, 20, 24, 28, 32]

const getY = (x) => (array.find(elem => elem >= x) || 0)

console.log(getY(17)) // 20
about 4 years ago · Juan Pablo Isaza Report

0

Use this function .

    function findY(x,array){
    for(var i=0;i<array.length;i++)
    {
    if(x<=array[i]){
        y=array[i]
        return y
    }
        }}
   findY(x,array)
about 4 years ago · Juan Pablo Isaza Report

0

Well its not a 1 line function but it does in an easy explanatory way.

var x = 24
var y = 0
var array = [8,12,16,20,24,28,32]

for (let step = 0; step < array.length; step++) { 
  if(x==array[step]){
    y=x
   }else if(x>array[step] && step+1 <array.length ){  
    y = array[step+1];
   }
    
}
console.log(x,y)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!