Cognizant HandsOn - Word Play - Operators, Conditional Control Statements & Loops.

 

Word Play - Operators, Conditional Control Statements & Loops

 

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)

Note: You may verify the correctness of your function by invoking it from within console.log

 

console.log(wordPlay(16));

Console Output 1:

 

 

console.log(wordPlay(-16));

Console Output 2:

 

 

console.log(wordPlay(166));

Console Output 3:


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

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.