[Javascript?] help with functions: isOdd in terms of isEven

In summary: The correct code is:var isOdd = function (x){ if((x % 2)===0){ return false; } else { return true; }};var isEven = function (x){ return !isOdd(x);};In summary, we can define the isOdd function as a function that returns true if the given number is odd, and false if it is even, by using the modulo operator and an if/else statement. We can then define the isEven function in terms of the isOdd function by returning the logical negative of the isOdd function.
  • #1
mindauggas
127
0
Problem statement:

Define isOdd function, and then define isEven function in terms of isOdd function.

My attempt is ridiculous:

Code:
var isOdd = function (x){
	if (x % 2 ===0){
		return isOdd;
	}else{
		return "is even";
	}
};
 
 var isEven = function (){
	return;
 };
 
Last edited by a moderator:
Technology news on Phys.org
  • #2


in java:

public boolean isOdd(int x) { return ( (x%2) == 1 ); } // returns boolean true if odd

public boolean isEven(int x) { return ( (x%2) == 0 ); } // returns boolean true if even

or alternatively:

public boolean isEven(int x) { return !isOdd(x); }

basically don't return strings for "is" style functions better to return clearly defined boolean values. With strings you have to contend with remembering whether to use upper / lowercase and a host of other little problems each of which can cause a programming error in your code.
 
  • #3


mindauggas said:
Problem statement:

Define isOdd function, and then define isEven function in terms of isOdd function.

My attempt is ridiculous:

Code:
var isOdd = function (x){
	if (x % 2 ===0){
		return isOdd;
	}else{
		return "is even";
	}
};
 
 var isEven = function (){
	return;
 };

What language are you programming in? Javascript?

Usually x % 2 gives the remainder when dividing x by 2, so if x % 2 ==0, then is x odd or even?

When you write something like
Code:
var isOdd = function (x){
	//do stuff
        return isOdd;
	}
};

You are defining a function that returns itself, which doesn't make much sense. Instead, try returning a boolean value:

Code:
var isOdd = function (x){
	//if x is odd
        return true;
        //otherwise
        return false;
	}
};
 
  • #4


Sorry, it's Javascript (my justification for not mentioning the programming language is that I'm new in programming and I presumed that it is easy to identify the language just by glancing ant the syntax - i learned that i was wrong ... ).
 
  • #5


Another important reason to include the name of the language in the title of your post is to tell people what's inside, so they don't waste their time opening a post about a language they don't know anything about. It also attracts people who do know the language, and might be in a position to help you.
 
Last edited:
  • #6


gabbagabbahey said:
What language are you programming in? Javascript?

Usually x % 2 gives the remainder when dividing x by 2, so if x % 2 ==0, then is x odd or even?

When you write something like
Code:
var isOdd = function (x){
	//do stuff
        return isOdd;
	}
};

You are defining a function that returns itself, which doesn't make much sense. Instead, try returning a boolean value:

Code:
var isOdd = function (x){
	//if x is odd
        return true;
        //otherwise
        return false;
	}
};

Yes, JavaScript. I would use the litteral booleans, if I had only one function to work with. However, I don't understand how to define the isEven function in term of isOdd (I need to use the logical operator "!" (not) somehow, but it is confusing. And from this confusion many kinds of non-workable ideas arise ...
 
  • #7


mindauggas said:
Yes, JavaScript. I would use the litteral booleans, if I had only one function to work with. However, I don't understand how to define the isEven function in term of isOdd (I need to use the logical operator "!" (not) somehow, but it is confusing. And from this confusion many kinds of non-workable ideas arise ...

In the definition of isEven() you can test the number x by calling isOdd(x). If isOdd(x) returns false, x must be even.
 
  • #8


Mark44 said:
In the definition of isEven() you can test the number x by calling isOdd(x). If isOdd(x) returns false, x must be even.

Here is the original problem statement:

PHP:
Remember the functions isOdd and isEven from Exercise 3.4?

We'd like you to code them here again! But this time, the aim is to define one function in terms of the other using the ! symbol.

Define isOdd, and then define isEven in terms of isOdd.

