What am I getting wrong here? Sleep Debt Calculator - Javascript

Hi, thepitycoder, thanks for the reply.
I am currently on the Web Development course so I think I’m not covering the scope until later.
I am following ionatan’s suggestions of rewriting the code and testing it once done, but if any further issues arise I’ll keep your reply in mind.
Cheers.

I’m skirting around the issue, but the point is that you need to demand to be in control of every single operation you carry out and be willing to observe everything you do.

When you’re writing a function you’d presumably be concerned only with what happens between the input and output of that function.

And you can print out a log of all the things you’re doing, ensuring the order of actions makes sense and that… they’re happening at all.

So maybe you’d do

console.log("about to call my function")
let result = myfunction()
console.log("the function returned! its result was:", result)

and everything you expected to happen in your function, which you’d be writing out to observe them, would be printing between those two. if anything was outside then… that would say something about the problem.

So you would expect an output like this:

about to call my function
calling some other function
that other function's result was 3
setting my variable to 3
hey look i can read it back: 3
adding 5 to 3, i get: 8
the function returned! its result was: 8
1 Like

So, are you implying that I console.log a summary of each part of my code so that it is clearer to see what I am doing?

I’m saying, interact with what you’re currently writing.

you’re not writing a bunch of text

you’re writing instructions
when you write an instruction, you run it and see if it has the desired effect … nothing surprising about that

earlier you had lots of code, and somewhere in the middle things went bad… well yeah duh, you weren’t looking.


and then as for what you actually write out, depends on what you want to observe.

Cheers for that! That really helps the writing of my functions.

The scope seems to be the problem of why the variable is not working for me. The scope refers to the variable being unusable outside of the function (a blocked scope variable.).
Would an alternative be to use the entire function instead of the variable instead?
Thanks.

The inside and outside of a function each has very different perspective on time.
When should your code run?
How does a function communicate with the outside world?

And, are you writing a function, or something else? A function is a self-contained unit of code. You would not be touching anything else while writing a function.

I understand that the position of a function is important for the processing (is that the correct word?) of the code. It allows it to be referred to later on, and it can even allow the function to be called in other ones.
I figured out very recently about scopes. The variables that I am writing inside the function are blocked-case scopes, meaning they cannot be referred to later on. What would be an alternative would be to call the entire function when comparing them with each other, using conditionals. Or, you can write a variable outside of the function, and still use it later on in the conditional.

when is later on, and is that a time when they should be referred to?

it BETTER be before the function finishes, and likewise, after it begins

so where runs when, and when do you want things to happen, and where do you then therefore have to put those things?

// got no business doing anything here, this runs before the function
// ...or after, point is, it doesn't run when the function does

read input

make whirring noises  // <- do things here.

emit output

// got no business doing anything here

Such as if you declare a variable inside a function, it is ONLY FOR USE INSIDE THE FUNCTION.

Can you explain that please? I don’t really understand this.

I think when a function is necessary it is put down, and you said earlier a function is a self contained unit of code, so it can be reused when you call it with values as it’s parameters.

If you call a function, does code outside the function run?
Take a finger toe, nose, anything pointy. Point at your code. What executes when a function is called?
Whatever your function wants to do, it better be in the path of that.

Of course! Just because it’s a function doesn’t mean it halts the running of everything else.

The block inside the function. This is where all the work that change the inputs to the outputs happens. It’s what’s necessary for the function to work.

(To be honest, this is the first time someone has critically analysed my code, and it’s fun to see where one can improve and learn from his own mistakes. Thanks very much for that, ionatan.)

You have a single point of execution which moves through your code. Like a marble rolling downhill.

When this moves into a function, the things in the function get carried out. Afterwards, this point moves back to the caller. There is now a result. No other code ran, the point of execution was in the function.

When does this point of execution go through whatever is outside the function? That’s unrelated to when the function runs, this is a different location. It probably doesn’t run as many times.

Whatever is outside your function is unrelated to your function. That’s something else. If you’re writing a function, then that’s irrelevant.

Whatever code your function needs to run, put it in the function. If you need it to run during the function, it goes into the function, that’s what runs when the function runs.

So, after the point of execution moves to the function caller, does it continue to the code that is outside the function, in between the function declaration and the function caller?

After the function call? Would it jump back to the place where code unrelated to the function started?

Understand this.

Understand this as well.

no why would it. you finished calling the function, do the next thing.
(also that would be completely uncontrollable)

point at the top of your file and start executing things. when you reach the bottom, the program ends.

You’re suggesting that this:

const f = () => {
}
f()
f()

would be an infinite loop, because after calling f, f would get called.

create f
run f
run f
exit

1 Like
const areaOfRectangle = (width, length) => {
    return width * length;
}

console.log('howdy folks, its ya boi');

console.log(areaOfRectangle(4, 10);

What gets logged to the terminal first, 40 or howdy folks, its ya boi?

according to you it would say howdy both before and after 40, so there’s your proof that the outside doesn’t run along with the function

which is the point of being a self-contained unit. the unit runs.

1 Like

Of course, because the outside code doesn’t run with the function

so therefore you can’t put code that belongs to your function outside it

1 Like

Alright, I understand now. Thanks for the help! I’ll come to you if I have any further questions.