Skip to content

Instantly share code, notes, and snippets.

@teckliew
Created June 17, 2015 19:51
Show Gist options
  • Save teckliew/d9e8ddcba85fed46379b to your computer and use it in GitHub Desktop.
Save teckliew/d9e8ddcba85fed46379b to your computer and use it in GitHub Desktop.
The js solution to sum of all numbers from 1 to n
//my solution
function f(n){
return n > 0 && n % 1 === 0 ? ((1+n)*n)/2 : false;
};
//good examples
//1
function f(n){
return (parseInt(n) === n && n > 0) ? n*(n+1)/2 : false;
};
//2
function f(n){
return n > 0 && !(n % 1) ? n * ++n / 2 : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment