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

402
Views
Why am I getting an error when using split function of javascript?

I'm trying to split my string but not get proper output as per my expected.

Input

let name = "helloguys";

Output

h
he
hel
hell
hello
hellog
hellogu
helloguy
helloguys
let name = "mynameisprogrammer";
for (let index = 1; index <= name.length; index++) {
  let finalData = name.split(name[index]);
  console.log(finalData[0]);
}

enter image description here

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

0

You can use substring to take parts of your input:

let name = "mynameisprogrammer";
for (let index = 1; index <= name.length; index++) {
  let finalData = name.substring(0, index);
  console.log(finalData);
}

about 4 years ago · Juan Pablo Isaza Report

0

You could use Array.map() along with String.slice() to split the string into the required values:

let name = "mynameisprogrammer";
let result = [...name].map((chr, idx) => name.slice(0, idx + 1));

console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; }

about 4 years ago · Juan Pablo Isaza Report

0

If you need this to be unicode aware, you might want to avoid using string substring / slice etc.

Instead convert to array, and split there.

eg..

let name = "my😀name🤣is🫠programmer👍";

const nameA = [...name];
for (let ix = 1; ix <= nameA.length; ix += 1) 
   console.log(nameA.slice(0, ix).join(''));
.as-console-wrapper { max-height: 100% !important; }

ps. To see what I mean by unicode aware, try using the name from the code above into some of the other answers.

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!