1

I tried to make a function that writes some divs in an HTML document. Everything is working fine but I want to try to change the function declaration from this:

function writeDiv(divDOMelement, numberOfExcercises, numberOfTasksInEx);

To something like this

function writeDiv(divDOMelement, {numberOfExcercises:N, numberOfTasksInEx:[v]}) 

Just to be clear, the first declaration works perfectly, but when I tried implementing the second one, and changing the function call I received an error.

Here is the full code:

let open = false;
let currentlyOpen;

function writeDiv(divDOMelement, {numberOfExcercises:N, numberOfTasksInEx:[v]}) {
  for (let i = 0; i < numberOfExcercises.N; i++) {
    // ... didnt include the code that prints the divs on the site just to make it easier to read.
    for (let j = 0; j < numberOfTasksInEx.v[i]; j++) {
    // ... same as the first loop.
    }
  }
}


$(document).ready(function () {
  writeDiv("#mainDiv", {numberOfExcercises:3, numberOfTasksInEx:[5,2,3]});
});

Just to clarify, this numberOfTasksInEx should be an array, the first element of the array tells me that the first exercise will have that many tasks, the second tells me that the second exercise will have that many tasks, and so on.

The error I am getting:

jQuery.Deferred exception: numberOfExcercises is not defined 
Uncaught ReferenceError: numberOfExcercises is not defined
5
  • Why you want to implement the second one? Commented Dec 29, 2021 at 14:42
  • @Leau I dont usually stop at the first solution I get. I want to learn as much as I can, and that is why I posted this on stack overflow. Commented Dec 29, 2021 at 14:43
  • 1
    Just put { numberOfExcercises, numberOfTasksInEx }? Commented Dec 29, 2021 at 14:44
  • 2
    Hello, out of curiosity, what reference did you use for the second declaration? It appears you are attempting to use types on those object properties. If you just came up with that syntax, I recommend you find a reliable source to answer these types of questions. You can start here Commented Dec 29, 2021 at 14:44
  • @RandyCasburn I misread it somewhere probably. Thank you for the link :) . Commented Dec 29, 2021 at 14:48

1 Answer 1

3

Your declaration syntax is incorrect. All you need is:

function writeDiv(divDOMelement, {numberOfExcercises, numberOfTasksInEx})

Then inside your function you'll have numberOfExercises as the overall count, and numberOfTasksInEx as the array of per-exercise tasks.

The declaration syntax extracts those properties from the single object passed in as the second parameter to the function in a process called "decomposition". The : in your version does something, but not what your code expects: it allows the property name from the source object (the actual parameter) to be renamed. That's not really necessary here, but for example numberOfExercises:N would mean that in your function you would refer to simply N, not numberOfExercises.N.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.