Challenge 14 Megathread

Welcome to challenge 14! Use this thread for any and all questions relating to challenge 14.

1 Like

I got my solution like this:

solution reveal

image

7 Likes

How I solved this one. There is probably a prettier way, but this was my last nocturnal synaptic event before my head hit the pillow like it should’ve done hours ago :grin:

Solution
const dynamicPricing = (numberOfPeople, distanceTraveled) => {
    const REGULAR_FEE = distanceTraveled * 0.25 + 1;
    const CROWDED_FEE = numberOfPeople >= 30 ? 0.25 : 0;
    return `$${(REGULAR_FEE + CROWDED_FEE).toFixed(2)}`;
}
5 Likes

I did this

const dynamicPricing = (numberOfPeople, distanceTraveled) => {
  let basePrice = 1 + distanceTraveled*0.25;
  return numberOfPeople >= 30 ? `$${(0.25 + basePrice).toFixed(2)}` : `$${basePrice.toFixed(2)}`;
}
5 Likes

i used this and one test is passing but i am not sure how to pass the second test as i have to use a conditional statement and then return it again. What am i doing wrong?

return numberOfPeople.toFixed(distanceTraveled);

Your solution is elegant.
To improve upon it, perhaps you can do without the first return statement as the last one takes care of both cases.

In the brackets of the .toFxed() method, you want to write how many decimals you would like to see.

1 Like

To keep things clean, you should avoid mixing single and double quotes. Also, you’re using type cohesion to get the price number into a string. Personally I would either place everything between backtics or add a toString() method to the end. Just to convey that you intended to change the type. :slight_smile:

Why ‘$$’ and not ‘$’?

2 Likes

Hello!
Here is my solution. I’m a beginner and it’s not too fancy, but I figured it out myself and understand it :slight_smile: #proud

Solution

57%20AM

6 Likes

${something} is actually one piece (it will inject the variable something into the string).
The first $ is the actual dollar sign in the string:

         start of variable injection
         ▼
return `$${price}`
        ▲
        actual dollar sign
2 Likes

Figured out a one liner for my solution, let me know what you folks think.

Solution (one line)

3 Likes

I really like these problems that can be reduced to a single line. This was my approach:

const dynamicPricing = (numberOfPeople, distanceTraveled) => {
  return `$${(1 + distanceTraveled * 0.25 + (numberOfPeople > 29) * 0.25).toFixed(2)}`;
};
1 Like
const dynamicPricing = (numberOfPeople, distanceTraveled) => {
  const basePrice = 1;
  const chargePerKm = 0.25;
  const threshold = 30;
  const thresholdAddition = 0.25;

  const totalPrice =
    basePrice +
    chargePerKm * distanceTraveled +
    (numberOfPeople >= threshold ? thresholdAddition : 0);

  return "$" + totalPrice.toFixed(2);
};
  1. Do not use “magic numbers” that nobody knows what they are
  2. Keep it readable and editable as opposed to “clever one-liner”

This is a “clever one-liner”, but it is unreadable:

const dynamicPricing = (numberOfPeople, distanceTraveled) =>
  "$" +
  (1 + 0.25 * distanceTraveled + (numberOfPeople >= 30 ? 0.25 : 0)).toFixed(2);
2 Likes

A concatenate-based solution:

Solution
  if(numberOfPeople >= 30){
    Price = 1 + 0.25*distanceTraveled + 0.25
  } else {
    Price = 1 + 0.25*distanceTraveled
  }
  
  return "$".concat(Price.toFixed(2))  // .toFixed function to specify decimal places

“Concatenate” is to squish together into a string. In this case “$” with Price to two decimal points

2 Likes

This is good, solid and production-ready code. The fancy “one-liners” with “magic numbers” are a nightmare to maintain. Keep up the good work!

const dynamicPricing = (numberOfPeople, distanceTraveled) => {
  const cost = 1 + (0.25 * distanceTraveled) + (numberOfPeople >= 30 && 0.25);
  return `$${cost.toFixed(2)}`;
}

Here is my answer!

1 Like

I am not sure what I am doing wrong here.

function dynamicPricing (numberOfPeople, distanceTraveled)  {
  // Code here!
 var Prices = [];
 var CostPerKm = .25;
 var PeakPremium = .25;
 var NonPeakPrice = (1+(distanceTraveled*CostPerKm)); 
 var PeakPrice = NonPeakPrice + PeakPremium;
 var PeakRider = 30;
 
 for (var Price of Prices) {
 if (numberOfPeople >= PeakRider)
 return "$"+toString(PeakPrice.toFixed(2));
 else 
 return "$"+toString(NonPeakPrice.toFixed(2));}
}