Home > front end >  How to return a string which repeats the phrases ("go for it" , "don´t do it") a
How to return a string which repeats the phrases ("go for it" , "don´t do it") a

Time:02-17

example:

doIt(1) ➞ "DO IT"

doIt(3) ➞ "do it, don't do it, DO IT"

doIt(2) ➞ "do it, DON'T DO IT"

CodePudding user response:

const repeastPhrases = (num) => {
  const phrases = ["go for it", "don't do it"];
  let result = "";
  for (let i = 0; i < num; i  ) {
    result  = i === num - 1 ? phrases[0].toUpperCase() : `${phrases[0]}, ${phrases[1]}, `;
  }
  return result;
}
console.log(repeastPhrases(4));

CodePudding user response:

in javascript as in python the work with strings is super ease, try this(by the way, this is javascript):

let doit = 'do it';
        let dontdi = 'don\'t do it';
        
        function doIt(n){
            let i = 1;
            let phrase = '';
            
            while(i<=n){                
                if(i%2 != 0){                   
                    phrase  = (i==n)?(doit.toUpperCase()):(doit);
                    phrase  = ', ';
                }else{
                    phrase  = (i==n)?(dontdi.toUpperCase()):(dontdi);
                    phrase  = ', ';
                }
                
                i  ;
            }
            
            return phrase.slice(-phrase.lenght,-2);
        }
        
        console.log(doIt(2));//do it, DON'T DO IT
        console.log(doIt(3));//do it, don't do it, DO IT
        console.log(doIt(5));//do it, don't do it, do it, don't do it, DO IT
        console.log(doIt(8));//do it, don't do it, do it, don't do it, do it, don't do it, do it, DON'T DO IT
  • Related