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:
Here is my code:
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
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