Back to Freecodecamp

Challenge 222: Equinox Shadows

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/699c8e045ee7cb94ed2322dd.md

latest2.5 KB
Original Source

--description--

Today is the equinox, when the sun is directly above the equator and perfectly overhead at noon. Given a time, determine the shadow cast by a 4-foot vertical pole.

  • The time will be a string in "HH:MM" 24-hour format (for example, "15:00" is 3pm).
  • You will only be given a time in 30 minute increments.

Rules:

  • The sun rises at 6am directly "east", and sets at 6pm directly "west".
  • A shadow always points opposite the sun.
  • The shadow's length (in feet) is the number of hours away from noon, cubed.
  • There is no shadow before sunrise (before 6am), after sunset (6pm or later), or at noon.

Return:

  • If a shadow exists, return "(length)ft (direction)". For example, "8ft west".
  • Otherwise, return "No shadow".

For example, given "10:00", return "8ft west" because 10am is 2 hours from noon, so 2<sup>3</sup> = 8 feet, and the shadow points west because the sun is in the east at 10am.

--hints--

getShadow("10:00") should return "8ft west".

js
assert.equal(getShadow("10:00"), "8ft west");

getShadow("15:00") should return "27ft east".

js
assert.equal(getShadow("15:00"), "27ft east");

getShadow("12:00") should return "No shadow".

js
assert.equal(getShadow("12:00"), "No shadow");

getShadow("17:30") should return "166.375ft east".

js
assert.equal(getShadow("17:30"), "166.375ft east");

getShadow("05:00") should return "No shadow".

js
assert.equal(getShadow("05:00"), "No shadow");

getShadow("06:00") should return "216ft west".

js
assert.equal(getShadow("06:00"), "216ft west");

getShadow("18:00") should return "No shadow".

js
assert.equal(getShadow("18:00"), "No shadow");

getShadow("07:30") should return "91.125ft west".

js
assert.equal(getShadow("07:30"), "91.125ft west");

getShadow("00:00") should return "No shadow".

js
assert.equal(getShadow("00:00"), "No shadow");

--seed--

--seed-contents--

js
function getShadow(time) {

  return time;
}

--solutions--

js
function getShadow(time) {
  const [hourStr, minuteStr] = time.split(":");
  const hours = Number(hourStr);
  const minutes = Number(minuteStr);
  const timeInHours = hours + minutes / 60;

  if (timeInHours < 6 || timeInHours >= 18 || timeInHours === 12) {
    return "No shadow";
  }

  const hoursFromNoon = Math.abs(12 - timeInHours);
  const length = Math.pow(hoursFromNoon, 3);
  const direction = timeInHours < 12 ? "west" : "east";

  return `${length}ft ${direction}`;
}