![]() ![]() I rarely use the ternary operator in this way, but I think it's readable enough. I guess what I'm asking is, is this indicative of good coding practice? Or and I making a mountain out of a molehill? Doesn't this solution just work for the set examples, making it so the step can ONLY be either 1 or -1? It feels like an "eloquent" solution to a limited problem, but the original problem had a much bigger issue, in that the user should be able to choose the step increment. The author suggests a ternary operator in the function declaration, like so: step = start < end ? 1 : -1Īt first I thought that this was quite neat and hadn't considered doing anything like that, but after thinking it over, it doesn't sit right with me. I've been picking my brain at various solutions to this for a day or so and eventually gave in and checked the solution. Make sure it also works with negative step values so that `range(5, 2, -1)` produces `." The author cites and example: "The function call `range(1, 10, 2)` should return ``. This argument is used to determine how much each step in the eventual range is returned. However, the next part of the exercise is to add an option third argument - the step. This is all well and good and I was able to write a successful bit of code for this problem. The first exercise tasks us to write a function that returns an array of numbers - starting with the first argument and ending with the second. In Chapter 4, the author talks about Data Structures. However, I have hit a theoretical wall that I can't seem to climb over. ![]() So, I've been reading through Eloquent JavaScript and, so far, it's been very insightful. Personal blog posts that are relevant to the subreddit's stated subject matter don't need prior approval (and are encouraged!). If you want to post something self-promotional, please message the mods first. Titles that begin with "hey guys" will be removed. If you're in doubt, message the mods first. The following are not allowed: Requests for subscribers, asking for "test users" for your new JS course, offering paid mentorships, and/or premium courses. If you’re asking for help, include enough information for others to recreate your problem. With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged. Everyone should feel comfortable asking any and all JavaScript questions they have here. This subreddit is a place for people to learn JavaScript together. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |