Back to Freecodecamp

Challenge 135: Re: Fwd: Fw: Count

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

latest1.7 KB
Original Source

--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.

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

emailChainCount("Meeting Notes") should return 0.

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

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

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

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

js
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.

js
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--

js
function emailChainCount(subject) {

  return subject;
}

--solutions--

js
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;
}