What is wrong with this code?


#1

Hello guys,

I'm taking the beginner's Javascript course and stuck at this code, which actually wasn't written by me:

var my_number = 7; //this has global scope

var timesTwo = function(number) {
my_number = number * 2;
console.log("Inside the function my_number is: ");
console.log(my_number);
};

timesTwo(7);

console.log("Outside the function my_number is: ")
console.log(my_number);


This is the description along this exercise:
"Change line 4 to use the var keyword. Notice that the value of my_number in the function is now 14 and outside the function is 7.

Using my_number without the var keyword refers to the global variable that has already been declared outside the function in line 1. However, if you use the var keyword inside a function, it declares a new local variable that only exists within that function. "

It gives me this message and I cannot proceed with the course:
"Your code doesn't look quite right. Check the Hint if you need help!
Your code threw the following error: TypeError: console.log is not a
function."

Thank you!


Uknown Error?
#2

It's asking you to re-declare the variable (using the same name) inside the function. This will overwrite the variable in the global scope to a local scope, allowing you to manipulate it without affecting it globally.


#3

Thanks emgo_dev, but I'm not sure what needs to be done, because I get error every time?


#4

That's the most I could really suggest. If you could provide a link to the lesson I could offer more help? I could have sworn that this forum provided a link to the lessons whenever a query like this is made, I can't seem to remember/find it though. It was a great feature.


#5

Here it is:
https://www.codecademy.com/en/courses/javascript-beginner-en-6LzGd/2/2?curriculum_id=506324b3a7dffd00020bf661

Thanks a lot!


#6

Your code is nearly identical to mine. Just add the var where it says, and make sure you add a semicolon after the 2nd last console.log().

I'm not getting a console.log isn't a function error when I run your code above.


#7

Thanks a lot emgo_dev!
I refreshed the browser and it worked. I don't have an idea what was the issue.


#8

This error mostly occurs if you use console.log = (...) this overwrites console.log with whatever was in () which leads to the problem that even fixing the code won't fix this and as the value is most likely not a function you get this " not a function error" when you try to e.g. call a string or a number. And glad to see that you discovered the way to fix it -> just refresh the page that reloads the javaScript and thereby resets the built-in functions.


#9

O.K. What about this code. It sill giving me an error:


var sleepCheck = function (numHours) {

if (numHours >= 8) {
return "You're getting plenty of sleep." + " " + "Maybe even too much.";}

else {
    return "Get some more shut eye!";
    }

};

console.log(sleepCheck(10));


Thanks a lot guys!


#10

If I write at the end:
sleepCheck(10);

Still doesn't work.


#11

Seems to be ok copy the messages from the instruction probably a typo somewhere.


#12

Thanks haxor789!

Here are the instructions:


  1. Write a function named sleepCheck that takes the parameter numHours

  2. Inside the function, write an if statement where if the number of hours of sleep is greater than or equal to 8, the computer will return "You're getting plenty of sleep! Maybe even too much!";.

  3. Otherwise (else) if the number of hours of sleep is less than 8, have the computer return "Get some more shut eye!";

Then call the function with different hours of sleep

  1. Call the function with 10 hours of sleep, like this: sleepCheck(10);
  2. Call the function with 5 hours of sleep.
  3. Call the function with 8 hours of sleep.


#13

Hi guys,

Yes, it was a typo. I put period after "You're getting plenty of sleep!" instead of exclamation mark.

Thanks everyone!