if/else statements only logs if, not else
I am stuck on a problem for my coding homework.
Here it is:
- Write a loop that logs "Marco!" when i is even,
- "Polo!" when i is odd.
- Do not edit the existing code.
- Hint 1: Use an if/else statement
- Hint 2: Google the mod operator (%)
My attempt
let x=11;
let y=4;
let i=x%y;
if (i) {
console.log("Marco!")
}
else {
console.log("Polo")
}
This logs Marco when I need it to log polo. So while I continue to try and solve this I wanted to see how experts would do it.
javascript if-statement math modulo
add a comment |
I am stuck on a problem for my coding homework.
Here it is:
- Write a loop that logs "Marco!" when i is even,
- "Polo!" when i is odd.
- Do not edit the existing code.
- Hint 1: Use an if/else statement
- Hint 2: Google the mod operator (%)
My attempt
let x=11;
let y=4;
let i=x%y;
if (i) {
console.log("Marco!")
}
else {
console.log("Polo")
}
This logs Marco when I need it to log polo. So while I continue to try and solve this I wanted to see how experts would do it.
javascript if-statement math modulo
5
Use% 2
to check if a number's even (to be more precise,% 2 === 0
)
– CertainPerformance
Jan 2 at 0:52
add a comment |
I am stuck on a problem for my coding homework.
Here it is:
- Write a loop that logs "Marco!" when i is even,
- "Polo!" when i is odd.
- Do not edit the existing code.
- Hint 1: Use an if/else statement
- Hint 2: Google the mod operator (%)
My attempt
let x=11;
let y=4;
let i=x%y;
if (i) {
console.log("Marco!")
}
else {
console.log("Polo")
}
This logs Marco when I need it to log polo. So while I continue to try and solve this I wanted to see how experts would do it.
javascript if-statement math modulo
I am stuck on a problem for my coding homework.
Here it is:
- Write a loop that logs "Marco!" when i is even,
- "Polo!" when i is odd.
- Do not edit the existing code.
- Hint 1: Use an if/else statement
- Hint 2: Google the mod operator (%)
My attempt
let x=11;
let y=4;
let i=x%y;
if (i) {
console.log("Marco!")
}
else {
console.log("Polo")
}
This logs Marco when I need it to log polo. So while I continue to try and solve this I wanted to see how experts would do it.
javascript if-statement math modulo
javascript if-statement math modulo
edited Jan 2 at 2:13
Dale Burrell
3,36042655
3,36042655
asked Jan 2 at 0:50
Marco RMarco R
61
61
5
Use% 2
to check if a number's even (to be more precise,% 2 === 0
)
– CertainPerformance
Jan 2 at 0:52
add a comment |
5
Use% 2
to check if a number's even (to be more precise,% 2 === 0
)
– CertainPerformance
Jan 2 at 0:52
5
5
Use
% 2
to check if a number's even (to be more precise, % 2 === 0
)– CertainPerformance
Jan 2 at 0:52
Use
% 2
to check if a number's even (to be more precise, % 2 === 0
)– CertainPerformance
Jan 2 at 0:52
add a comment |
1 Answer
1
active
oldest
votes
If you want to check if a number is even or odd, use the modulo operator (%
), which returns the remainder of dividing one number by the other. You should reverse your logic:
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
Here's how this works:
let i = x % y;
What this does is it divides x
by y
(divides 11
by 4
), and takes away the remainder - in this case the remainder would be 3
, so i = 3
.
Now, here comes the tricky bit. If you want to find out if a number is even, you can use % 2
, which is what we're doing in the if
statement. If the number is even, it will return 0
as there will be no remainder from dividing by two. It's tricky, but I'll show you as best I can:
If we have 6
(which we know is even), and we test if it is even by dividing it by 2
, it should return 0
as there is no remainder:
console.log(6 % 2);
And this is how our logic in the first snippet works, only it uses Boolean truthy and falsy values. Falsy values are:
false
0
''
""
``
null
undefined
NaN
So if i
is even, the modulo will return 0
, meaning that the first if
statement will not run because i % 2
will return 0
which evaluates to false
, therefore the code will run console.log("Marco!")
if i
is even, but console.log("Polo!")
if i
is odd.
Further reading:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
- How can I use modulo operator (%) in JavaScript?
- https://developer.mozilla.org/en-US/docs/Glossary/Falsy
- https://developer.mozilla.org/en-US/docs/Glossary/Truthy
- https://www.sitepoint.com/javascript-truthy-falsy/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000118%2fif-else-statements-only-logs-if-not-else%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to check if a number is even or odd, use the modulo operator (%
), which returns the remainder of dividing one number by the other. You should reverse your logic:
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
Here's how this works:
let i = x % y;
What this does is it divides x
by y
(divides 11
by 4
), and takes away the remainder - in this case the remainder would be 3
, so i = 3
.
Now, here comes the tricky bit. If you want to find out if a number is even, you can use % 2
, which is what we're doing in the if
statement. If the number is even, it will return 0
as there will be no remainder from dividing by two. It's tricky, but I'll show you as best I can:
If we have 6
(which we know is even), and we test if it is even by dividing it by 2
, it should return 0
as there is no remainder:
console.log(6 % 2);
And this is how our logic in the first snippet works, only it uses Boolean truthy and falsy values. Falsy values are:
false
0
''
""
``
null
undefined
NaN
So if i
is even, the modulo will return 0
, meaning that the first if
statement will not run because i % 2
will return 0
which evaluates to false
, therefore the code will run console.log("Marco!")
if i
is even, but console.log("Polo!")
if i
is odd.
Further reading:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
- How can I use modulo operator (%) in JavaScript?
- https://developer.mozilla.org/en-US/docs/Glossary/Falsy
- https://developer.mozilla.org/en-US/docs/Glossary/Truthy
- https://www.sitepoint.com/javascript-truthy-falsy/
add a comment |
If you want to check if a number is even or odd, use the modulo operator (%
), which returns the remainder of dividing one number by the other. You should reverse your logic:
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
Here's how this works:
let i = x % y;
What this does is it divides x
by y
(divides 11
by 4
), and takes away the remainder - in this case the remainder would be 3
, so i = 3
.
Now, here comes the tricky bit. If you want to find out if a number is even, you can use % 2
, which is what we're doing in the if
statement. If the number is even, it will return 0
as there will be no remainder from dividing by two. It's tricky, but I'll show you as best I can:
If we have 6
(which we know is even), and we test if it is even by dividing it by 2
, it should return 0
as there is no remainder:
console.log(6 % 2);
And this is how our logic in the first snippet works, only it uses Boolean truthy and falsy values. Falsy values are:
false
0
''
""
``
null
undefined
NaN
So if i
is even, the modulo will return 0
, meaning that the first if
statement will not run because i % 2
will return 0
which evaluates to false
, therefore the code will run console.log("Marco!")
if i
is even, but console.log("Polo!")
if i
is odd.
Further reading:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
- How can I use modulo operator (%) in JavaScript?
- https://developer.mozilla.org/en-US/docs/Glossary/Falsy
- https://developer.mozilla.org/en-US/docs/Glossary/Truthy
- https://www.sitepoint.com/javascript-truthy-falsy/
add a comment |
If you want to check if a number is even or odd, use the modulo operator (%
), which returns the remainder of dividing one number by the other. You should reverse your logic:
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
Here's how this works:
let i = x % y;
What this does is it divides x
by y
(divides 11
by 4
), and takes away the remainder - in this case the remainder would be 3
, so i = 3
.
Now, here comes the tricky bit. If you want to find out if a number is even, you can use % 2
, which is what we're doing in the if
statement. If the number is even, it will return 0
as there will be no remainder from dividing by two. It's tricky, but I'll show you as best I can:
If we have 6
(which we know is even), and we test if it is even by dividing it by 2
, it should return 0
as there is no remainder:
console.log(6 % 2);
And this is how our logic in the first snippet works, only it uses Boolean truthy and falsy values. Falsy values are:
false
0
''
""
``
null
undefined
NaN
So if i
is even, the modulo will return 0
, meaning that the first if
statement will not run because i % 2
will return 0
which evaluates to false
, therefore the code will run console.log("Marco!")
if i
is even, but console.log("Polo!")
if i
is odd.
Further reading:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
- How can I use modulo operator (%) in JavaScript?
- https://developer.mozilla.org/en-US/docs/Glossary/Falsy
- https://developer.mozilla.org/en-US/docs/Glossary/Truthy
- https://www.sitepoint.com/javascript-truthy-falsy/
If you want to check if a number is even or odd, use the modulo operator (%
), which returns the remainder of dividing one number by the other. You should reverse your logic:
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
Here's how this works:
let i = x % y;
What this does is it divides x
by y
(divides 11
by 4
), and takes away the remainder - in this case the remainder would be 3
, so i = 3
.
Now, here comes the tricky bit. If you want to find out if a number is even, you can use % 2
, which is what we're doing in the if
statement. If the number is even, it will return 0
as there will be no remainder from dividing by two. It's tricky, but I'll show you as best I can:
If we have 6
(which we know is even), and we test if it is even by dividing it by 2
, it should return 0
as there is no remainder:
console.log(6 % 2);
And this is how our logic in the first snippet works, only it uses Boolean truthy and falsy values. Falsy values are:
false
0
''
""
``
null
undefined
NaN
So if i
is even, the modulo will return 0
, meaning that the first if
statement will not run because i % 2
will return 0
which evaluates to false
, therefore the code will run console.log("Marco!")
if i
is even, but console.log("Polo!")
if i
is odd.
Further reading:
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators
- How can I use modulo operator (%) in JavaScript?
- https://developer.mozilla.org/en-US/docs/Glossary/Falsy
- https://developer.mozilla.org/en-US/docs/Glossary/Truthy
- https://www.sitepoint.com/javascript-truthy-falsy/
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
let x = 11;
let y = 4;
let i = x % y;
if (i % 2) {
console.log("Polo!");
} else {
console.log("Marco!");
}
console.log(i); //So you can see if the above works or not
console.log(6 % 2);
console.log(6 % 2);
answered Jan 2 at 1:07
Jack BashfordJack Bashford
11.7k31846
11.7k31846
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54000118%2fif-else-statements-only-logs-if-not-else%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Use
% 2
to check if a number's even (to be more precise,% 2 === 0
)– CertainPerformance
Jan 2 at 0:52