Test says fail but output is correct

Tell us what’s happening:
Failing test for reasons unknown to me

function titleCase(str) {

  let word = ""   // used for word from string
  let arrOfWords = [""] 
                  // used to create array of strings
  let j=0         // array index for arrOfWords

  str = str.toLowerCase() //makes str lower case
// loop bellow is used for filling arrOfWords
    for( let i=0;i<str.length;i++)
    {
      if (str[i]!= " ")
      {
        word+=str[i]
      }

      if (str[i]==" "|(i+1)==str.length)  
      {
        arrOfWords[j]= word
        j++
        word = ""
      }
      
    }
    
   
      let string = ""  //This is the return string
      /* loop is used to convert the start of each word  to a capital and joined back into a single string sentence*/
      for (let i =0; i <arrOfWords.length;i++)
      {
        string +=arrOfWords[i][0].toUpperCase()
        for (let j = 1; j<arrOfWords[i].length;j++ )
        { 
            string +=arrOfWords[i][j]
        }
        string += " "
      }
   
  return string;
}

let str = titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
console.log(str)`**

function titleCase(str) {
let word = ""
let arrOfWords = [""]
let j=0
str = str.toLowerCase()

  for( let i=0;i<str.length;i++)
  {
    if (str[i]!= " ")
    {
      word+=str[i]
    }

    if (str[i]==" "|(i+1)==str.length)  
    {
      arrOfWords[j]= word
      j++
      word = ""
    }
    
    
  }
  
 
    let string = ""
    for (let i =0; i <arrOfWords.length;i++)
    {
      string +=arrOfWords[i][0].toUpperCase()
      for (let j = 1; j<arrOfWords[i].length;j++ )
      { 
          string +=arrOfWords[i][j]
      }
      string += " "
    }
 
return string;
}

let str = titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
console.log(str)
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0

Challenge: Title Case a Sentence

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Your code is correct to the extent that you are seeing. But there are some things that you are not seeing that are screwing up your test.

I think if you do:

console.log('***' + str + '***')

at the end it will be more apparent what I mean.

Are you using the bitwise OR operator | intentionally?

true | true is 1 which then gets coerced to true.

if(true | true) {
  console.log(true | true); // 1
  console.log(Boolean(true | true)); // true
}

Another way to check your output is to look at the length.

console.log("I'm a little tea pot".length)
console.log(titleCase("I'm a little tea pot").length)
1 Like

apologies i do not see what u mean

let str = titleCase("HERE IS MY HANDLE HERE IS MY SPOUT");
console.log('***' + str + '***')

it displayes ***Here Is My Handle Here Is My Spout *** which is what i expect

lol, u r correct it was a logical or not a bitwise. i am wondering for an if statement such as
if (1) { do something} does it work differently than if (true ) {do something} ?
Thank for ur help

That is what you expect? Look very carefully at the end of the string.

"***|Here Is My Handle Here Is My Spout |***"

if addition if i do

let str = titleCase('*** '+"HERE IS MY HANDLE HERE IS MY SPOUT"+  '***');
console.log(str )

it returns *** Here Is My Handle Here Is My Spout*** because i put a space like “***/S” the challeng is not state that ignore special chars like * so my code does make it upper case. its just that upper * == lower * . i hope i am making sense. Thank you for ur help

Your problem is with the spaces.

Slow down for a second.

Look at this formatted version of your output:

//                                     ↓
"***|Here Is My Handle Here Is My Spout |***"
//                                     ↑

Now look at the test case:

titleCase("HERE IS MY HANDLE HERE IS MY SPOUT") should return the string Here Is My Handle Here Is My Spout

Look at the very end of the target output.

Look at the spaces.


function titleCase(str) {
  let word = "" // used for word from string
  let arrOfWords = [""]
  // used to create array of strings
  let j = 0 // array index for arrOfWords

  str = str.toLowerCase() //makes str lower case
  // loop bellow is used for filling arrOfWords
  for (let i = 0; i < str.length; i++) {
    if (str[i] != " ") {
      word += str[i]
    }

    if (str[i] == " " | (i + 1) == str.length) {
      arrOfWords[j] = word
      j++
      word = ""
    }
  }

  let string = "" //This is the return string
  /* loop is used to convert the start of each word  to a capital and joined back into a single string sentence*/
  for (let i = 0; i < arrOfWords.length; i++) {
    string += arrOfWords[i][0].toUpperCase()
    for (let j = 1; j < arrOfWords[i].length; j++) {
      string += arrOfWords[i][j]
    }
    string += " " /* LOOK HERE */
  }

  return string;
}

TY!!! i see my problem now.

2 Likes

Hi @hellotheir4, as many other are pointing out, your output is not strictly identical to what’s expected.

Besides visualizing it like proposed above you can try to see the string length as well.
Using your implementation:

titleCase("Hello").length // 6
"Hello".length // 5

As you can see, there’s something extra that’s taking one extra “space”.

Hope it helps :sparkles:

1 Like

this helped a ton ty for ur time!

1 Like

Depends on what you mean by “work”. The outcome may be the same, but how it gets there is different. The if statement will coerce the expression to a Boolean.

console.log(Boolean(1))
// true

console.log(Boolean(0))
// false

I would suggest not relying on coercion when you don’t have to, or when you do, at least make sure you know which values coerce to what.

The reason why the number 0 coerces to false is sort of for historical reasons. Some languages does not have boolean data types so they use 0 to represent false, and 1 to represent true. JS syntax is considered a C-type’ish language so it makes sense for it to have adopted this. Although, it’s maybe not such a great idea.

1 Like