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

187
Views
Splice method don't properly

My splice method don't work here (don't give my code attention just I want to know why this code don't work in my console).

function capSpace(txt) {
  // write your code here
  wordSplit = txt.split("");
  for (let i = 0; i < wordSplit.length; i++) {
    if (wordSplit[i].toUpperCase() == wordSplit[i]) {
      wordSplit.splice(5, 0, " ")
    }

  }
  return wordSplit
}

console.log(capSpace("fausJkalMalkihkLhb"));

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

0

First of all, you need to increment the i for each character that you add to the array.

I assume you're trying to add a space before each capital letter, in which case you need to add it at the i index, not the 5th index.

function capSpace(txt) {
  // write your code here
  wordSplit = txt.split("");
  for (let i = 0; i < wordSplit.length; i++) {
    if (wordSplit[i].toUpperCase() == wordSplit[i]) {
      wordSplit.splice(i, 0, " ");
      i++;
    }

  }
  return wordSplit
}

var result = capSpace("fausJkalMalkihkLhb").join('');
console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

The splice method is adding an empty space to the wordSplit array in your snippet found here:

wordSplit.splice(5, 0, " ")

This is incrementing the length of wordSplit on each iteration and the condition of the for loop:

i < wordSplit.length

will never be satisfied because the length of wordSplit increases which creates an infinite loop and therefore is never able to return wordSplit and therefore never able to console.log the variable of result because it's stuck in an infinite loop.

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!