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

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

4 Years
Discussion Span
Last Post by stultuske

"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

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));
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.