0

what is the actual syntax for the for loop? what I want to do is to calculate 2 number variables inside a function using a for a loop. I am going over and over the internet and I cannot find anything that can help me specifically for this task I was given to do. I am not looking for the answers, just for a guideline or a hint. or just give me the syntax and I will try my best to adjust it to my needs. just so you have a better understanding of the task, here it is.

Features
Instead of hardcoding Y (the result of the Fibonacci of X), calculate it with a for loop
The calculation should be wrapped in a function, that gets X as an argument, and returns Y
After the function, you should call the function, and assign the returned value in your HTML to present to the user

btw, i know that i did not put the innerHTML correcly, and also i did not do the syntax right, this is actually what i am asking here.

thank you!!

i have tried this:

var answer = document.getElementsByClassName("paragraph")
function calc (x){
    for (let a = 2; a <= x; a++){
        answer = document.textContent("this is a pr")
    }
    return calc
}
calc(2)
  • 2
    The syntax for *the loop* is OK. The code `.innerHTML('this is a pr')` isn't. `innerHTML` is a property, not a method. But also `answer` is a NodeList, not even a single element. And `.document` also seems wrong. – VLAZ Nov 01 '22 at 14:55
  • You are supposed to `x` in the `for` loop: `for (let a = 2; a <= x; a++) {`. Also: [Generating Fibonacci Sequence](https://stackoverflow.com/a/7944251) – 001 Nov 01 '22 at 14:58
  • ok, i updated the code as u can see... i am seriousely cannot see the problem here now.. what do they mean i am not defining the variable answer??? – JavaScriptStudent Nov 01 '22 at 15:12
  • your use of `innerHTML` is incorrect. It must be `innerHTML = 'string';`. Then `answer.document` is incorrect for 2 reasons. 1st, `document.querySelectorAll` returns a `Node List` not a single element, and as such an iteration or index must be used. 2nd, the `document` is wrongly used here. `document.element.document` makes no sense! – tacoshy Nov 01 '22 at 15:14
  • PS after you updated your code the issue remains. `getElementsByClassName` returns a `HTML Collection Object` and also needs either an index or iteration. – tacoshy Nov 01 '22 at 15:16
  • Your function should return the answer value, you are returning the function itself. – Arleigh Hix Nov 01 '22 at 15:19

2 Answers2

1

You should avoid to use classname, instead use .querySelectorAll
You don't need to specify how many answer exists if your calc() is just 2.

let answer = document.querySelectorAll(".paragraph");
    
function calc(x) {
  for (let a = 0; a <= x; a++) {
    answer[a].innerText = "this is a pr";
  }
}

calc(2);
<p class="paragraph">A</p>
<p class="paragraph">B</p>
<p class="paragraph">C</p>
<p class="paragraph">D</p>
<p class="paragraph">E</p>

I don't know if this is what you're searching for but it's this is the standard for loop function to achieve these types of result.

Frash
  • 67
  • 1
  • 9
  • this does not help with the instructions I have, but thanks for the knowledge.. will use this! but how can I make a function return the value of x and y? when x is 3, a is 5, and y is 3 + 5..? I know how to write it in an if statement.. but inside a value, and with using a for loop? no way.. can someone help me? – JavaScriptStudent Nov 01 '22 at 17:17
  • @JavaScriptStudent `return y = x + a` simple as that. – Frash Nov 01 '22 at 19:31
0

function calc(x){
  let sum = 0;
  for (let a=0; a<x; a++){
    sum+=a;
  }
  return sum;
}
console.log(calc(5));

The Syntax of the loop in your code is correct, the thing is that you cannot return your function inside itself without calling it as you have done in your code.

'return calc' is wrong. You should use 'return calc(argument-Variable or value)' as you have a parameter passed in your function, but this should only be used if you are writing a code of recursion. If not you should return a variable. Refer to the code below for further clarifications

  • it is not working for me tho.. i have no idea what i am doing wrong here. i need to calculate both variables for example let a = 5 let b = 3 let c = a + b and i need it all to be in a for loop inside a function... im getting crazy over here.. it seems impossible. – JavaScriptStudent Nov 01 '22 at 17:26
  • I have modified my answer for more clarification. Please refer to that. Also, remove the "return calc" from your code and return the desired value or console.log() the desired value. You can reach out for more clarification. – Tarun Kumar Sao Nov 02 '22 at 18:23