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?
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
2 Asked on December 29, 2020 by chetan-batra
0 Asked on December 29, 2020 by hctor-m-c
1 Asked on December 29, 2020 by coderaty
3 Asked on December 29, 2020 by jayg713
2 Asked on December 29, 2020 by kiran
1 Asked on December 28, 2020 by nishant-sagar
django django forms django templates django views python 3 x
1 Asked on December 28, 2020 by dmswjd
0 Asked on December 28, 2020
2 Asked on December 28, 2020 by haider-abidi
0 Asked on December 28, 2020 by mona
4 Asked on December 28, 2020 by thefrontenddev
1 Asked on December 28, 2020 by theprettyface
13 Asked on December 28, 2020 by valerio0999
1 Asked on December 28, 2020 by sotn
1 Asked on December 28, 2020 by a-clmnt
Get help from others!
Recent Answers
© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir