How to create a function that repeats a certain value

Asked

Viewed 266 times

-5

I want to create a function that takes two values, the first will be any word, and the second will be the amount of times the system will print that word.

var valor = "palavra" // exemplo
var qtd = 2 // exemplo
function repete(valor,qtd){
  for(var i = 0; i <qtd; i++ ){
     return valor
  }
}

I tried to develop using the for and even filling the variables did not print the result.

  • 3

    Edit your question by trying to better describe what you really need, as there are 4 answers that meet your request and none of them meet your need

5 answers

0

Good afternoon. Following resolution:

function repete(valor,qtd){
    for(i = 1; i <= qtd; i++) {
        console.log("" + valor + "");
    } 
}

0

I did it in a very simple way using your own example

var valor = "palavra" // exemplo
 var qtd = 2 // exemplo 
 function repete(valor,qtd){
 let palavra='';
    for(i=0;i<qtd;i++){
        palavra += valor+' ';
    }
    return palavra;
 }
 repete(valor,qtd);
  • So it would be more like this only that I need the "Qtd" to determine how many times the value will repeat itself. Be it an apalavra or number. And then print that value "Qtd" times.

  • @Biancakelly he is already doing that, I tested with number and words and changed the amount of times and he respected normally, n understood where this error could explain me better?

  • @Biancakelly vc wants it to print somewhere in html?

  • No, no, what was troubling me was that I was using Return instead of console.log. Thank you

  • @Biancakelly if the code worked then ok, vote for the code that helped you so that other people can use it too

0

Good morning Bianca, if your intention is to return a string with the words in sequences follows the code that worked for me: if the last word is "Banana" it returns "Bananabanana", but I don’t know if this is exactly what you need.

function repete(palavra, qtd) {
    for(i = 0; i < qtd; i++) {
        valor = valor + palavra;
    } 
    return valor;
}

I’m available in case you need anything else.

  • Let’s assume that I need the word "banana" to be repeated five times. I don’t think this function would suit very well... But thanks just the same

0


An alternative solution, and I believe it to be simpler, would be to create an array and fill in the same with the desired value, and return a Join similarly so that it is not necessary to concatenate values to string, for example:

function repete(palavra, qtd){
  // Cria um array com length determinado por "qtd"
  const array = new Array(qtd).fill(palavra);
   
  // Troque o - por espaço ou outro valor para determinar como as palavras devem ser separadas
  return array.join(' - ');
}

document.write(repete('Palavra 1', 5) + "<br />");
document.writeln(repete('Palavra 2', 2) + "<br />");
document.writeln(repete('Palavra 3', 10) + "<br />");

Concatenating Strings into a for is not a problem as long as the number of repetitions is small and the text is not too large, but you may encounter problems if the number of repetitions or the text is too large.

Both mine and the other answers given so far solve the problem described in the statement, if anything is missing then you should edit your question and add details about what you really need otherwise your question will be closed.

Avoid editing your questions by adding other questions on your own as this invalidates the answers given previously, since you are new here, take some time to read more about the Stackoverflow Survival Guide

  • Thank you, I’m still adapting to the site, but I found out what the problem was. I will read the guide as soon as possible, thank you very much.

0

The method repeat() returns a new string with a specified number of copies of the string what was it called.

var valor = "palavra" // exemplo
var qtd = 5 // exemplo 
console.log((valor+" ").repeat(qtd));

very simple

Browser other questions tagged

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