Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
how to replace some character with certain repeting regex

this is the log

console.log(duckShoot(4, 0.64, '|~~2~~~22~2~~22~2~~~~2~~~|'));

the output must be====>|~~X~~~X2~2~~22~2~~~~2~~~|

here the code i've try:

function duckShoot(ammo, aim, ducks) {

    let shot = Math.floor(ammo * aim)

    // console.log(shot);

    return ducks.replace (/2/g, "X")
}

how to make /2/g just replace certain repeating

i wanna make code above same function with this

function duckShoot(ammo, aim, ducks) {

let shot = Math.floor(ammo * aim)

// console.log(shot);

for (let i = 1; i <= shot; i++) {

    ducks = ducks.replace("2", "X");

}

return ducks

}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let c = 2;   // how many you want to replace
'|~~2~~~22~2~~22~2~~~~2~~~|'.replaceAll('2', o => (c-- >= 0) ? 'X':o )

or you can keep the 'old' replace with the regex

'|~~2~~~22~2~~22~2~~~~2~~~|'.replace(/2/g, o => (c-- >= 0) ? 'X':o )

whereas

(o) => (c-- >= 0) ? 'X':o

is a simple function decreasing the counter and returning an 'X' or keep the o(riginal)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!