0
function change(o)                                      
{                                       
  var str = "";                                     
  for (var p = 0; p<=o ;p++)                                        
  {                                     
    temp = p;                                       
    str = str + temp;                                       
    if (p == o){                                        
    }                                       
    else{                                       
      str = str + ",";                                      
    }                                       
  }                                     
console.log(str);                                   
}                                       
change(5);   

(consolelog will be changed into return, it's just to view the output)

Basically, I am a beginner who recently started and I wanted to make a simple function that returns a value of the function. For example; if change(5) was inputted, it would return a string value of 5 x's, so xxxxx. If it was change(7), it would return me xxxxxxx, so any idea how to do this? Because my code only gives value from 0 to my inputted value.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Deerosion
  • 1
  • 1
  • Possible duplicate of [Repeat Character N Times](https://stackoverflow.com/questions/1877475/repeat-character-n-times) – theduck Dec 06 '17 at 11:59

6 Answers6

4

You could declare a new Array and use Array.prototype.join, similar to the below:

function change(o)                                      
{                                       
  return new Array(o+1).join('x')
}             

console.log(change(5));
console.log(change(9));

Or if you don't have to support Internet Explorer you could use String.prototype.repeat(), similar to the below:

function change(o)                                      
{                                       
  return 'x'.repeat(o);
}             

console.log(change(5));
console.log(change(9));
Nope
  • 22,147
  • 7
  • 47
  • 72
  • ...only if you don't use IE. – 31piy Dec 06 '17 at 11:56
  • @31piy Nice catch, I added an alternative. – Nope Dec 06 '17 at 12:08
  • Nice to see the answer which is not a mindless copy paste of other answers but actually. – dfsq Dec 06 '17 at 12:12
  • @dfsq I had `.repeat()` originally but didn't notice the lack of IE support. Added `Array.join` as an alternative and then made it the preferred solution due to it's compatibility though I see now someone already suggested that. Honestly didn't see that. – Nope Dec 06 '17 at 12:16
1

You can do like this

function change(o)                                      
{ 
  if(o>0){
    console.log(Array(parseInt(o)+1).join("x"));
  }else{
    console.log("Blank");
  }
}
<input type="number" onkeyup="change(this.value)">
freelancer
  • 1,174
  • 5
  • 12
0
function change(count){
   var string = ""; // declare an empty string;
   for(var i=0; i<count; i++){ 
      string = string + "x"; // keep adding an x in each iteration to the string

   }
   return string; // return the string 
}

change(5); // gives xxxxx
blueren
  • 2,730
  • 4
  • 30
  • 47
0

function createX(num) {
  var str = '';
  for(i=0; i<num; i++){
    str += 'x';
  }
  return str;
}

console.log(createX(5));

Something like this?

Madhavan.V
  • 825
  • 1
  • 6
  • 14
0

I suppose you want this:

function change(o)                                      
{                                       
  var str = "";                                     
  for (var p = 0; p<o ;p++)                                        
  {                                     
    str += 'x';                  
  }                                     
  console.log(str);                                   
}         

change(5); 
Sascha
  • 4,576
  • 3
  • 13
  • 34
0

Here is how you can do it:

function change(o)                                      
{                                       
  var str = "";                                     
  for (var p = 0; p<o ;p++)                                        
  {                                                                            
    str = str + "x";                                                              
  }                                     
console.log(str);                                   
}                                       
change(5);   
Vinod Bhavnani
  • 2,185
  • 1
  • 16
  • 19