-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapitalizationandMutability.js
More file actions
45 lines (34 loc) · 1.34 KB
/
CapitalizationandMutability.js
File metadata and controls
45 lines (34 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Task:
DESCRIPTION:
Your coworker was supposed to write a simple helper function to capitalize a string (that contains a single word) before they went on vacation.
Unfortunately, they have now left and the code they gave you doesn't work. Fix the helper function they wrote so that it works as intended (i.e. it must make the first character in the string upper case).
The string will always start with a letter and will never be empty.
Examples:
"hello" --> "Hello"
"Hello" --> "Hello" (the first letter was already capitalized)
"a" --> "A"
*/
//P - single parameter - string
//R - return a string where first character is capitalized and the rest of the string remains unchanged
//E
//P
//ANSWER
function capitalize(word) {
//Get the first character of the string. Convert the first character to uppercase
let firstChar = word.charAt(0).toUpperCase();
// Get the rest of the string starting from the second character
let restOfString = word.slice(1);
//Concatenate the uppercase first character with the rest of the string
let capitalizedWord = firstChar + restOfString;
//Return the result
return capitalizedWord;
}
console.log(capitalize("hello")); //Hello
console.log(capitalize("Hello")); //Hello
console.log(capitalize("a")); //A
/*Best Practice
function capitalizeWord(word){
return word[0].toUpperCase() + word.slice(1);
}
*/