Can someone please check what on earth I am getting wrong here, and how to correct and prevent this issue in the future?
Thanks so much.
Your error message is describing a problem, have you read it? What does it say? Do you agree?
actualSleepHours is not defined, according to the error message. The previous step tells us to set
actualSleepHours equal to the
getActualSleepHours() function call.
I don’t think there’s anything wrong in my code, to be honest. I have ran tests on
getIdealSleepHours() and they turned out perfect.
your code looks like it creates four functions and then compares actualSleephours to idealSleepHours. which will crash. because you have not created such variables at that point.
you might want to write less code and run it after just about every single change you make instead of writing so much between when you run it and then not being able to see what you’ve written or fix problems
if you run between each edit then you get told immediately if it isn’t okay.
Thank you. I’ll look through it again.
If you’re writing a function then you might want to call your function.
You can additionally use console.log to write out what is being done.
It may be particularly useful to write out what input is received, and what output is finally found and produced as the result.
You may similarly want to print at the location where you call that function, to observe how it behaves from that perspective.
There’s a big block of red text there which would strongly disagree with you…
Take a look at where you’ve declared those variables, and where you’re trying to reference them.
This is introduced in the following lesson, but your problem is essentially one of scope (right-click this link and use “open in new tab”… unintended consequence of the blurring).
I’m currently testing the code by calling it and logging the result, then saving it as a comment for later use.
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.
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
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?
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.