I recently began learning javascript, and this is the answer to a practice problem (Write a function that swaps the cases of each letter in a string):
var swapCase = function(letters){
var newLetters = "";
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){
newLetters += letters[i].toUpperCase();
}else {
newLetters += letters[i].toLowerCase();
}
}
console.log(newLetters);
return newLetters;
}
var text = "Life is 10% what happens to you, and 90% of how you REACT to it";
var swappedText = swapCase(text);
OUTPUT:
"lIFE IS 10% WHAT HAPPENS TO YOU, AND 90% OF HOW YOU react TO IT"
The code is perfectly functional and does exactly what it needs to do, but I am confused on the use of letters
. This is not my code.
The parameter letters
is not linked to anything anywhere, and this is what confuses me. I believe it represents each individual letter, but where is it defined to do so? I am familiar with Python, so I was expecting something like for i in list
.
Thanks in advanced.
I recently began learning javascript, and this is the answer to a practice problem (Write a function that swaps the cases of each letter in a string):
var swapCase = function(letters){
var newLetters = "";
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){
newLetters += letters[i].toUpperCase();
}else {
newLetters += letters[i].toLowerCase();
}
}
console.log(newLetters);
return newLetters;
}
var text = "Life is 10% what happens to you, and 90% of how you REACT to it";
var swappedText = swapCase(text);
OUTPUT:
"lIFE IS 10% WHAT HAPPENS TO YOU, AND 90% OF HOW YOU react TO IT"
The code is perfectly functional and does exactly what it needs to do, but I am confused on the use of letters
. This is not my code.
The parameter letters
is not linked to anything anywhere, and this is what confuses me. I believe it represents each individual letter, but where is it defined to do so? I am familiar with Python, so I was expecting something like for i in list
.
Thanks in advanced.
Share Improve this question asked Jul 24, 2015 at 15:47 Charles WatsonCharles Watson 1,0752 gold badges16 silver badges39 bronze badges 3- 1 WHats your question? WHenever you call the function swapCase letters is gonna be whatever you pass to it in parenthesis. swapCase(text) is just saying letters will be equal to text – code Commented Jul 24, 2015 at 15:51
-
1
when you put
text
in the parameter,text
beesletters
inside the function – FutoRicky Commented Jul 24, 2015 at 15:52 -
1
letters
is placeholder/ argument that has been provided when you callswapCase(text);
, sotext
beesletters
inside the function – vinayakj Commented Jul 24, 2015 at 16:04
4 Answers
Reset to default 2When you put text
in the parameter, text
bees letters
inside the function.
var swapCase = function(letters){ //anything you put as a parameter in this function will bee 'letters'
var newLetters = "";
for(var i = 0; i<letters.length; i++){
if(letters[i] === letters[i].toLowerCase()){ //letters[i] represents the character in the 'i' position (which is assigned in the for loop) in the string you added as a parameter.
newLetters += letters[i].toUpperCase();
}else {
newLetters += letters[i].toLowerCase();
}
}
console.log(newLetters);
return newLetters;
}
var text = "Life is 10% what happens to you, and 90% of how you REACT to it";
var swappedText = swapCase(text); // You are adding the text string as a parameter in the function, thus it being the letter variable inside the function
letters
is a function parameter, so basically when you call swapCase(text), the function takes text
and assign it to letters
. If you call the function without parameter like this swapCase()
then you basically pass undefined
to this function and that is assign to letter
. You can do a quick check at the beginning of the function to check for that.
if(letters === undefined) return false;
The parameter letters is not linked to anything anywhere
It is defined here — function(letters){
— as an argument name on the function.
It is passed a value when the function is called here — swapCase(text);
— where text
is a variable defined as a string on the line above.
I believe it represents each individual letter, but where is it defined to do so?
It's a string. You can access characters in a string using square bracket notation.
When you write the line of code swapCase(text)
you are passing the variable text
into the function swapCase
. The letters
variable inside the function gets assigned the value of whatever text
is.