0

Write a JavaScript program to build a word play. The function wordPlay must take a number greater than 0 & lesser than 51 as its argument and return the output in the following format.

When the argument is :

  1. greater than 50, the function must return : Range is High
  2. lesser than 1, the function must return : Not Valid
  3. within the specified range : the function must iterate through all the numbers within the range, append each of them to a string with a white-space & return the same.
  4. However, when a number divisible by both 5 & 3 is encountered, "Jump" is appended to the string; when a number divisible only by 3 is encountered, "Tap" is appended to the string; when a number divisible only by 5 is encountered, "Clap" is appended to the string (refer to the console outputs)

function wordPlay(number){
    if (number>50){
        console.log("Range is High");
    }
    else if(number<1) {
        console.log("Not Valid");
    }
    else if(number<=1 && number>=50) {
        for(var i=1;i<=number;i++)
        {
            if(number%5===0 && number%3===0){
                console.log("Jump");
            }
            else if(number%5===0){
                console.log("Clap");
            }
            else if(n%3===0)
            {
                console.log("Tap");
            }
            else
            {
                console.log("number");
            }
        }
    }
}
console.log(wordPlay(16));

Code is not working why can someone help?

Output should be:

1 2 Tap 4 Clap Tap 7 8 Tap Clap 11 Tap 13 14 Jump 16

function wordPlay(num) {
  if (num < 1) return "Not Valid";
  if (num > 50) return "Range is High";

  let str = "";

  for (i = 1; i <= num; i++) {
      if (i % 15 === 0) str += " Jump";
      else if (i % 3 === 0) str += " Tap";
      else if (i % 5 === 0) str += " Clap";
      else str += ` ${i}`;
  }

  return str;
}

console.log(wordPlay(16));

It is said to append space before any integer|Tap|Clap|Jump

So the output will be

' 1 2 Tap 4 Clap Tap 7 8 Tap Clap 11 Tap 13 14 Jump 16'
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Sim
  • 19
  • 1
  • 2
    `console.log(wordPlay(16));` doesn't make sense. `wordPlay` doesn't contain a return statement. `else if(number<=1 && number>=50)` doesn't make sense. A number can't be `<= 1` and `>= 50` at the same time. I guess you mean a simple `else`. – jabaa Feb 01 '22 at 16:19
  • 2
    Define "not working". What *is* happening? What were your thoughts about the difference(s) in behavior? – Dave Newton Feb 01 '22 at 16:19
  • @Sim Check your logic conditions; see if they make sense, and the program flow is what you expect. – Dave Newton Feb 01 '22 at 16:21
  • Looks like a slightly rebranded FizzBuzz test – phuzi Feb 01 '22 at 16:21
  • Fundamentally, it's because you're misusing the `&&` operator. For example, `number<=1 && number>=50` will never be true, because if `number<=1` is `true`, then `number>=50` is `false` by definition. But `&&` requires both of its operands to be true. You have a similar problem with `number%5===0 && number%3===0`. You're also not outputting the number when not outputting `Tap` / `Clap` (you're outputting the string `"number"`). And a couple of other basic logic things, like testing `number` rather than `i` in the loop. – T.J. Crowder Feb 01 '22 at 16:23
  • When trying to figure out why something isn't working, your first step should be to use the debugger built into your IDE and/or browser. Using the debugger isn't an advanced skill, it's basically the next thing users should learn to do after writing the equivalent of "Hello world." – T.J. Crowder Feb 01 '22 at 16:25
  • Can you please give me an updated code? I tried a lot but I'm getting undefined value. Please – Sim Feb 01 '22 at 17:01
  • Here is the answer: https://jsfiddle.net/xkvr20uL/ – Wimanicesir Feb 02 '22 at 08:24

0 Answers0