A function that can be used in JavaSccript to get the lower, higher, and closest number. The function receives two parameters: an array (or object) with numbers, and the number. Compares that number with the numbers from array /object, than returns an object with three elements: the "lower", "higher", and "closest" number.
Here's the function:
// retutrs an object with the number from "nums": lower, higher and closest to nr
// "nums" can be an Array with numbers [-2, 0, 3], or an object {'n1': -1, 'n2': 1, 'n3': 2}
function closestLowerHigherNr(nums, nr) {
 // JavaScript & jQuery Course - https://coursesweb.net/javascript/
  // Gets into an Array the values from "nums" array /object, and sorts from lowest to highest
  var vnums = [];
  for each(var val in nums) vnums.push(val);
  vnums.sort(function(a,b){return a - b;});

  var nr_vnums = vnums.length;      // number of elements in vnums

  // sets the object that will be returned, initially with the lowest and highest number from vnums
  var re_obj = {'lower': Math.min(vnums[0], nr), 'higher': Math.max(vnums[nr_vnums-1], nr), 'closest': nr};

  // traverse the numbers, stores in re_obj the number immediately lower and higher than nr
  for(var i=0; i<nr_vnums; i++){
    if(nr > vnums[i]) re_obj['lower'] = vnums[i];
    else if(nr <= vnums[i]){
      // if the current number from vnums is equal to nr, or immediately higher, stores that number
      // and stops the for each() loop
      re_obj['higher'] = vnums[i];
      break;
    }
  }

  // here it gets the closest number to nr
  // (the number ('lower' or 'higher') with the lowest diferrence between its value and nr)
  re_obj['closest'] = (Math.abs(nr - re_obj['lower']) < Math.abs(re_obj['higher'] - nr)) ? re_obj['lower'] : re_obj['higher'];

  return re_obj;
}
Example usage closestLowerHigherNr() function.
<script type="text/javascript"><!--
// retutrs an object with the number from "nums": lower, higher and closest to nr
// "nums" can be an Array with numbers [-2, 0, 3], or an object {'n1': -1, 'n2': 1, 'n3': 2}
function closestLowerHigherNr(nums, nr) {
 // JavaScript & jQuery Course - https://coursesweb.net/javascript/
  // Gets into an Array the values from "nums" array /object, and sorts from lowest to highest
  var vnums = [];
  for each(var val in nums) vnums.push(val);
  vnums.sort(function(a,b){return a - b;});

  var nr_vnums = vnums.length;      // number of elements in vnums

  // sets the object that will be returned, initially with the lowest and highest number from vnums
  var re_obj = {'lower': Math.min(vnums[0], nr), 'higher': Math.max(vnums[nr_vnums-1], nr), 'closest': nr};

  // traverse the numbers, stores in re_obj the number immediately lower and higher than nr
  for(var i=0; i<nr_vnums; i++){
    if(nr > vnums[i]) re_obj['lower'] = vnums[i];
    else if(nr <= vnums[i]){
      // if the current number from vnums is equal to nr, or immediately higher, stores that number
      // and stops the for each() loop
      re_obj['higher'] = vnums[i];
      break;
    }
  }

  // here it gets the closest number to nr
  // (the number ('lower' or 'higher') with the lowest diferrence between its value and nr)
  re_obj['closest'] = (Math.abs(nr - re_obj['lower']) < Math.abs(re_obj['higher'] - nr)) ? re_obj['lower'] : re_obj['higher'];

  return re_obj;
}

// Example
var numbers = [-8, -3, 0, 5.8, 12, 9, 2.1];
var test1 = closestLowerHigherNr(numbers, -6);
var test2 = closestLowerHigherNr(numbers, 3);

// the numbers are strored into an object
var nums_obj = {'n1':-8, 'n2':0, 'n3':12, 'n4':5.8, 'n5':9, 'n6':2.1};
var test3 = closestLowerHigherNr(nums_obj, 9);

// see the values in test1, test2, test3
alert('lower: '+test1.lower+ ', higher: '+test1.higher+ ', closest: '+ test1.closest);
  // Result: lower: -8, higher: -3, closest: -8

alert('lower: '+test2.lower+ ', higher: '+test2.higher+ ', closest: '+ test2.closest);
  // Result: lower: 2.1, higher: 5.8, closest: 2.1

alert('lower: '+test3.lower+ ', higher: '+test3.higher+ ', closest: '+ test3.closest);
  // Result: lower: 5.8, higher: 9, closest: 9
//-->
</script>
This function works also if "nr" is lower, or higher than any number in array /object.
Example:
<script type="text/javascript"><!--
// here add the closestLowerHigherNr() function

var numbers = [-8, 2, 12];
var test4 = closestLowerHigherNr(numbers, -23);      // -23 is lower than any number in "numbers"
var test5 = closestLowerHigherNr(numbers, 18);       // 18 is higher than any number in "numbers"

// see the values in test4, test5
alert('lower: '+test4.lower+ ', higher: '+test4.higher+ ', closest: '+ test4.closest);
  // Result: lower: -28, higher: -8, closest: -23
alert('lower: '+test5.lower+ ', higher: '+test5.higher+ ', closest: '+ test5.closest);
  // Result: lower: 12, higher: 18, closest: 12
//-->
</script>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute indicates that an input field must be filled out before submitting?
required="required" autofocus="autofocus" placeholder="text"
<input type="text" name="name1" required="required" />
Which CSS method increases or decreases the size of the HTML element (including its content)?
translate() scale() skew()
#some_id:hover {
  transform: scale(2, 1.5);
  -ms-transform: scale(2, 1.5);    /* IE 9 */
  -moz-transform: scale(2, 1.5);   /* Firefox */
}
Click on the Date object method that returns the day of the month.
setDate() getMonth() getDate()
var rightnow = new Date();
var day = rightnow.getDate();
alert(day);
Which function is used to upload a file on server?
is_file() move_uploaded_file() fopen()
if(move_uploaded_file($_FILES["field_name"]["tmp_name"], "dir/file_name")) {
  echo "The file succesfully uploaded";
}
Get Lower, Higher and Closest Number in JavaScript

Last accessed pages

  1. Register and show online users and visitors (25529)
  2. Dynamic variables in JavaScript (6831)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (12024)
  4. Read Excel file data in PHP - PhpExcelReader (61000)
  5. JQZoom Image Magnifier (9941)

Popular pages this month

  1. Insert, Select and Update NULL value in MySQL (417)
  2. Read Excel file data in PHP - PhpExcelReader (332)
  3. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (260)
  4. PHP getElementById and getElementsByTagName (210)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (199)