js-mentorship-razvan / javascript

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

Ironman Triathlon #545

Closed RazvanBugoi closed 4 years ago

RazvanBugoi commented 4 years ago

https://www.codewars.com/kata/57d001b405c186ccb6000304/train/javascript

RazvanBugoi commented 4 years ago
function iTri(s){
  let totalDistance = 140.6; 
  if(s == 0) return 'Starting Line... Good Luck!';
  if(s <= 2.4) return {'Swim':`${(totalDistance - s).toFixed(2)} to go!`};
  if(s <= 114.4) return {'Bike':`${(totalDistance - s).toFixed(2)} to go!`};
  if(s <= 140.6 && (totalDistance - s) > 10) return {'Run':`${(totalDistance - s).toFixed(2)} to go!`};
  if(s <= 140.6 && (totalDistance - s) < 10) return {'Run':'Nearly there!'};
  return "You're done! Stop running!"
}

De optimizat solutia

RazvanBugoi commented 4 years ago
function iTri(s){
  let totalDistance = 140.6; 
  let arr = [{'Swim':`${(totalDistance - s).toFixed(2)} to go!`}, 
{'Bike':`${(totalDistance - s).toFixed(2)} to go!`},
{'Run':`${(totalDistance - s).toFixed(2)} to go!`},
{'Run':'Nearly there!'}
];
  if(s ==0 ) return 'Starting Line... Good Luck!';
  if(s <= 2.4) return arr[0];
  if(s <= 114.4) return arr[1];
  if(s <= 140.6 && (totalDistance - s) > 10) return arr[2];
  if(s <= 140.6 && (totalDistance - s) < 10) return arr[3];
  return "You're done! Stop running!";
}