Sunday, April 18, 2021

32. Write a JavaScript program to find a value which is nearest to 100 from two different given integer values

32. Write a JavaScript program to find a value which is nearest to 100 from two different given integer values.

function near_100(x, y) {
  if (x != y)
  {
  x1 = Math.abs(x - 100);
  y1 = Math.abs(y - 100);

  if (x1 < y1)
  {
    return x;
  }
  if (y1 < x1)
  {
    return y;
  }
  return 0;
  }
  else
    return false;
}

console.log(near_100(90, 89));
console.log(near_100(-90, -89));
console.log(near_100(90, 90));

No comments: