Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
str has assigned as an empty string in the first line of code, and it won't go in to the for loop when 1 <= 0. So it'll directly return ""
Oke, but what is returned when count_sheep(0) is executed? Not an empty ""...
i literallt did exactly this and it didnt pass me
what a beautifully thorough and graspable response. thank you so much.
This is known as an "arrow" function in JS, and for this particular example there isn't really a point. For simple cases there isn't a difference, it's just a stylistic choice. Many devs (including myself) prefer the arrow syntax because it's concise and much more readable in certain contexts. For example if I had a collection of things and I wanted to filter them by some criteria, I could write this with traditional "function declaration" syntax:
or with function expression syntax I could write:
or with arrow syntax I could simply write:
Now for more complex cases there are deliberate differences in functionality - arrow functions cannot be generators or constructors, for example, and they bind differently. It's okay if you're not familiar with those concepts yet, and it's definitely okay to be confused in general! You should become familiar with this syntax though; even if you dislike it yourself it's still going to pop up all over the place in other people's code.
Edit: There is a broader point to think about, too; in JavaScript functions are much like any other variable value and we often want to pass functions around. When filtering an array, as in the example above, the
filter()
function requires that you give it a function to apply to each element. To give a function to another piece of code, we often have to make that function into a variable.whats the point of naming a variable initialized as a function? i find that syntax incredibly confusing.
When I first came to CodeWars, I was completely stumped. I have come back a few weeks later, after practicing with Javascript for hours each day and now I've done several of them. I say this to say, give it time. Even seasoned programmers get stuck on problems.
Amazing! tried it for hours but couldnt solve it because I tried to return something inside the for loop and always get 1 sheep...and used if/else (num !== 0) { forloop }
didnt need if else in this hahahahaha got my brain juiced up
same!!!
I'm confused. Since i = 1, if you put in the argument (function(1)) then "i <= num" becomes true. So it would increase i by 1. Therefore, i = 2 and the output would be "1 sheep...2 sheep", no? not "1 sheep...". if someone could explain this please. That would be appreciated.
Yes if you are going to use template literals, you need to use backticks!
Easy to understand. This is the best practice for sure.
Best practice
In this case
``
is a template literals with own rules: ${} returns a value and whatever you put between``
will be shown just like a regular string.question - why would you use `` instead of ''? I don't understand the difference in syntax except that it wasn't working when I wrote ''
Loading more items...