0

I'm giving the first Project Euler question a go: "sum of all the multiples of 3 or 5 below 1000".

I tried running the Javascript portion in jsfiddle as standalone Javascript but nothing happened when I press run. Then I tried putting it in an HTML file and opened it in Chrome and nothing happened. I don't know if there's something about running Javacript (such as using HTML) or something is wrong with my code.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <head>
</head>
</head>
<body>
<p>ha</p>
<script>
    var solution1 = function(input){
        var sum = 0;
        for(var i = 1; i < input; i++){
            if(i % 3 === 0){
            sum += i;
            }
            else if(i % 5 === 0){
            sum += i;
            }
            return sum;
        };
    var input = 1000;
    console.log(solution1);
</script>
</body>
</html>
2
  • Do you open your Chrome Debugger? console.log logs your output into the console, not the webpage itself. Commented May 23, 2015 at 2:49
  • 2
    To output something in HTML, you have to use the DOM API. For instance, in this case, you can give your p element an id of output, and then use the DOM API to update its contents: document.getElementById('output').innerHTML = input; Commented May 23, 2015 at 2:51

4 Answers 4

1

You're missing a closing bracket and you're not invoking solution1. Try writing it like this.

function solution1(input) {
    var sum = 0;
    for (var i = 1; i < input; i++) {
        if (i % 3 === 0) {
            sum += i;
        } else if (i % 5 === 0) {
            sum += i;
        }
        return sum;
    }
}
var input = 1000;
solution1(input);

if you want your output to show up in the console rather than returning the sum log it out with console.log

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

Comments

1

You are not writing directly to the document. Try console.log(solution1()) or document.getElementById("mydiv").innerHTML.append(solution1()) instead.

console.log will log the result in the Javascript Console, which you may access differently depending on your browser.

document.getElementById("mydiv").innerHTML.append(solution1()) will search your webpage for a div with id="mydiv" and add your answer into the div.

Additionally, you forgot the close the bracket for the function declaration. This means that the parser thinks that your function call is part of the function itself, which will result in an error. You can check your javascript console for errors.

You're also not calling the function correctly. The function itself doesn't know what to take as input. You need to call it like this: solution1(input). This passes input as a parameter.

Comments

0

The JavaScript you wrote does not write directly to the webpage at all.

console.log() however can be used to output something to the console, as your script does. For more info have a look at the docs.

You can access the console in most browsers by pressing F12.

Note that you have a couple of issues in your code, too, as gautsch points out. These issues will make the output as you would expect it impossible for your code.

Comments

0

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <head>
</head>
</head>
<body>
<p>ha</p>
<script>
    var solution1 = function(input){
        var sum = 0;
        for(var i = 1; i < input; i++){
            if(i % 3 === 0){
            sum += i;
            }
            else if(i % 5 === 0){
            sum += i;
            }
            alert(sum);
        };
      };
    var input= 1000;
    solution1(input);
</script>
</body>
</html>

4 Comments

return will stop function,so you should alert
This code will produce 1000 alert message boxes. If you are using Chrome and tick the box to prevent them, then the page will need to be closed and reopened before the alert boxes start working again.
yes!you are right,so wo can console.log instead of alert
Yes I would use console.log or document.write. The latter seems more appropriate in this situation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.