To define the isOdd function, you will need to use:

a) The modulo % symbol.
b) If / else statement in the body of the function

I have to have both the isEven and isOdd funtions.

Code:
var isOdd = function (x){
	if (!x % 2 ===0){
		return "x is odd";
	}
};
 
var isEven = function (x){
	isOdd(x);
	return !isOdd(x);
};

I know this is WRONG, just don't know how to do it :)
 
  • #9


Think about what the type being returned from isOdd is.

Does using ! on that type make any sense?

Also
Code:
 if(!x % 2 ===0)

is incorrect. That says "IF (NOT X) MOD 2 ===0". Try to rewrite this using the != operator.
 
Last edited:
  • #10


mindauggas said:
I have to have both the isEven and isOdd funtions.

Code:
var isOdd = function (x){
	if (!x % 2 ===0){
		return "x is odd";
	}
};
David already pointed out what was wrong with your if expression. The other thing is that you DON'T want to return a string. A function can return a boolean constant (i.e., true or false).
mindauggas said:
Code:
var isEven = function (x){
	isOdd(x);
	return !isOdd(x);
};

I know this is WRONG, just don't know how to do it :)
Try this:
Code:
var isEven = function (x){
	[STRIKE]isOdd(x);[/STRIKE]
	return !isOdd(x);
};
 
  • #11


Code:
var isOdd = function (x){
[I][B]	if ((x % 2)!=== 0){[/B][/I]
		return true;
	}
};
 
var isEven = function (x){
	return !isOdd(x);
};

Tried it like that, but it seems to be sintacticaly incorrect. I got an error: "missing operand; found =" on line 2
 
  • #12


mindauggas said:
Code:
	if ((x % 2)!=== 0){[/B][/I]

Tried it like that, but it seems to be sintacticaly incorrect. I got an error: "missing operand; found =" on line 2

When you write it like that, order of operations is to first compute (x%2), then try to compare it to 0 using the comparison operator "!===", but "!===" is not a valid comparison operator in javascript. There is no javascript comparison operator for "is not exactly equal to", so instead, you could test whether (x%2) is exactly equal to 0, and then take the logical negative of the comparison:

Code:
var isOdd = function (x){
	if(!((x % 2)===0)){
		return true;
	}
};

Additionally, you should also specify that the function should return false when (x%2)===2, which could be done like

Code:
var isOdd = function (x){
	if(!((x % 2)===0)){
		return true;
        }else{
	        return false;
        }
};

or like

Code:
var isOdd = function (x){
	if(!((x % 2)===0)){
		return true;
        }
        return false;
};

or, without using the logical not operator "!",
Code:
var isOdd = function (x){
	if((x % 2)===0){
		return false;
        }else{
	        return true;
        }
};
 
  • #14
Solved, thanks to all :)
 

1. What is the purpose of the isOdd function in Javascript?

The isOdd function in Javascript is used to determine whether a given number is odd or not. It takes in a number as a parameter and returns a boolean value of true if the number is odd, and false if it is even.

2. How does the isOdd function work?

The isOdd function works by using the modulus operator (%) to check if the given number is divisible by 2. If the remainder is 1, then the number is odd and the function returns true. If the remainder is 0, then the number is even and the function returns false.

3. Can the isOdd function be used with non-numeric values?

No, the isOdd function can only be used with numerical values. If a non-numeric value is passed in as a parameter, the function will return an error.

4. How can I use the isOdd function in my code?

To use the isOdd function in your code, you can first declare the function and then call it with a number as a parameter. For example:

function isOdd(number) {
    if (number % 2 === 1) {
        return true;
    } else {
        return false;
    }
}

isOdd(5); // returns true

5. Are there any alternative methods for checking if a number is odd in Javascript?

Yes, there are alternative methods for checking if a number is odd in Javascript. One way is to use the bitwise AND operator (&) with 1. If the result is 1, then the number is odd. Another way is to use the Math.abs() method and check if the absolute value of the number is divisible by 2. If it is not, then the number is odd.

Similar threads

  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
991
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
983
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
12
Views
1K
Back
Top