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

118
Views
How to change (manipulate) char string depend on giving value

i want to ask how to manipulate char in string depends on giving value

my string

"---x---x---x------x"

when im input a value = 2

char "x" was changed to "o" in 2 times

my expected value is

"---o---o---x------x"

thank you in advance

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

const x = "---x---x---x------x";
let input = 2;

let output = [];
for (const dashes of x.split("x")) {
  output.push(dashes);
  if (input > 0) {
    input--;
    output.push("o");
  } else {
    output.push("x");
  }
}
output.pop();
output = output.join("");

console.log({ output });
about 4 years ago · Santiago Trujillo Report

0

You can just loop over the and replace x with o until value becomes 0(which is falsy value)

let str = "---x---x---x------x";
let value = 2;

while (value--) {
  str = str.replace("x", "o");
}

console.log(str);

about 4 years ago · Santiago Trujillo Report

0

based on solution here:

var str = "---x---x---x------x"
var n = 0
var N = 2
var newStr = str.replace(/x/g,s => n++<N ? 'o' : s)
about 4 years ago · Santiago Trujillo 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!