Chapter 11 Exercises: More Functions (Shuttle)

I’ve been struggling with the Shuttle exercise for about three days. Before implementing recursion, I tried to solve the problem using a for loop. The fuel level is reduced, but it doesn’t seem to end properly. With my current setup and fuel level being 200,000, the loop stops at 100,000. But, the fuel status stays green.

I tried it with another fuel level to test. With the fuel level being 100,000, it only iterates once and stops at 90,000. I’ve tried placing the if statement above and below the addition and subtraction of fuel. Also, I tried to change the conditions. For the original fuel level of 200,000, should the remaining fuel level be 100,000 or above 100,000?

If there are sections in the course materials (in Chapter 10 or previous chapters) that address this problem, please let me know.

checkFuel returns green if level > 100000 (i.e., strictly greater than 100,000). So, the level of 100,000 will return yellow. If you started with 200,000 (which was green), you can’t let it drop to 100,000. You have to stop before that.

For simplicity, I am assuming you can use integers. So, the minimal remaining level to still return green is 100,001.

1 Like

Okay, that makes sense. I will try to break it before it reaches 100,000 or 50,000. The conditions are fulfilled for some test numbers, but another condition breaks them improperly (ex. 100,000 and 94500 breaks due to my first condition). There are confusing…

For the anonymous function, are we returning the fuel that’s left over, or the fuel that’s taken?

UPDATE(1): I finished the exercise, but I have not been able implement recursion. I don’t know if it’s not possible with the topics covered up to this chapter or if my approach to these anonymous functions is inefficient.

“Once ye figure out how much fuel ter pump out, return that value.”

I don’t see an explicit requirement that you should use recursion in your solution. Use whatever works and makes sense to you.

1 Like

I’m with Leonid on this one. Recursive functions can be tough to even understand,let alone use.

" 11.9.2. Why Do I Need To Know Recursion?

If you ask veteran programmers how often they use recursion, you will get answers ranging from “Not since I had to do it in school,” to “Very regularly.” Some programmers avoid recursion like the plague, while others look forward to using it wherever it fits…"
I just completed unit 2 (Java) and am about to start liftoff. I don’t think we have come across a real world example yet, although maybe some people in the class have used it.

In most real world examples I have seen it is also relatively rare(on the surface). it comes in very useful for splitting strings to find or replace a single character, if you had reason to,but there are many other ways to do this as well.

At this point I would focus on getting things to work, getting fancy will come with time (and need). You have a lot of learning to go, but you have been doing awesome.
I know it is hard some days, but do as much work as you can, and if you are ever caught up, try something new.
When you break/then fix (or even give up) something, you learn a lot about how it really works, and if you figure it out on your own, it really sinks in.

Good luck and keep on coding. You got this!

1 Like

I read that instruction in two different ways, but it makes sense to return the fuel taken. I’m glad that the shuttle exercises did not require recursion. For the studio and exercises, I’ve been trying to use recursion for about four days now (esp. for the bonus mission), but I have been unsuccessful with refactoring any of my solution to use recursion. Thank you for your feedback.

Thank you for the encouragement. I read that section in the chapter as well. Everything has been working now without recursion. For Part A of the studio, I was close to solving it, but then I saw LaunchCode’s solution in Part B. After about four days of studying, I have not been able to solve the bonus mission for the studio or understand recursion. It’s disheartening that I am still having trouble comprehending this concept.

Congratulations on completing LC101! I wish you the best with Liftoff.

Since I completed LC101 in the past (studying Python and Java), I’ve had to adjust to strictly studying online on my own. It’s been hard studying JavaScript without being in a LC101 class setting. I struggled with Python, but I did better with Java.

I think I may take a few days off from coding and explore other things. I was pushing myself to work through all the materials within a few months. I don’t know where I fit in the tech field. Hopefully, more clues will come with time and exploration.

Thank you again for your feedback!

Don’t know what part of the country you are from, but there should be tech jobs available everywhere, even for newcomers.
Did you recently complete the LC101? What about going through the Liftoff program next time it comes up?
Have you been in tech before? Either way, you likely have skills that others don’t or perhaps you dont realize are skills (funny how imposter syndrome works). You have already been through LC101, that means you already have some pretty good chops and just need to find some one to encourage/train you up, not beat you down for what you still don’t know.

I know there are no guarantees, but I feel like LaunchCode wants to help you succeed. I know as a community, the support has helped greatly, especially on those days you don’t want to pick it up again.
So walk away for a while, but come back soon, and keep reaching out.

Just looked back at my repl for this studio. I did not complete the bonus mission (recursion) on this. I doubt I could without assistance. You know more than you think you do.

1 Like

Here is how you can rethink the shuttle fuel exercise using recursion:

  1. Assume, you don’t know how much fuel there is in the tank (just some initLevel). This is your function’s parameter.
  2. Assume, you don’t know at what specific fuel level the lights switch on or off. You just know that they do when the fuel level drops below some treshold. So, you need to keep checking if the light switches. This is your changing condition.
  3. You want to repeatedly siphon an amount of fuel. You want to siphon as much fuel as you can without triggering a light change. Let’s do it 1,000 units/gallons at a time.
  4. You try to siphon the first 1,000 gallons. Then another 1,000 (2,000 total). Then another (3,000 total) etc. You keep drawing… Every time you do, your function adds to the “loot”. And the fuel level drops, decreases by 1,000. You keep calling the function passing the lower fuel level in. This is your recursion.
  5. You keep checking the light every time you try to syphon fuel. At some point, the light changes. Stop! You can’t take this last portion of the fuel. You take 0 gallons this time. This is your base case. When your function reaches the base case, it returns 0: No fuel taken this time and neither can you keep trying.
  6. The result returned by the function is the amount of fuel you were able to syphon at that particular try. You add it to your total and repeat the recursive call. When it reaches the base case (0 new gallons syphoned), it stops calling itself. End of recursion.

Hope this helps. No, I don’t like recursion either. :weary::sweat_smile:

1 Like

The trick here is, in the process of your recursion, to get down to an array of size 0 (an empty array) or of size 1 (an array with one element). Those need no sorting. That’s your base case. So:

  1. You find a min value in the given array and add it into a new array;
  2. Delete the value from the old array. (This is what makes your problem smaller for each recursive call).
  3. Concat your new array (which is accumulating sorted numbers, one by one) with a recursive call to your function, passing in the now smaller old array.
  4. Eventually, you will get to your base case (one element remains in the array). Nothing left to sort.

Notice that if you try to sort an empty array or an array with one element, you will immediately hit Step 4 (the base case).

1 Like

Thank you for sharing an alternative way to view the shuttle exercise. I refactored my code to check the light switches instead of checking the fuel level.

The last step you mentioned is where I returned something different:

In my solution, I am returning the amount of fuel left in the shuttle instead of returning what is siphoned. Then, I’m subtracting the remaining fuel in the shuttle from the original fuel amount in the irs function to show the fuel taken.

I’ve tried to introduce a new variable to hold the siphoned fuel, but I either get errors or wrong calculations. I do not understand how I can add the siphoned amount before repeating the recursive call. I’m sorry for being so confused.

I live in Missouri and completed LC101 at the end of 2018. I wanted to go through the Liftoff program, but the program location was far from home and at night. I am not working and share a car with my family at this time. I would have to figure out a way to attend in-person without it interfering with my father’s and sister’s schedules. It would be great if I could participate in the program online.

I studied art and music in college, but my work experience in tech is mainly through clerical work. Imposter syndrome does make it difficult for me to believe I am capable of being in tech. I hope I can find a mentor for support and more opportunities for training. I am curious about front-end development, but I feel more drawn towards learning about UI/UX design because of my background in the arts.

Thank you again for your encouragement!

Sorry for sending so many updates. I finally figured out a few things I needed to adjust in my code to sort the array of numbers. I set an empty array and set outside the recursion and set the base case return to an empty array.

To check if it works, I used console.log to check how the new array prints through each iteration. The results for both are correct, but the iterations look different…:confused:

Without Recursion

With Recursion

UPDATE: Looked at the RecursionExample01 and the calls do work the same as my studio solution. I also finally figured out how to add the siphoned fuel total and repeat the recursive call for the exercise. Okay, I guess I can officially can take a break now…Thank you both for your help.

1 Like

Good job figuring out the recursive approach to sorting.

If you have a talent for and leanings toward UI/UX design, perhaps you are torturing yourself with diving so deeply into back-end type of coding? As an example, take a look at the 5 Web Dev Independent Projects at Codecademy. Would they be a better match for your skills?

Not before. At the same time, in your return statement. If you look at Example 11.8.3, it may give you the clue.

Thanks! Hopefully, I will get comfortable using the recursive approach with more practice.

Yeah, I am pushing myself hard for something that I am not interested in. But, I am trying to apply my interest in being a lifelong learner to back-end just in case I do not qualify or fit other tech roles. The projects from your Codecademy link seem to match my skills and some of my interests better.

I’m not sure if I would be a good fit for UI/UX design yet. When I studied art in college, I focused on traditional drawing, painting, and fundamental design concepts (line, shape, value, etc.). I’ve been taking courses about digital painting and 2D game art recently, due to my interest in digital art, illustration, and video games. But, I have not had the chance to explore graphic/visual design, user experience/research, or user interface design. Internally, I am debating whether I should pursue what LC101 mainly focused on (back-end development) to qualify for jobs now or pursue more visually and/or human-centered roles that will take some time to prepare for. It’s been difficult feeling stuck.