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.
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:
- The function
nearestTo250
takes two numbers as arguments,num1
andnum2
. - It calculates the absolute difference between 250 and each of the two numbers, storing these in
diff1
anddiff2
respectively. - It then compares
diff1
anddiff2
. Ifdiff1
is less thandiff2
, it meansnum1
is closer to 250, so it returnsnum1
. - If
diff2
is less thandiff1
, it meansnum2
is closer to 250, so it returnsnum2
. - If
diff1
anddiff2
are equal, it means both numbers are equally distant from 250, so it returns a message stating this.
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?
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.