good day! I'm a new learner of Javascript, and when I get to learn the way of using a function. It sometime confuses me on why we should declare a new variable and add the variable to the action we want to execute. Let's look into the code.
function reverse(word){
Array.from(word);
let wordLength='';
for(i = word.length-1; i >= 0; i--) {
wordLength = word[i];
}
return wordLength;
}
I'm sure you know this one of the way of reversing string in javascript, my question is:
Why do we need to declare a new variable within the function, when should we declare it?
Why can't I just type
console.log(word[i]);
?What does it mean by
wordLength =word[i];
?Why should we return the new variable(wordLength), instead of the function(reverse) after the loop?
Thank you guys!
CodePudding user response:
Why do we need to declare a new variable within the function...
Because you need a place to store the reversed word as you build it. (Note: wordLength
isn't a good name for that variable. It doesn't contain the word's length. It contains the characters of the reversed word.)
...when should we declare it?
Any time before you first need it.
Why can't I just type console.log(word[i]);?
Because the goal of the exercise is to build a string containing the reversed word, not just to output it. (And because console.log
writes a new line each time you call it.)
What does it mean by wordLength =word[i];?
That adds the character in word[i]
to the end of wordLength
. For instance, if the word is "start", wordLength
starts out with ""
, then gets "t"
added to it to make it "t"
, then gets "r"
added to it to make "tr"
, and so on.
( =
is a shorthand way to write wordLength = wordLength word[i];
. There are several of these compound assignment operators, most of them for math: -=
, *=
, etc.)
Side note: The Array.from
call in your code isn't doing anything useful. It's creating an array, but then throwing that array away because nothing uses the return value. The rest of the code is using the string you receive in word
.
CodePudding user response:
Why do we need to declare a new variable within the function, when should we declare it?
Vars is a place to store data. If your algorithm requires keeping some data to use it later you need vars. Also well named variables is a good way to create easy-to-understand code
Why can't I just type console.log(word[i]);?
You can, but it will do nothing useful. Your goal is to build a string and return it. Usage of your function will be something like
const word = getSomeText()
const reversedText = reverse(word)
doSomeStuff(reversedText) // whatever, send it online, or render it on screen some fancy way, not in the console.
So you need to return actual string, not to solve a puzzle and show the answer whatever way you like
Why should we return the new variable(wordLength), instead of the function(reverse) after the loop?
Because it contains reversed word and you function supposed to return it. there is rare complicated occasions when a function returning itself is useful, but it has nothing in common with your task