1

This is my first ever using mod operator. What I've done so far is declare and initialize a variable and parsed the string to an integer. I used a if statements to determine whether the remainder is an even or odd number. Then I used document.write to display the output for each if statement.

But the output is not showing in the browser. The error console message is that I have an invalid assignment on the left-hand side. I tried switching the left-hand side to the right-hand side but the error is still invalid on the left-hand side.

The other thing is I'm not sure whether I should be using the parse since I read that the mod operator attempts to convert the string to a number. But I'm a little confused on this.

Anyway, here is the code: Any suggestions?

<?xml version ="1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1   /DTD/xhtml1-strict.dtd">
<html xmlns - "http://www.w3.org/1999/xhtml>
<html>
<head> 

<script type="text/javascript">
 //Declare variables

 var num = 18;

 //converts to integer
 num = parseInt(num);

 //write to output
 if(num % 6 = 0){
 document.write(% + "<h2> is an even number</h2>");
 }
 if(num % 6 != 0){
 document.write(% + "<h2> is a odd number</h2>");
 }

 //end if


 </script>

 </head>
 <body>

1
  • I like your answer. The only reason I used document.write is I thought the only other ways to output was with alert or message. But I'm learning. I'm still trying to grasp the concept of this Mod operator. I just doesn't look logical not to use an assignment operator. But it was because of the assignment operator I was getting the error. Commented Oct 24, 2011 at 18:56

4 Answers 4

2

What are you trying to do with

document.write(% + "<h2> is an even number</h2>")

% + "some string" isn't valid Javascript.

Also, as others have said, you need num % 2 == 0 (change the 6 to 2 and make sure you have two = signs).

Sign up to request clarification or add additional context in comments.

Comments

1

The shortest way to write it would be:

document.write("<h2> is an "+(num % 2 ? "odd" : "even")+" number</h2>");

[EDIT] The original solution does not work because only a single "=" is used to test equality. In Javascript, you must use either "==" (loose equality) or "===" strict equality.

7 Comments

A yes, short and sweet. Though it would be nice if you described what was wrong with the original answer.
I really like this answer. Your logic is so amazing. I'm still learning the syntax. But what I'm grasping is why the 2? Is there an explanation that maybe I, a novice, can understand?
I like the original answer. But this one is more concise.
I'm getting a syntax error on the example. This is the error:Error: syntax error Source File: file:///Users/schweideltyson/Documents/CIS%2080%20Assignments/6.27.html Line: 17, Column: 20 Source Code: document.write(% , + "<h2> is an odd number</h2>");
I used you example and got results. The output was an even number. Thanks.
|
0

Shouldnt this be num % 2 == 0 and num % 2 != 0. Notice I am using == instead of = and also using the modulus of 2.

Comments

0

Change num % 6 = 0 to num % 2 == 0!

If I was really using this code, I'd probably write it like this:

 if(num % 2){
     document.write("<h2> is an odd number</h2>");
 } else {
     document.write("<h2> is a even number</h2>");
 }

This way, num % 2 evaluates to a truthy or falsy value, so I can use if without a comparison to get a true or false response. Because of the way it responds, I've swapped the odd and even as well.

I'd also not use document.write, instead changing the innerHTML of an element, due to the problems with document.write and XML, among other things.

2 Comments

I still do not understand why he is doing % + "something" (and why you didn't correct it in your answer)...
That's a very good point... What is he doing? Shows the power of copy and paste!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.