Knowee
Questions
Features
Study Tools

Activity 6 - JavaScriptEunice Sison•Oct 720 pointsDue 11:59 PMFind a value which is nearest to 250 from two different given integer values.

Question

Activity 6 - JavaScript

Eunice Sison
•Oct
7 points
Due 11:59 PM

Find a value which is nearest to 250 from two different given integer values.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple JavaScript function that will find the value nearest to 250 from two given integer values:

function nearestTo250(num1, num2) {
    var diff1 = Math.abs(250 - num1);
    var diff2 = Math.abs(250 - num2);

    if (diff1 < diff2) {
        return num1;
    } else if (diff2 < diff1) {
        return num2;
    } else {
        return "Both numbers are equally distant from 250.";
    }
}

Here's how it works:

  1. The function nearestTo250 takes two numbers as arguments, num1 and num2.
  2. It calculates the absolute difference between 250 and each of the two numbers, storing these in diff1 and diff2 respectively.
  3. It then compares diff1 and diff2. If diff1 is less than diff2, it means num1 is closer to 250, so it returns num1.
  4. If diff2 is less than diff1, it means num2 is closer to 250, so it returns num2.
  5. If diff1 and diff2 are equal, it means both numbers are equally distant from 250, so it returns a message stating this.

This problem has been solved

Similar Questions

Activity 6 - JavaScriptEunice Sison•Oct 720 pointsDue 11:59 PMFind a value which is nearest to 250 from two different given integer values.

Given that y is the nearest integer of , find the value of ,where [.] denotes greatest common integer.

Find the points on the graph of the function that are closest to the given point.f(x) = x2,    (0, 6)

If the distance between the points (x, 2) and (-7, 18) is 20, then the value of x is1 point5 or -19-5 or 195 or 19-5 or -19

What is the minimum number of comparisons required in an array to find the minimum and maximum of 105 integers?

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.