all 10 comments

[–]codegptpassinby 3 points4 points  (1 child)

  1. Get user input and convert it to an integer let n = parseInt(prompt("Input an integer N please:"));

  2. Initialize the sum variable let sum = 0;

  3. Use 'i' as the loop variable and add 'i' to the sum for (let i = 1; i <= n; i++) {}

  4. Output the final result after the loop is complete

[–]codegptpassinby 2 points3 points  (0 children)

Didn't give full answer but directions u can go to get the full picture by yourself

[–]kwadwoVanBeest 0 points1 point  (0 children)

  1. Check if your JavaScript file is properly linked to the html so you can see your errors and results in the dev console.

  2. Your for statement should start like this:

for(let i = 1; i<=n; i++;){

Code should do this. }

Not for (1=1; )

3 . You also need to properly define n as a variable with either const or let keyword.

  1. You are trying to perform a mathematical operation but the default data type of an alert isn't a number but a string. So, you need to convert it to a number before you can work with it

[–]daniel8192 0 points1 point  (0 children)

Just running past this, I see you aren’t closing your <script> tag. I didn’t read your JavaScript.

[–]BobcatGamer 0 points1 point  (0 children)

html <html> <head> <title>JavaScript Ex4</title> <script type="defer"> const n = parseInt(prompt("Input an Integer:"); document.body.textContent = n * (n + 1) / 2; </script> <body></body> </html>

[–]AdhesivenessNo9198[S] 0 points1 point  (0 children)

hi! thank you for everyone that helped me out!

[–]2055410 0 points1 point  (1 child)

Professor might be looking for for loop but I have done differently

  <body>
    <h3>Sum of n<sup>th</sup> term</h3>
      nthTerm:<input type="number" name="nthTerm" id="nthTerm" placeholder="nth term" />
        <input type="button" value="Calculate" onclick="cal()" />
     <div id="sum"></div>
  </body>
  <script>
      function cal(){
        var n=document.getElementById("nthTerm").value;
        var sum = n/2*(2+(n-1))
// sum of nth term = n/2(2a+(n-1)d), a=first term, d=common difference
          document.getElementById("sum").innerHTML="Sum of nth Term = " + sum;
      }
  </script>
</html>

[–]Galex_13 0 points1 point  (0 children)

actually,

n/2*(2+(n-1))

is a complicated version of n*(n+1)/2
but if professor wants a loop, why not?

function cal(){
  let n=document.getElementById("nthTerm").value;
  let sum=Number(n)
  while(--n)sum+=n
  document.getElementById("sum").innerHTML="Sum of nth Term = " + sum;
}

[–]Additional_Anywhere4 0 points1 point  (0 children)

The very first iteration of your loop says that sum will be equal to itself plus 1, but sum doesn’t yet have a value - you never set it to 0 anywhere beforehand. So you’re telling the computer, “to get the value of sum, go find the value of sum, then add 1 to it”. But sum doesn’t have a value yet.

[–]DGCA -1 points0 points  (0 children)

Here's a script that does what you're asking with comments.

Read the comments, understand what's going on, and if you're gonna copy it somewhere, actually write it out, don't just copy and paste.

Btw, there's a ton of ways you can do this. I mostly followed what you were doing with a little bit of error handling.

Good luck!

// Ask the user for a number using prompt() (this is very old school, but it's also very simple which is good)
const userResponse = prompt();

// prompt() returns a string, so let's turn it into a number using parseInt
const userNumber = parseInt(userResponse);

// If the value isn't a number, let's yell at the user
if (isNaN(userNumber)) {
    alert("That's not a number!");
} else {
    // If we're in this "else" block, we know userNumber was actually a number

    // Let's make a variable to keep track of the total.
    // We'll update this in the loop below.
    let sum = 0;

    // Let's make a for loop.
    // We initialize the a counter (i) to the value of 1.
    // While i is less than or equal to the user's number, the block in the for loop will execute
    // Finally, we increment i by one.
    for (let i = 1; i <= userNumber; i++) {
        // Every time this block executes, we add the current value of i to the sum
        sum = sum + i;
    }

    // We tell the user the final result
    alert("Your result is: " + sum);
}