Could someone please help me with this javascript exercise?

Joined
Nov 9, 2021
Messages
3
Reaction score
0
I have just started teaching myself and I'm stuck on this exercise.
'Write a program that asks the user for a number n and prints the sum of the numbers 1 to n'

I will attach my code so you can see where I'm wrong.

Thanks in advance, guys!

ps: 'eingabe' is the german word for input
 

Attachments

  • Screenshot (24).png
    Screenshot (24).png
    131.3 KB · Views: 25
Joined
Nov 13, 2020
Messages
302
Reaction score
38
Pictures are not a help when it comes to code. Use the < / > and paste your code. Line 16 should be removed.
Defining result should be done before (outside) the for loop. -> With it inside you keep making it zero for every iteration.
Plus 'result = '';' always defines a string in JS. so use
result = Number(result + i);

I don't think the $ dollar signs are needed.
 
Joined
Nov 9, 2021
Messages
3
Reaction score
0
JavaScript:
<html>
    <body>
        <script>
            "use strict"
            let eingabe = Number(prompt('Give me a positive integer:'));

            function findSum(eingabe) {
             let result = 0;
             result = Number(result + i);
             for(let i = 1; i <= eingabe; i++)
            
             return result
            }

        console.log(`Sum of numbers from 1 to {eingabe} is {findSum(eingabe)}`)
        </script>
    </body>
</html>


I have tried to adjust the code but it still seems to not work.
When running the code in my browser I get the pop-up field but nothing after that.
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
I like making my variables global unless there is a good reason not to.
You didn't write anything inside the loop.
Why are you using curly brackets? They are not needed.
You are not using single quotes in the last statement. You're using an acute or backtick. The single quote is on the same key as the double quote.
You are enclosing the variables in the quote. See how I do it.
I use alerts (Old school). Don't like opening the console to see one thing.

Always end a statement with a semicolon.

Code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>

<body>
<script>
var eingabe = Number(prompt('Give me a positive integer:'));
var result = 0;
function findSum(eingabe){
    for(i=1; i<=eingabe; i++){
        result = Number(result + i);
    }
    return result;
}
alert('Sum of numbers from 1 to ' + eingabe + ' is ' + findSum(eingabe));
</script>
</body>
</html>
 
Joined
Nov 13, 2020
Messages
302
Reaction score
38
It defines the character set used to write the website. We should also declare the language.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top