Good day! Please tell me how to search for an approximate number. Suppose there is an array [1 5 2 4 9], the required number is 6, if it is not in the array, then you need to find a close smaller one (4). Search is carried out in the array of old in the field of olds.

<input type="text" placeholder="Возраст" id="age"><br> <input type="text" placeholder="Зрелость" id="maturity"><br> <button onclick="inputData()">Обучить</button> <hr> <input type="text" placeholder="Введите свой возраст" id="myAge"><br> <button onclick="myMaturity()">Узнать свою зрелость</button> var old = [ { olds: 1, mature: "Молодой" }, { olds: 30, mature: "Зрелый" }, { olds: 60, mature: "Старый" } ]; function inputData() { var a = document.getElementById('age').value; var b = document.getElementById('maturity').value; old.push({ olds: a, mature: b }); console.log(old); } function myMaturity () { var a1 = document.getElementById('myAge').value; for (var key in old) { if (old[key].olds <= a1) { console.log(old[key].mature); } } } 
  • What does an approximate number mean? - Grundy
  • Suppose there is an array [1 5 2 4 9], the required number is 6, if it is not in the array, then you need to find close to it (these are 4 and 5). - user220343
  • and which of these two numbers should be returned? 4 or 5 or both? - Grundy
  • In this case, it would be necessary to find 4, inattentively described the question when he wrote the topic, excuse me. - user220343
  • edit it with the edit button. - Grundy

2 answers 2

 var delta = 999, itemId = -1; for (var key in old) { if (Math.abs(old[key].olds - a1) < delta) { itemId = key; delta = Math.abs(old[key].olds - a1); } } var near = old[itemId]; // <= тот элемент, который нам нужен 
  • Thank you very much! - user220343
  • Sorry, I didn’t ask immediately, but could you explain why the delta is used? - user220343
  • @ user220343, from the code it is visible. To search for the smallest value between age and an array of ages. Where the difference is smaller, then the value and closer - ArchDemon
  • Thank you for the answer, I thought so, but I wanted to make sure my argument was correct. - user220343

if the array is sorted, then the last element satisfying the condition old [key] .olds <= a1 will be the required one

  var old = [{ olds: 1, mature: "Молодой" }, { olds: 30, mature: "Зрелый" }, { olds: 60, mature: "Старый" }]; function inputData() { var a = document.getElementById('age').value; var b = document.getElementById('maturity').value; old.push({ olds: a, mature: b }); } function myMaturity() { var a1 = document.getElementById('myAge').value; var mature = old.filter(a => a.olds <= a1).sort((a, b) => a.olds - b.olds).pop().mature; console.log(mature); } 
 <input type="text" placeholder="Возраст" id="age"> <br> <input type="text" placeholder="Зрелость" id="maturity"> <br> <button onclick="inputData()">Обучить</button> <hr> <input type="text" placeholder="Введите свой возраст" id="myAge"> <br> <button onclick="myMaturity()">Узнать свою зрелость</button>