Repeat a String in JavaScript a Number of Times

Repeat a string in JavaScript a number of times

These days, the repeat string method is implemented almost everywhere. (It is not in Internet Explorer.) So unless you need to support older browsers, you can simply write:

"a".repeat(10)

Before repeat, we used this hack:

Array(11).join("a") // create string with 10 a's: "Repeat a String in JavaScript a Number of Timesaa"

(Note that an array of length 11 gets you only 10 "a"s, since Array.join puts the argument between the array elements.)

Simon also points out that according to this benchmark, it appears that it's faster in Safari and Chrome (but not Firefox) to repeat a character multiple times by simply appending using a for loop (although a bit less concise).

How can I recurse in JS to repeat a string n times?

Recursion involves making a function call itself and AVOIDS using a loop. You have a loop in the recursive function which is a big red flag.

Do something like this:
If n is 1 return s else return s concatenated to the result of the function with n - 1.

function repeatStr (n, s) {
if (n == 1)
return s;
else
return s.concat(repeatStr(n - 1, s))
}
repeatStr(3, "Hi")

Repeat String - Javascript

Good news! String.prototype.repeat is now a part of JavaScript.

"yo".repeat(2);
// returns: "yoyo"

The method is supported by all major browsers, except Internet Explorer. For an up to date list, see MDN: String.prototype.repeat > Browser compatibility.

MDN has a polyfill for browsers without support.

Javascript: repeat a string in given a count parameter with a while loop

Try this, you need to build the string before you return it. If you're not using recursion, you only return once:

function repeatString(string, count) {

var num = 0;
var str = "";
if ( count === 0 ) {
return "";
}
while ( num < count ) {
num += 1;
str = str + string;
}
return str;
}

repeatString("asdf", 2)


"asdfasdf"

How to repeat a letter number of times between two strings

You would need to keep track of str between function calls. You could do this by storing the value of str in this using Function.prototype.bind, by adding an additional str parameter, or by using a global variable.

// Using Function.prototype.bind
function repeat_bind(n) {
const str = this.str ?? '';
if (n <= 0) return 'B' + str + 't'
return repeat_bind.bind({ str: str + 'a' })(n - 1);
}
console.log('bind: ' + repeat_bind(5));

// Using additional parameter
function repeat_param(n, str = '') {
if (n <= 0) return 'B' + str + 't'
return repeat_param(n - 1, str + 'a');
}
console.log('parameter: ' + repeat_param(5));

// Using pre-existing global variable
let str = '';
function repeat_preglobal(n) {
if (n <= 0) {
const result = 'B' + str + 't';
str = '';
return result;
}
str += 'a';
return repeat_preglobal(n - 1);
}
console.log('pre-existing global: ' + repeat_preglobal(5));

// Using ghost global variable (created and deleted)
function repeat_ghostglobal(n) {
const str = window.str ?? '';
if (n <= 0) {
delete window.str;
return 'B' + str + 't';
}
window.str = str + 'a';
return repeat_ghostglobal(n - 1);
}
console.log('ghost global: ' + repeat_ghostglobal(5));

How to repeat a function a number of times based off quantity variable

Make an anonymous function for the onClick handler () => {...}.
And call your function onMintPressed(quantity) inside it.

<button onClick={()=>{onMintPressed(quantity)}}> Mint NFT </button>

Also, your quantity state variable is a string, so take that into consideration when using it for quantity.

update
You can also make your onMintPressed method the click handler. It will be passed the event as the parameter, but you can ignore that. Then inside that function, just grab quantity since it's a state variable.

<button onClick={onMintPressed}> Mint NFT </button>

const onMintPressed = (event) => {
const quantityAsNumber = +quantity
console.log('quantity is', quantityAsNumber)
}

update 2
I'm not sure what you want to do, but you can call some 'minting' function quantity times by simply sticking it in a for() loop. I also added async/await keywords so each of the 'mintSomething()' calls will block until they complete. This is pretty basic, so perhaps I misunderstand what you want to do.

const onMintPressed = async (event) => {
const quantityAsNumber = +quantity
for (let i=0; i < quantity; ++i) {
await mintSomething()
}
}

Post answer accept update
Whoops. I wrote above that the mintSomething() calls would work in parallel, but that's not true. Each will block until it completes. To mint things in parallel, you just hold onto the promise each mintSomething request returns and then wait on all of them.

const onMintPressed = async (event) => {
const quantityAsNumber = +quantity
const promises = []
for (let i=0; i < quantity; ++i) {
promises.push(mintSomething())
}
await Promise.all(promises)
}


Related Topics



Leave a reply



Submit