Skip to content
Advertisement

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

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement