Skip to content

Instantly share code, notes, and snippets.

@gmittal
Created January 10, 2018 01:08
Show Gist options
  • Save gmittal/41442bbeafb83c9cdedcd3328d30dbbf to your computer and use it in GitHub Desktop.
Save gmittal/41442bbeafb83c9cdedcd3328d30dbbf to your computer and use it in GitHub Desktop.
/*
* http://paleyontology.com/AP_CS/rainfall.html
*/
let lyst = [-76, 0, -1, 1, 2, -999];
function rainfall(nums) {
nums = nums.splice(0, nums.indexOf(-999));
const positive = nums.filter(n => n >= 0);
return positive.reduce((total, n) => {
total += n;
return total
})/positive.length;
}
console.log(rainfall(lyst)); // returns 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment