Help with Challenge Excersice

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.
My approach - - ->

<!DOCTYPE html>
<html>
	<head>
		
	</head>
	<body>

		<script type="text/javascript">
			/* 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(string){
				return toUpperCase(0);
			}
			indxCap = capitalize("grace");
			console.log(indxCap);
			 
		</script>
	</body>
</html>

Hello there,

This is not JavaScript:

/* 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.*/

If you are writing within a script tag, you must comment text out using JS:,

// This is a JS comment

Also, read about the use of toUpperCase

Hope this helps

its a script within an HTML D.O.M

Sorry, I missed it. You are right using those comment tags.

Still, read docs on the use of toUpperCase for your issue.

i have read some few docs on it…can’t seems to apply it on the challenge exercise.

Well, let us walk through it:

function capitalize(string){
			return toUpperCase(0);
		}

this function expects a string as an argument, and assigns it to the parameter variable named string.

What do you think toUpperCase(0) is referring to, and doing?

i was trying to capitalize the first letter i.e index[0]

Right, and how would you get the first letter of this code:

let string = 'Test';

let firstLetter = ?;
1 Like

let firstLetter = [0];

I recommend you go over this challenge: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/use-bracket-notation-to-find-the-first-character-in-a-string

If you are struggling with anything in there, refer to the previous lessons.

Hope this helps

1 Like

My bad…
let firstLetter = string[0];

Right. So, what is the value of:

toUpperCase(0)

Why not run a few tests in an editor like Replit, CodePen, the fCC editor for a challenge. Use console.log() to print to the console some values and variables, until you get what you expect.

2 Likes

I figured it out, before now my logic towards the approach was wrong…
My new approach - - -

	<script type="text/javascript">
		/* 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(){
			let string = "watch";
			let newString = string.toUpperCase()[0];
			return newString;
		}
		
		console.log(capitalize());
		 
	</script>
</body>

Yes, you figured it Right
toUppercase Function doesn’t take any arguments as you were doing previously
toUppercase(0) :x:
toUppercase()[0] :white_check_mark:

1 Like

the second one is not right either, toUpperCase() is not an array

The challenge exercise requested that the first letter of the returned string should be capitalized. Do you have any other approach to solving it?

the logic may be sound, but it was a syntax issue, toUpperCase()[0] doesn’t make sense

okay… can you show me a better approach?

use the correct syntax…

2 Likes

The idea is to return a capitalized string . Keep in mind, the words may not be all lowercase. Your function should be able to return string of any format for example, the letters could be, string, sTring, STRING etc. So far,approach is only limited to converting a lowercase text and the way you are using “toUpperCase” is not convenient. So try doing things in a more effective manner. Goodluck :slight_smile:

1 Like