Need help with simple JavaScript code.

Solaris

Party Escort Bot
Joined
Feb 11, 2005
Messages
10,318
Reaction score
4
I'm trying to teach myself Javascript through the Euler Problems.

Problem one is:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

Here is my code:

Code:
<html>
<head>
<script type="text/javascript">
var a = 1;
var b = 0;
var c = 1;
	
	for (c=1;c<=1000;c=c+1)
	
	{
	var x = a%3;
	var z = a%5;

		if (x=0)

					{
						b=b+a;
						a=a+1;
					}
		else if (z=0)
					{
						b=b+a;
						a=a+1;
					}
					
		else		{
						a=a+1;
					
					}
	}
	
alert(b);


</script>
</head>
</html>

Here's an explanation of what the code should do.

It should cycle through each number between 1 and 1000. For each number, it checks if it is divisible by 3, if it is it adds it to the total (b), if it is not, it checks if it is divisible by 5, if it is, it adds it to the total (b).

If it is divisible by neither 3 nor 5, it goes onto the next number.

However I seem to be having some problem with the modulus function.

When a=3, x should equal 0 and so b should increase by the number 3.

However whenever I run the code. It always returns the answer 0. This means that for every number 1-1000, none of the numbers are multiples of either 3 or 5. I cannot work out why this is happening.

Many thanks.

Solaris
 
Haha, everyone has had this problem at some point. Wanna know what the problem is?

You're assigning the number 0 to x and to z (x = 0, z = 0) instead of comparing them to 0 (x == 0, z == 0). So you just override the value that was assigned to it by the modulus operation. And because 0 is equivalent to false in Javascript, it doesn't enter the if statement.
 
Haha, everyone has had this problem at some point. Wanna know what the problem is?

You're assigning the number 0 to x and to z (x = 0, z = 0) instead of comparing them to 0 (x == 0, z == 0). So you just override the value that was assigned to it by the modulus operation. And because 0 is equivalent to false in Javascript, it doesn't enter the if statement.

Thanks a lot dude!
 
^I was proud to correctly recall my high school C++ knowledge and figured out the problem, but you beat me to it! ;)

I was trying to figure out something else when I noticed you posted - I tested the algorithm for c<=10 and it returned 33, instead of 23 (it worked correctly for c<=9) but I don't yet see where's the problem.

EDIT: Oh *below* 10. So the loop should continue until c<10 and, in your case c<1000.
 
A simplification of your code:

Code:
var sum = 0;

for(var n = 0; n < 1000; n++) {
    if(n % 3 === 0 || n % 5 === 0) {
        sum += n;
    }
}
 
Optimization, that's a few additional FPS right there! ;)

But '===' (three '=' signs)? Does that mean something, or is this a typo?
 
Three equal signs also compare the type of variable it is (intiger, boolean, word). So if you try to compare var=true to var="true" you would get false since one is a boolean and the other is a string. Atleast thats how it works in PHP and I assume javascript is the same.
 
Optimization, that's a few additional FPS right there! ;)

But '===' (three '=' signs)? Does that mean something, or is this a typo?

It means the two objects have to be both equal and of the same type as opposed to just equal.

Also... am I the only one who sees the code blocks and the completely unreadable light gray on white? Why is that still that way? Discouraging people from posting code?
 
Optimization, that's a few additional FPS right there! ;)

But '===' (three '=' signs)? Does that mean something, or is this a typo?

No, Javascript is just a silly language. When you use ==, it will use something called type coercion, as in: it will try to convert the types on both sides of the equality signs to the same type. It's more like equivalence than equality and kind of wonky really. When you use three (===) it won't do this. This is faster and less prone to error in most cases.

For example:

"1" == 1 (is true)
"1" === 1 (is false, left side is a string, right side is a number)
"sdfsdf" == true (is true)
"sdsdf" === true (is false, because left side is a string, right side is a boolean)

And the biggest WTF of them all:

"\n\t" == false (is true, because a line break and a tab are white space and so equivalent to false)
 
Back
Top