Files
freeCodeCamp/curriculum/challenges/english/blocks/daily-coding-challenges-javascript/691f7773cddba1caf1bf5ece.md

1.8 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
691f7773cddba1caf1bf5ece Challenge 135: Re: Fwd: Fw: Count 28 challenge-135

--description--

Given a string representing the subject line of an email, determine how many times the email has been forwarded or replied to.

For simplicity, consider an email forwarded or replied to if the string contains any of the following markers (case-insensitive):

  • "fw:"
  • "fwd:"
  • "re:"

Return the total number of occurrences of these markers.

--hints--

emailChainCount("Re: Meeting Notes") should return 1.

assert.equal(emailChainCount("Re: Meeting Notes"), 1);

emailChainCount("Meeting Notes") should return 0.

assert.equal(emailChainCount("Meeting Notes"), 0);

emailChainCount("Re: re: RE: rE: Meeting Notes") should return 4.

assert.equal(emailChainCount("Re: re: RE: rE: Meeting Notes"), 4);

emailChainCount("Re: Fwd: Re: Fw: Re: Meeting Notes") should return 5.

assert.equal(emailChainCount("Re: Fwd: Re: Fw: Re: Meeting Notes"), 5);

emailChainCount("re:Ref:fw:re:review:FW:Re:fw:report:Re:FW:followup:re:summary:Fwd:Re:fw:NextStep:RE:FW:re:Project:Fwd:Re:fw:Notes:RE:re:Update:FWD:Re:fw:Summary") should return 23.

assert.equal(emailChainCount("re:Ref:fw:re:review:FW:Re:fw:report:Re:FW:followup:re:summary:Fwd:Re:fw:NextStep:RE:FW:re:Project:Fwd:Re:fw:Notes:RE:re:Update:FWD:Re:fw:Summary"), 23);

--seed--

--seed-contents--

function emailChainCount(subject) {

  return subject;
}

--solutions--

function emailChainCount(subject) {
  const markers = ["re:", "fwd:", "fw:"];
  const lower = subject.toLowerCase();
  let count = 0;

  for (const marker of markers) {
    let index = 0;
    while ((index = lower.indexOf(marker, index)) !== -1) {
      count++;
      index += marker.length;
    }
  }

  return count;
}