AnswerBun.com

Is there a way to make JavaScript read strings as strings when multipying by numbers?

Stack Overflow Asked by PythonCreator on October 1, 2020

I am (using JavaScript) trying to get an output of 50 dashes as a banner like this:

"————————————————–"

I have so far tried the following:

let a = []
for (var i = 0; i < 50; i++) {
  a.push("-")
}

console.log(...a)

and

console.log("-" * 50)

These do not work, as in the first one, there are spaces in between the dashes.
The second one returns NaN, which is not what I need either.

The regular way I used to do this in Python was:

print("-" * 50)

This gave the perfect result of "—————————————————-".

The print() function being Python’s equivalent of console.log().

However, this does not work in JavaScript, as said before.

I also figured out that this does not work due to the fact that the string is parsed as a number.

Is there a way to achieve the "—————————————————-" banner in JavaScript without typing it manually, or is there a way to make sure that "-" is not parsed as a number?

3 Answers

No, javascript lack of that feature. But you could use repeat() on ECMAScript6:

console.log("-".repeat(50))

This is Mozilla's reference https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/String/repeat

Correct answer by Cristian Torres on October 1, 2020

if you want to use an Array:

let a = Array(50).fill('-').join('')

console.log(a)

Answered by Mister Jojo on October 1, 2020

let a = []
for (var i = 0; i < 50; i++) {
  a.push("-")
}

a=a.toString();

a=a.replace(/,/g,'');
console.log(a);

Answered by DCR on October 1, 2020

Add your own answers!

Related Questions

Why am i getting the output twice in the Node console?

2  Asked on December 29, 2020 by chetan-batra

       

button to act as checkbox and change styles?

3  Asked on December 29, 2020 by jayg713

     

What should be structure of Redux Store?

2  Asked on December 29, 2020 by kiran

         

Random integer numbers with fixed sum

3  Asked on December 28, 2020 by whitecircle

 

how to attach a header in axios.all()

2  Asked on December 28, 2020 by haider-abidi

     

How to reduce dom manipulation while appending css using javaScript

4  Asked on December 28, 2020 by thefrontenddev

     

Problem with chaining relationships in Laravel

2  Asked on December 28, 2020 by mrmar

   

Lambda expresions and “? :” operator in Java 14

1  Asked on December 28, 2020 by theprettyface

     

There is no tracking information for the current branch

13  Asked on December 28, 2020 by valerio0999

     

flask upload python-base64 image

1  Asked on December 28, 2020 by a-clmnt

       

Getting italic font everywhere in HTML

1  Asked on December 27, 2020 by user9343456

     

Binding objects defined in code-behind

11  Asked on December 27, 2020 by xandy

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir