How do I repeat a phrase that the user has typed in n times in js?

Asked

Viewed 818 times

-2

My code that is not working, how do I repeat the phrase that the user typed?

Because what I did isn’t working?

Since it’s a college issue, I need to resolve it with the tie for.

[[1]

  • can explain better the problem??

  • post your code and not images of it... we can not edit or copy if there is something wrong...

  • Welcome João Vitor Lumertz, if an answer solves your problem, mark it as accepted, see https://i.stack.Imgur.com/jx7Ts.png and why https://pt.meta.stackoverflow.com/questions/1078/como-e-porque-acceptinga-resposta/1079#1079. This tour is interesting https://answall.com/tour. In the best navigation!!

2 answers

3

The for statement creates a loop consisting of three optional expressions, within parentheses and separated by semicolons, followed by a statement or a sequence of statements executed in sequence.

for (inicilização; condição; incremento) {
    // código que será repetido
}

Because what I did isn’t working?

Since it’s a college issue, I need to resolve it with the tie for.

Note that in the inicialização of your loop for you do i<0 condition that does not exist. For this you should declare the variable i as being negativa, for example -2 and in that case the loop would be executed from -2 up to the previous number as indicated in the prompt, i.e., i<q.

See in the example below the step-by-step code being executed with the variable i being declared negative.

    var i =-2;
    var nome = prompt("Digite qualquer nome:");
    var q = prompt("Informe a quantidade de vezes que você quer que repita:");

    for(i<0; i<q; i++){
      console.log( i + " " + nome);
    }

or by initiating in this way for(i=-2; i<q; i++){, that is, from i = -2 to the value immediately below the one at the prompt

    var i;
    var nome = prompt("Digite qualquer nome:");
    var q = prompt("Informe a quantidade de vezes que você quer que repita:");

    for(i=-2; i<q; i++){
      console.log( i + " " + nome);
    }

To have your code run exactly the number of times typed at the prompt, change the startup to i=0

var i;
var nome = prompt("Digite qualquer nome:");
var q = prompt("Informe a quantidade de vezes que você quer que repita:");

for(i=0; i<q; i++){
  alert(nome);
}

1


Use the function repeat:

var palavra = "oi";
var quantidade = 10;

alert(palavra.repeat(quantidade));

In the excerpt of your code, would look like this (adapt as your need):

var i;
var nome = prompt("Digite qualquer nome:");
var q = prompt("Informe a quantidade de vezes que você quer que repita:");
q = parseFloat(q);

alert(nome.repeat(q));

Using for:

var i;
var nome = prompt("Digite qualquer nome:");
var q = prompt("Informe a quantidade de vezes que você quer que repita:");
q = parseFloat(q);

var resultado = "";

for (var i = 1; i <= q; i++)
{
	resultado += nome;

}

alert(resultado);

  • This is a college issue and I need to settle it with whatever.

  • added with the use of For. But if it’s a question of college, try to study to understand the error and what part of the code is having difficulty

  • Thank you very much friend.

  • blza settled, please mark as solved the answer. thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.