0

Recently Attended the interview, Some one asked the question like below:

var array = [0,1,2,3,4,5];

Output :

  temp:
    temp1:1
    temp22:22
    temp333:333
    temp4444:4444
    temp55555:55555

I tried below code it is working fine but is there any best solution for this example :

array.forEach(function(item,index){
      var text ="";
        if(index >= 2){
               for(var j =1; j <= index; j++){
               text += index;
               }
                console.log("temp"+text + ":" + text);
        }else{
        console.log("temp"+index + ":" + index);
        }
});

Thanks in advance!

0

2 Answers 2

1

Using ES6 template strings and String.prototype.repeat

var array = [0,1,2,3,4,5];

array.forEach(item => {
  const text = String(item).repeat(item);
  console.log(`temp${text}: ${text}`);
})

And the same code translated into ES5 - this will work in all browsers starting from IE9 and above.

var array = [0,1,2,3,4,5];

array.forEach(function(item) {
  var text = Array(item+1).join(item);
  console.log("temp" + text + ": " + text);
})

Since String.prototype.repeat does not exist in ES5, there is a bit of a hack to generate a string of specific length with repeating characters: Array(initialCapacity) will create a new array with empty slots equal to what number you pass in, Array.prototype.join can then be used to concatenate all members of the array into a string. The parameter .join takes is the separator you want, so, for example you can do something like this

var joinedArray = ["a","b","c"].join(" | ");

console.log(joinedArray);

However, in this case, each of the members of the array is blank, since the array only has blank slots. So, upon joining, you will get a blank string, unless you specify a separator. You can leverage that to get a repeat functionality, as you are essentially doing something like this

//these produce the same result
var repeatedA = ["","",""].join("a");
var repeatedB = Array(3).join("b");

console.log("'a' repeated:", repeatedA);
console.log("'b' repeated:", repeatedB);

Using the Array function, you can scale it to any number of repeats you want. The only trick is that you need to add 1 when creating the array, since you get one less character when joining.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi @vlaz, Nice if you don't mine could explain me code how it will work in ES5, I am not getting ` Array(item+1).join(item);` Thank you so much in advance !
@Siva sure, let me edit the answer for better clarity
1

You could iterate the array and iterate the count. Then display the new string.

var array = [0, 1, 2, 3, 4, 5];

array.forEach(function (a, i) {
    var s = '';
    while (i--) {
        s += a;
    }
    console.log ('temp' + s + ':' + s);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.