Lovely JS example
by Rob Carroll
JavaScript
const buildingBase = 1000;
const decayRate = 0.01;
function strongEnough(earthquake, age) {
const earthquakeStrength = calcMagnitude(earthquake);
const buildingStrength = getStrength(age);
return (buildingStrength > earthquakeStrength)
? console.log('Safe!')
: console.log('Needs Reinforcement!');
}
function getStrength(age) {
return buildingBase * Math.pow(1 - decayRate, age);
}
function calcMagnitude(earthquake) {
return earthquake.reduce((acc, shockwave) => acc * calcShockwave(shockwave), 1);
}
function calcShockwave(shockwave) {
return shockwave.reduce((acc, n) => acc + n, 0);
}
strongEnough([[5,3,7],[3,3,7],[4,1,2]], 6);