Title Case a Sentence - Does not pass tests

The aim here is to capitalize only the first letter of each substring, this code seems to run correctly in my terminal (i’m using node to run it), but it does not pass the FCC tests,
Could you please review and give me a hint on what’s wrong ?
Thank you,

function titleCase(str) {
  let arr = str.split(' ');
  let arrayOfChars = [];
  let capitalized = [];
  for (let i = 0; i < arr.length; i++) {
  for (let i = 0; i < arrayOfChars.length; i++) {
    for (let j = 0; j < arrayOfChars[i].length; j++) {
      if (j == 0) { capitalized.push(arrayOfChars[i][j].toUpperCase()); }
      else { capitalized.push(arrayOfChars[i][j].toLowerCase()); }
    if (i != arrayOfChars.length) { capitalized.push(' '); }
  return capitalized.join('');

I finally figured it out, the final condition should be

if (i != arrayOfChars.length - 1) { capitalized.push(' '); }

Reading my code out of my post helped see the problem :slight_smile:

Can you add a link to the exercise?

I believe the OP already figured out their issue. If they respond back in a separate reply, then I can mark that reply as “Solved”, but otherwise I can not. We will need to wait for the OP to respond.

Here is the link to the exercise : https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

I don’t believe there was a bug with FCC tests, my code was returning a string with a trailing white space, that’s what was causing the code to fail the tests (the white space at the end of the string).
Thank you for your replies,