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
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.
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
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
// ↓
"***|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;
}
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.
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.