Logical error ? Sting operation

Tell us what’s happening:

Can any one please tell me what is the problem with this code. I hand run it and i think it works fine, obviously there is some problem please help anyone

Your code so far


function titleCase(str) {
//1. Method1: Convert str to arr.
//2. Use given str and  else if to find space.
let foundSpace =false;
for(let i=0;i<str.length;i++){

    if(str[i]!==" " && !foundSpace){
       i==0?str[i].toUpperCase():str[i].toLowerCase();
    }
    else if(str[i]!==" " && foundSpace){
       str[i].toUpperCase();
       foundSpace = false;
    }else if(str[i]==" "){
      foundSpace = true;
    }

} 
return str;
}

console.log(titleCase("I'm a little tea pot"));


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

this is not doing anything
string are immutable, you can’t change a single character in the string just like that and toUpperCase returns a new value, doesn’t change what is applied to

let str ="i'm a little tea pot";
console.log(str[0].toUpperCase());
// I

They why this works ?

You are printing the output of that command, but not the original string.

Try

let str = "resistance is futile";
str[0].toUpperCase();
console.log(str[0]);
1 Like

it doesn’t work, you are logging to the console the return value of str[0].toUpperCase(), but it doesn’t change the value of str
try this and see if it changes anything

str[0].toUpperCase()
console.log(str)
2 Likes

But if you see str has’nt still changed.

console.log(str)

Strings are immutable

2 Likes
function titleCase(str) {
  //1. Method1: Convert str to arr.
  str.toLowerCase();
  let newStringArray= str.split(" ");

  for(let i=0;i<newStringArray.length;i++){
      newStringArray[i].charAt(0).toUpperCase();
  }
  
  return newStringArray.join(" ");
}

console.log(titleCase("I'm a little tea pot"));

This also does not work. i changed my str to array, then why it’s not working ?

because toUpperCase return a new value, doesn’t change the value of what it is applied on, and you are still trying to change a character in the string, you can’t change pieces of the string

if you try to do it manually it errors:

let str = "hello";
str[0] = "H"; // error, impossible to do

you are trying to do the same thing in a fancier way but it is still not possible

and this just doesn’t work:

let str = "hello";
str.charAt(0).toUpperCase();
console.log(str);

make the above work and you can solve the algorirhm

1 Like
let str1 = "I'm a little tea pot";
str1 = str1.toLowerCase().concat(" tHIS IS ");
console.log(str1);
//i'm a little tea pot tHIS IS

I understand i cannot change a character within string, but when i change whole string to a lowerCase and doing concat and saving the value in same string variable str1. It works

Since it is immutable it should not change, what exactly is happening ? is it creating new reference to str1??

Actually i am also confused if I declate str1 as const the above code will not work, so what exactly is happening behind the scene ?

you can change a whole string alright in that way
you can do str1 = str1.toLowerCase(), but as toLowerCase() return a new value for the code it is the same as doing str1 = "A wizard went to the market to buy an apple"
you can’t still do str1[1] = str1[1].toLowerCase()

1 Like

with const you can’t reassign any value

let a = 8;
a = 9; // fine
a++; // still fine

const b = 9;
b = 11; // error, you can't reassign a variable declared with const

So a new reference will be created or not ?. The same reference will point to new value ?

when you use a method that returns a new string you are creating a new string, which you can reassign to the variable that was holding the old string

only with arrays and objects you can have different references to the same object

1 Like