Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

find a number that is closest to a specific number in javascript

var counts = [4, 9, 15, 6, 2],
  goal = 5;

var closest = counts.reduce(function(prev, curr) {
  return (Math.abs(curr - goal) < Math.abs(prev - goal) ? curr : prev);
});

console.log(closest);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to append select option in jquery 
Javascript :: console.log big red text 
Javascript :: vue-cli-service 
Javascript :: jquery get form data 
Javascript :: react native seperator code 
Javascript :: electron hide top bar 
Javascript :: javascript sort by numerical value 
Javascript :: javascript open url 
Javascript :: babel cdn react 
Javascript :: remove element by class 
Javascript :: node wait 10 seconds 
Javascript :: email validation using javascript 
Javascript :: Ignoring TypeScript Errors in next js 
Javascript :: how to remove the last character of a string in javascript 
Javascript :: isoddjs 
Javascript :: how to check if object has key javascript 
Javascript :: jquery scroll to bottom 
Javascript :: heroicons nextjs 
Javascript :: jqiery call onclick in another element 
Javascript :: if (!firebase.apps.length) { firebase.initializeApp({}); }else { firebase.app(); // if already initialized, use that one } 
Javascript :: history.push in nextjs 
Javascript :: border react native 
Javascript :: jquery onchange input value 
Javascript :: format percentage javacsript 
Javascript :: cep validator js 
Javascript :: get params in nuxtjs 
Javascript :: javascript foreach key value 
Javascript :: jquery toggle attribute disabled 
Javascript :: jquery get iframe content 
Javascript :: mui image 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =