Learn Form Validation by Building a Calorie Counter - Step 57

Tell us what’s happening:

I’m not sure what’s wrong with my code. I’ve tried a few more iterations, but I feel like I’m missing some knowledge. Perhaps some getElementByValue( ) method, or something???

Your code so far

function getCaloriesFromInputs(list) {
  let calories = 0;
  for (let i = 0; i < list.length; i++) {
    const currVal = list.value[i];
  }
}

If it helps at all, here is what I get in the console:

// running tests
You should access the element in list at index i using bracket notation.
You should access the value attribute of the element in list at index i.
You should assign the value of the element in list at index i to a variable called currVal.
// tests completed

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn Form Validation by Building a Calorie Counter - Step 57

first correctly decide which keyword you have to use to instantiate a variable var, let, const.
Choose the appropriate keyword (var , let , or const ) based on your use case and the desired scoping and mutability behavior. const is often preferred when you know the variable should not be reassigned, and let is commonly used when reassignment is needed. var is still used in some scenarios, but its usage has been largely replaced by let and const in modern JavaScript.
so, your choice should be between let, and const.since your are inside a loop and reassigning currVal every time until the loop ends. which would be correct to select from the two?
Next, how do you access the elements of an array list with index i. the console is exactly telling you what to do(line 1)

, it is list[i], then please read line 2 in the console result or output

so, list[i].value

so,finally do exactly what it says you to do.

2 Likes

Thank you, this solved my problem.

But may I ask, why is it list[i].value and not list.value[i]?

The way the step is phrased makes it seem like the goal is to access the value at index i of the array list. Is there something I am missing?

1 Like

actually using const would n’t be a problem , Since the value of currVal changes in each iteration, using const is suitable because it ensures that you don’t accidentally reassign a new value to currVal within the loop after the initialization. It also helps communicate the intention that currVal is meant to be constant within each iteration.

because, list is an array, so does arrays have .value property or method like `map,forEach,reduce,length, etc. of course not the value property is on each of the input elements so first you should access each of the input elements from the array as exactly the console tells you

after that you can access the value of each input using list[i].value.
so order matters.if my suggestion has solved your problem so be sure to select or click on the solution button on the reply above so, that others can easy find it.