Back to Freecodecamp

Challenge 224: Coffee Roast Detector

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69a890af247de743333bd4cd.md

latest2.0 KB
Original Source

--description--

Given a string representing the beans used to make a cup of coffee, determine the roast of the cup.

  • The given string will contain the following characters, each representing a type of bean:

    • An apostrophe (') is a light roast bean worth 1 point each.
    • A dash (-) is a medium roast bean worth 2 points each.
    • A period (.) is a dark roast bean worth 3 points each.
  • The roast level is determined by the average of all the beans.

Return:

  • "Light" if the average is less than 1.75.
  • "Medium" if the average is 1.75 to 2.5.
  • "Dark" if the average is greater than 2.5.

--hints--

detectRoast("''-''''''-'-''--''''") should return "Light".

js
assert.equal(detectRoast("''-''''''-'-''--''''"), "Light");

detectRoast(".'-''-''..'''.-.-''-") should return "Medium".

js
assert.equal(detectRoast(".'-''-''..'''.-.-''-"), "Medium");

detectRoast("--.''--'-''.--..-.--") should return "Medium".

js
assert.equal(detectRoast("--.''--'-''.--..-.--"), "Medium");

detectRoast("-...'-......-..-...-") should return "Dark".

js
assert.equal(detectRoast("-...'-......-..-...-"), "Dark");

detectRoast(".--.-..-......----.'") should return "Medium".

js
assert.equal(detectRoast(".--.-..-......----.'"), "Medium");

detectRoast("..-..-..-..-....-.-.") should return "Dark".

js
assert.equal(detectRoast("..-..-..-..-....-.-."), "Dark");

detectRoast("-'-''''''..-'.''-'.'") should return "Light".

js
assert.equal(detectRoast("-'-''''''..-'.''-'.'"), "Light");

--seed--

--seed-contents--

js
function detectRoast(beans) {

  return beans;
}

--solutions--

js
function detectRoast(beans) {
  let total = 0;

  for (const bean of beans) {
    if (bean === "'") total += 1;
    else if (bean === "-") total += 2;
    else if (bean === ".") total += 3;
  }

  const avg = total / beans.length;

  if (avg < 1.75) return "Light";
  if (avg <= 2.5) return "Medium";
  return "Dark";
}