Maybe you forgot to define a function named isEven? I don`t know where, help, please


#1

I can`t figure out what is wrong with my function, could you help me, please?

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/1?curriculum_id=506324b3a7dffd00020bf661#

isEven = function(number) {
If ("camila".length / 2 = 5)
{
return false;
}else if ("camila".length / 2 = 3){
return true;
}else { return false;
}
};
isEven (3)

Thank you!



Replace this line with your code.


#2

You should use var keyword before function definition.

var isEven = function(number) {...};

You used If instead of if.

You should use === here instead of =.

Also, your function doesn't check if given number is even. Rethink your code.


#3

Hi there, thank you for replying.

I tried it again, I made some changes, but it still asking me: It looks like you have a ReferenceError. Maybe you forgot to define a function named isEven?

I don`t know why :frowning2:

var isEven = function(number) {
// Your code goes here!
if (10 / 2 = 5)
{
return true;
}else if (10 / 2 === 3){
return false;
}else { return 0;
}
};

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/1?curriculum_id=506324b3a7dffd00020bf661#


#4

Hello @textwhiz64874,

As @arkussss stated, you should always use "===" instead of "=" in a if statement. The reason for this is because "=" is the assignment operator, which you use when you are initialising a variable or reassigning a variable.

var hello = "Hello, world!" //correct
var hello === "Hello, world!" //incorrect

On the other hand, "===" is the equality operator, which checks whether a variable has a certain value or not

if (hello === "Hello, world!") {/* Some code! */} //correct
if (hello = "Hello, world!") {/* Some code! */} //incorrect

So you should change the if branch of your statement, as it is still using the assignment operator instead of the equality operator. Codecademy is probably not able to enter the if statement, which means that the function is unable to return anything, which is giving you the ReferenceError. Fix this and try again :smile:.

Regards,
Barry Allen


#5

Here I am again.
I tried to change it to:

var isEven = function(number) {
// Your code goes here!
if (10 / 2 === 5)
{
return true;
}else if (10 / 2 === 3){
return false;
}else { return 0;
}
};

It still not right.
Showing this message:
Oops, try again. Looks like your function returns false when number = 3. Check whether your code inside the if/else statement correctly returns true if the number it receives is odd.

Sorry to be a pain, I am totally new to coding and all this.

Thank you

https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/1?curriculum_id=506324b3a7dffd00020bf661#


#6

Hey @textwhiz64874,

So, you are implementing a function that returns true when a function is Even and False otherwise. You must use the modulo (%) operator in order to to this. A quick refresher on the % operator: it returns the remainder of the numbers when they are divided.
56 % 2 === 0 //because 56/2 = 28 remainder 0
53 % 4 === 1 //because 53/4 = 13 remainder 1

So, you have to make your if statement in such a way that it checks whether the number is divisible by 2 or not :wink:

Regards,
Barry Allen


#7

Thank you so much for your explanation! I will change it and hopefully, it will be alright, many thanks :slight_smile:


#8

No worries @textwhiz64874. Keep coding hard! :laughing:

Regards,
Barry Allen


#9

I am still working on it and can`t find a solution :confused:

Could you help me, please?
What is wrong now?

var isEven = function(number) {
// Your code goes here!
if (12 % 2 === 0)
{
return true;
}else if (24 % 2 === 3){
return false;
}else { return 0;
}
};
isEven (1);
isEven (2);
isEven (3);

It does print: true.
But it keeps showing this message: Oops, try again. Looks like your function returns false when number = 3. Check whether your code inside the if/else statement correctly returns true if the number it receives is odd.

Help me, please.
Thank you
https://www.codecademy.com/courses/javascript-beginner-en-qDwp0/0/1?curriculum_id=506324b3a7dffd00020bf661#


#10

Hello @textwhiz64874,

Sorry for the late reply, I had matters to attend to and so logged off for a while.

The whole point of a function is that it is a reusable block of code so when you pass in arguments, it will perform operations on those arguments. So instead of checking whether 12 and 24 are divisible by 2, you should instead be checking if number (your argument) is divisible by two or not. This function will then return true if the number that you have passed in is even and false otherwise. Here I'll start you off

var isEven = function(number) {
    if (number % 2 === 0) {
        //Some code!
    } else {
        //Some code!
    }
};

Regards,
Barry Allen


#11

Thanks a lot! now I understand :slight_smile:


#12

No worries, just keep coding hard :smile:!


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.