Just tips/pointers (without solutions) please. I have used a while loop, but I am unsure of what to put inside of it. Simply typing in return str doesn’t work, because this will kick me out of the loop. Can anyone explain why str =str + "" isn’t recognized? And, what areas do I need to read up on, look more carefully at, understand better, etc.? Thank you in advance.
function repeatStringNumTimes(str, num) {
// use a while loop
i = 0;
while (i < num) {
i++;
str = str + "";
}
return "";
}
repeatStringNumTimes("abc", 3);
It is recognizing str = str + ""; but you are telling it to add "" to str, which is nothing, so its literally doing just that, adding nothing to str which results in no change to str.
return ""; is saying to return no value, which is what it does.
Think about what value it is you want added to string…
and for return, think about what value it is you want it to output…
Think of it this way… you have a conditional statement… if the string does not pass the conditional statement, it will return nothing anyway. So no need to tell it to return nothing.
What you need is to tell it what to return if it does pass your conditional statement.
in other words:
Its like telling the computer, if you have more than 0 apples, tell me how many apples you have.
If the computer has more than 0 apples, it will tell you how many it does have. If it doesn’t have any apples, it wont say anything.
(trying to help without giving away code like you asked lol hope my examples help!)
function repeatStringNumTimes(str, num) {
// use a while loop
strArr = [];
i = 0;
while (i < num) {
i++;
strArr.push(str);
}
return strArr.join('');
}
repeatStringNumTimes("abc", 3);
In the explanation, it is stated that if num > 1, we add the string to a call of our function with num being decreased by 1, which will add another str and another… until eventually num is 1. And return that whole process.
How does JavaScript know to store that whole process as one instance of str? I hope this makes sense.
This is an example of recursion. It can be very confusing at first, but the way to do it is to take a set of small number (1,2,3) and follow through the execution.
So, if num is one, the return value is just str. Expected.
Now, if it is 2, the return value is str + the same function called again. So if we set up a stack of return functions:
This is helpful. I’m looking at an example of recursion provided on https://docs.microsoft.com/en-us/scripting/javascript/advanced/recursion-javascript. In order to factorialize the argument, or number, the stack of return functions would have to look like return str * (return str), and not return str + (return str), correct? Does JavaScript know to multiply each return because it is written in the original return statement return (num * factorial(num - 1))? In other words, the operator in the return statement determines how each subsequent return interacts with the previous return.
function factorial(num)
{
// If the number is less than 0, reject it.
if (num < 0) {
return -1;
}
// If the number is 0, its factorial is 1.
else if (num == 0) {
return 1;
}
// Otherwise, call this recursive procedure again.
else {
return (num * factorial(num - 1));
}
}
var result = factorial(8);
document.write(result);
// Output: 40320
Yeah, pretty much. Because the function will keep on expanding out any recursive calls, so long as you have some operation that can be chained together on results, and the type of the result you get back from the recursive calls is consistent, everything will work out.