-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJadenCasing
46 lines (45 loc) · 2.3 KB
/
JadenCasing
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
46
/*
This was my first kata on codewars, excluding the entrance kata.
The kata gave me quite a challenge and I enjoyed working through it.
There is something that I still don't understand about this function
that perplexed me. I created the code fine with a test string, but
upon removing the test string and trying to bring in the parameter
I noticed it would always show up as undefined. I stumbled upon "this",
but unfortunately still do not have a grasp on why it was relevant in this
instance. I feel that it must have something to do with the way the function
is setup. I know that this is a function expression because I am storing it into
a variable for the declaration. I also understand that it is an extension
of String.prototype. I feel that something about it being a function expression
or having something to do with the fact that it was an extension of the String method.
I will look into this and see where and why it comes into play.
I used the tools available to me to complete this kada. I know that some of the other
submissions used different ways to get the job done. I will come back and rewrite this as
I learn more as well to see if I can make it better.
*/
String.prototype.toJadenCase = function () {
//split the string into an array of substrings using split function with space as the separator
var jadenStringArray = this.split(" ");
//defining my final result at the top so it is in scope of all loops.
var finalResult = "";
//use foreach loop to run through each of the substrings
for (var i = 0; i < jadenStringArray.length; i++){
//split the substrings into arrays.
var txt = jadenStringArray[i].split("");
//setup the result within scope to be used.
var result = "";
//Loop through the letters of each word.
for (var ii = 0; ii < txt.length; ii++){
// if the letter is the first letter
if (ii === 0){
//change the case to upper case
txt[ii] = txt[ii].toUpperCase();
//save the word we created
jadenStringArray[i] = txt.join("");
}//end if
}//end for letters iteration
}//end for word iterations
//join the strings back together as a string.
finalResult += jadenStringArray.join(" ");
//return the finalResult back to the user
return finalResult
};// end function toJadenCase