I need help with my function

The question is below and my code is under it.

Write a function called capitalize that takes a string and returns that string with only the first letter capitalized. Make sure that it can take strings that are lowercase, UPPERCASE or BoTh.

function capitalize(text=' ') {
  
  let captial= text.toUpperCase();
  let capletter=text.slice(0);
  let lowcase=capital.toLowerCase();
  console.log(capletter+lowcase);
}

in your code you should use variable / paramater in the function header

this line slice the text from index 0 till the end not just the first letter

  let capletter=text.slice(0);

this line make lowcase = the whole text in lower case not the rest of the text

  let lowcase=capital.toLowerCase();

this will print the whole text in capital then lowercase

  console.log(capletter+lowcase);

check the following code


function capitalize(text) {
  
  let firstletter = text.slice(0,1);
  
  let textlower = text.toLowerCase();
  
  let final = textlower.replace(firstletter,text.slice(0,1).toUpperCase());
  
  console.log(final);
}

capitalize("hKOJOJd");

hope that help and have a nice day :slight_smile:

1 Like

Yes it does, thank you :slight_smile:

you welcome :slight_smile:

I know its no biggie, but I managed to even figure out how to fix my original code :smiley:

function capitalize(text) {
  
  let capital= text.toUpperCase();
  let capletter=capital.slice(0,1);

  let lowcase=capital.toLowerCase();
  let missingcap= lowcase.slice(1);
  console.log(capletter+missingcap);
}

well done @ayanda :+1: