I'm trying to do the following exercise: Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
Here is my code; I can't figure out what I'm missing:
function even_or_odd(n) {
if(n % 2 === 0)
{
console.log('Even');
}
else
{
console.log('Odd');
}
};
You are not returning anything, just outputing it in the console.
function even_or_odd(n) {
if(n % 2 === 0) {
return 'Even';
} else {
return 'Odd';
}
};
假设“i”是要检查的数字...
if(i%2){alert('odd');} else {alert('even');}
Nothing just return the string if returning is the goal:
function even_or_odd(n) { if (n % 2 === 0) { return 'Even'; } else { return 'Odd'; } }; alert(even_or_odd(5)); alert(even_or_odd(2));
If anybody looking for shorthand using es6, They can try this-
const evenOrOdd = number => number % 2 ? 'Odd' : 'Even';
// Outputs-
evenOrOdd(2) // Even
evenOrOdd(3) // Odd
yeha I was having the same problem,then i remembered i need to return the values
function even_or_odd(number) {
if(number % 2 == 0){
return ("Even");
}else{
return ("Odd");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.