Basic JavaScript Day 4: Homework

Hey JavaScripters!

Awesome teamwork today! :purple_heart: We covered a lot, and I am really proud to see how beautifully you cooperate and try to help each other! Also, please, do remember that you need time, practice and rest to let everything sink in, so it’s perfectly normal if it feels confusing and a lot. Because it is!

:loudspeaker: Quick reminder about homework for the next session!
Tomorrow we’ll start with the ES6 section, so your homework is to do the exercises until that point and finish basic Javascript module!

Remember folks, you’re not alone.
Don’t hesitate to seek help in the js-help channel or on the forum!

Be sure to collaborate, stay hydrated and get plenty of rest!!!

We’re all in this together!
Happy Coding!
#PatPat🤚

4 Likes

**

Homework-4 Done! :blush:

**

3 Likes

Basic JavaScript Day 4 Homework: Completed

Loved the lookUpProfile() function Challenge!

2 Likes

Hello everyone - Ramón, thanks for the excellent tuition!

I have a question about the record collection exercise - this is the nth time I have tried it, and I am getting better at it. I now have a solution which works - but only if I have included console.logs - it’s very strange to me, as I believe console.logs are not part of the program, but are just there to illustrate what is happening as the program runs, by displaying the contents to the console.

Here is my code
function updateRecords(records, id, prop, value) {
console.log(records[id])
if (prop !== “tracks” && value !== “”) {
records[id][prop] = value;
} else if (prop === “tracks” && !records[id].hasOwnProperty(“tracks”)) {
records[id].tracks = [value];
} else if (prop === “tracks” && value !== “”) {
records[id].tracks.push(value);
} else if (value === “”) {
delete records[id][prop];
} else
console.log(records[id])
return records;
}

This runs perfectly, but if I remove both instances of "console.log(records[id]), it fails.

I’m mystified - can anyone explain? Thanks

Very odd! Do you know how it fails? What are the errors telling you? Can you reproduce the output?

It’s very strange - I have reproduced it both in Safari and in Firefox (I know no-one likes using Safari for coding ;))

Here is what is returned in the console if I run the code without the console.logs present: I had to include a screenshot as it would not let me copy/paste the console contents -

And, for the sake of completeness, here is what I see when I run the tests when the console.logs are present:

Hmm! Try removing else

I think you’ll need to console.log that updateRecords call to confirm :slight_smile:

Thank you - removing the last

else

did the trick!

I tried to do the homework but am totally baffled by the recursive statements - I have to do my work today then I will be catching up with stream #5 and hopefully it will start to make some sense to me after that :sweat_smile:

2 Likes

Remember, this is a step by step process, you’re doing great!

2 Likes

Day4 Homework done!

2 Likes

Hello, please I’m really struggling to understand recursion. please does anyone have any good resources to help with that? thank you.

Hi Hameeda,

Recursiveness is a tough thing to wrap your head around. In a lot of programming jobs you may not ever need it, but unfortunately it is often a topic during interviews.

This lecture explains it really well Recursion - CS50 Shorts - YouTube (note that the code sample looks like JS but it is C - there is a difference only in how the function definition is done)

Also this video is really good What Is Recursion - In Depth - YouTube . If you are just starting out with JS the last part may be a bit complex to follow, but you can always revisit that later.

And in text, there are quite a few good examples that give you the feel of what recursion is in this post:

Please take note of the joke that Ben Halpern pulled at the top of the post - and see if it makes you smile. If it does, you’ve got it!

Also, if you want to step and visualize your code, try that here, I am sure it will give you further insights: JavaScript coding tutor - Learn JavaScript by visualizing code execution

I hope these help!

4 Likes

Thank you so much. I appreciate the time you took to respond and the resources. I’ll check them out. Thanks again.

2 Likes

Syl shared some great resources but I just wanted to chime in with one more video that really helped me understand how recursion works:

6 Likes

That is a great find! Very concise! It explains both what is a function (and its input/output) as well as how recursion works. And all in just 6 and a bit minutes! Thanks for sharing!

2 Likes

Hi Ramón - I’m being difficult here. :slight_smile: Would it be possible to go over the exercise Profile Lookup at the end of one of the classes please? I have tried it multiple times and although I am getting closer, I don’t understand the freecodecamp explanation, and don’t just want to copy and paste the solution.
Also, in the exercise Generate Random Whole Numbers in a Range, could you please walk through the code Math.floor(Math.random() * (max - min + 1)) + min. I don’t understand the underlying principles here, or the algebra that seems to be involved.
In fact, do you think it would be possible to cover the last exercises in the Basic JavaScript part of the course - up to and including Recursion? Maybe a couple a day after the end of each lesson? I’m sorry to be demanding, but I am lost with these exercises, and don’t find the explanations online very helpful.
Would it be possible to go over the following from Replace Loops using Recursion. I am lost with that stuff, but fine with the ES6 stuff. :slight_smile:

1 Like

Hi Jane, I am sure @hola_soy_milk will respond when he reads your post, but let me attempt to explain your ‘Generate Random Whole Numbers in a Range’ here:

  • Remember that Math.random() returns a value between 0 and 1, but can never quite return a 1
  • The maximum ‘span’ for the range of number is determined by the difference between max - min
  • This ‘span’ actually stars from the next number up, and we use floor, which is why we add +1

So for an example where max = 20 and min = 15, this span would yield: 5 + 1
→ then multiply this by an outcome of Math.random(), say 0.8 = 4.8
→ and take the floor: 4

As we are seeking a value between min and max, we need to increase the value by min (as that was the start value for our range), in this example it would yield 19, a perfectly valid whole number in our 15 to 20 range.

Try to do this exercise also with really small values for theoretical outcome of the random function (like 0.01) and really large (like 0.99) and see if that makes sense now?

See also what happens if you would not add that +1.

Also a much better explained example can be found here: Generating random whole numbers in JavaScript in a specific range - Stack Overflow

I hope this helps.

3 Likes

Hey Jane!

Please never feel like you’re being difficult. Learning this stuff is not straightforward at first glance. Having a function call itself is especially weird to wrap around at first.

I can’t promise that I’ll be able to do all of these cutting into Q&A, but will find a way to make it work.

I also recommend this Visualiser that was brought to my attention by the incomparable @syllie:
https://pythontutor.com/javascript.html#mode=edit

Use this to step through loops and recursion!