js-mentorship-razvan / javascript

Javascript study notes
GNU General Public License v3.0
22 stars 2 forks source link

Solve 'Difference of Volumes of Cuboids' #315

Closed odv closed 5 years ago

odv commented 5 years ago

https://www.codewars.com/kata/difference-of-volumes-of-cuboids/train/javascript

RazvanBugoi commented 5 years ago
function findDifference(a, b) {
  return a.reduce((a,b) => a * b) > b.reduce((a,b) => a * b) ? a[0]*a[1]*a[2] - b[0]*b[1]*b[2] : b[0]*b[1]*b[2] - a[0]*a[1]*a[2];
}

Improved solution using Math.abs method

function findDifference(a, b) {
  return Math.abs(a.reduce((a,b) => a * b) - b.reduce((a,b) => a * b));
}