Basically i have literally just started learning Java Script and i was seeking some help as to how i could repeatedly display the character "X" any given number of times using a function.

Here is what i have so far:

function makex(8)
{
  var resultStr = "";
  for (var xcount = 0; xcount < makex(8); xcount++)
  {
    resultStr = resultStr + 1;  // this line was an experiment so could be completely wrong?
  }
  return resultStr;
}
// this is just so when you press run
// i get some output
console.log(makex(7));

Would really appreciate anyone that could help me. I am virtually clueless to this as it is a new topic, so it may seem very easy to some others. Thanks

"resultStr" is an empty string which i want to add each occurance of "x" into then i want to display these at the end of the program on the console. the function "makex() (number of x's) is to hold any of the functionality of my program. I am told its relatively simple, but still clueless to be honest. Any pointers would be hugely appreciated! Thanks

Member Avatar for stbuchok
function xChars(char, times){
    var result = '';

    for(var i = 0; i < times; i++){
        result += char;
    }

    return result;
}

//this should display xxxxxxxxxx
alert(xChars('x', 10));

//this should display aaaa
alert(xChars('a', 4));

//this should display testtesttest
alert(xChars('test', 3));

do understand this is a Java forum, not a JavaScript forum.
the JavaScript forum can be found here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.