loading...

re: Daily Challenge #87 - Pony Express VIEW POST

TOP OF THREAD FULL DISCUSSION
re: My solution in js const riders = (stations = []) => { let numOfRiders = + !!stations.reduce((acc, distance) => acc + distance, 0), totalD...
 

Nice your solution is a little bit on an expert level here my JS solution

function riders(stations) {
  // check if stations is defined
  if(stations) {
    let distanceRode = 0
    let numberOfRiders = 0

    stations.forEach(distance => {
      distanceRode += distance
    });

    //Check if there is anything passed within the array 
    if(distanceRode == 0)
      return 0

    //modulus division
    let i = distanceRode%100

    //check if distance is greater than 100
    if (distanceRode >= 100) {
      if(i == 0) {
       /* 
         if distance is greater than 100 
         and modulus division is 0 then simply divide by 
         100 to get distance rode
       */
       numberOfRiders = distanceRode/100
      } else {
        /* 
         if distance is greater than 100 
         and modulus division is not 0 then simply divide by 
         100 to get number of riders and set the remainder as an extra rider
      */
        numberOfRiders = Math.floor(distanceRode/100) + 1
      }
    }
    return numberOfRiders
  } else {
    return 0
  }
}

console.log(riders()); // 0
console.log(riders([])); // 0
console.log(riders([0, 0, 0, 0, 0])); // 0
console.log(riders([43,23,40,13])) //2
console.log(riders([34,56,12,67,23,90,56])) //4
console.log(riders([56,67,100,345,12,45])) //7
console.log(riders([100, 100, 100, 50, 50, 30, 20, 25, 25, 25, 25])); //6

 

Good approach. Taking the total and dividing by 100 to see how many riders.

code of conduct - report abuse