Home > Net >  Countdown won't return "Kaboom!" when finished
Countdown won't return "Kaboom!" when finished

Time:04-10

Can someone please tell me what I did wrong and point me in the right direction? It should countdown from 5, 4, 3, 2, 1, Kaboom!

Problem to solve

Here is my code:

let count = 6;
let message = "Start over.";

const handleRequest = (request) => {
  const url = new URL(request.url);

  if (url.pathname.includes("count") && count > 0) {
    count--;
    return new Response(count);
  } if (url.pathname.includes("count") && count <= 0) {
      return new Response("Kaboom!");
  } else {
    return new Response(message);
  }
};

I get this error:

test output: 
Countdown is 5, 4, 3, 2, 1, Kaboom!
AssertionError: Values are not equal:


[Diff] Actual / Expected

-   0
    Kaboom!

Thanks in advance.

CodePudding user response:

The task is to return 'Kaboom!' as the sixth event. Contrary, your code returns 0 because it still enters the first if clause when count is 1.

You need to adapt your conditional logic to make sure 'Kaboom!' is returned after the count of 1 has been returned. Following should work:

let count = 6;
let message = "Start over.";

const handleRequest = (request) => {
  const url = new URL(request.url);

  if (url.pathname.includes("count") && count > 1) {
    count--;
    return new Response(count);
  } if (url.pathname.includes("count") && count <= 1) {
      return new Response("Kaboom!");
  } else {
    return new Response(message);
  }
};
  • Related