2

I need to understand how Javascript handles the new string object just like in the C# code below

var UpperBound = 14;
for (int i = 0; i < UpperBound; i++) {
    Console.WriteLine(new string('*', i));
  }
Console.ReadLine();

This was how i did it in Javascript:

var upperBound = 14;
for(var i=0;i<upperBound;i++){
  console.log(new string("*", i));
}

Am a newbie in programming and i started off with javascript so if i appear dumb by asking this question pls pardon and assist me by explaining.. thanks

fokosun
  • 548
  • 1
  • 6
  • 18

3 Answers3

2

Your code is good except for this part:

new string("*", i)

It is incorrect syntax for JS. You need to create string another way.

That's how you should create a string with repeating character in JavaScript.
I should note that String.prototype.repeat is a part of ECMAScript 6.

var upperBound = 14;
for(var i=0;i<upperBound;i++)
{
    document.write('*'.repeat(i));
    document.write("<br/>");
}

Before it, you should use another approach like making this string manually in a for-loop like this:

function repeatString(ch, t)
{
  var res = '';
  for (var i = 0; i < t; i++) res += ch;
  return res;
}

var upperBound = 14;
for(var i=0;i<upperBound;i++)
{
  document.write(repeatString('*', i));
  document.write("<br/>");
}

There are some other approaches and hacks. You can read this StackOverflow topic on it.

I suggest you to read a good tutorial on JavaScript before trying to write it.
C# and JavaScript are very different languages in all terms. You cannot just take a C# code, change a syntax and think that it will work.

Community
  • 1
  • 1
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
2

There is no equivalent for new string("*", i) in JS. You need to do repetition yourself.

A handy hack is Array(i + 1).join("*"), but it is not very efficient I don't think, as it needs to construct the array. The best way is probably to loop and concatenate.

Amadan
  • 191,408
  • 23
  • 240
  • 301
1

I would like to combine two solutions, so (the best option is to use es6-shim) here is a polyfill (borrowed from here)

if (!String.prototype.repeat) {
  String.prototype.repeat = function(count) {
    'use strict';
    if (this == null) {
      throw new TypeError('can\'t convert ' + this + ' to object');
    }
    var str = '' + this;
    count = +count;
    if (count != count) {
      count = 0;
    }
    if (count < 0) {
      throw new RangeError('repeat count must be non-negative');
    }
    if (count == Infinity) {
      throw new RangeError('repeat count must be less than infinity');
    }
    count = Math.floor(count);
    if (str.length == 0 || count == 0) {
      return '';
    }
    // Ensuring count is a 31-bit integer allows us to heavily optimize the
    // main part. But anyway, most current (August 2014) browsers can't handle
    // strings 1 << 28 chars or longer, so:
    if (str.length * count >= 1 << 28) {
      throw new RangeError('repeat count must not overflow maximum string size');
    }
    var rpt = '';
    for (;;) {
      if ((count & 1) == 1) {
        rpt += str;
      }
      count >>>= 1;
      if (count == 0) {
        break;
      }
      str += str;
    }
    return rpt;
  }
}

And the usage:

var upperBound = 14;
for(var i=0;i<upperBound;i++){
    console.log("*".repeat(i));
}
hazzik
  • 13,019
  • 9
  • 47
  • 86