1

Here I have a generic calculation function.

private calculateTotal(order: InventoryOrder):number{
  let total = 0.0;
  if(order && order.currentInventory){
    order.currentInventory.forEach(x =>{
      console.log(x.quantity);
      console.log("helloworld");
    }  //error appears here<- "," expected.
  } return total;
}

I tested very similar code at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

    const array1 = ['a', 'b', 'c'];
    
    array1.forEach(element => {
                    console.log(element);
                    console.log("helloworld");
                              });

// expected output: "a"
// expected output: "b"
// expected output: "c"

Any idea what's going on?

4
  • 1
    You are missing a closing parenthesis after the closing bracket after "hello world" Commented Apr 8, 2022 at 20:30
  • 1
    there is no closing parenthesis for forEach Commented Apr 8, 2022 at 20:30
  • I just ran the code locally and it worked fine for me 🤷🏽‍♂️ Commented Apr 8, 2022 at 20:32
  • as an aside, you can try using the angular foreach loop angular.forEach(order.currentInventory, function(value, key) { // test it here }); Commented Apr 8, 2022 at 20:37

1 Answer 1

1
private calculateTotal(order: InventoryOrder):number{
    let total = 0.0;
    if(order && order.currentInventory){
        order.currentInventory.forEach(x =>{
            console.log(x.quantity);
            console.log("helloworld");
        });
    } 
    return total;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I went through your code again and there was a missing bracket, I would highly suggest you use an editor such as VS code or phpstorm that will highlight these issues for you straightaway. Thanks.
Yep! That's correct as outlined by the folks above. I appreciate you taking the time to do that. Marked correct :) - I'll assume you meant parenthesis instead of bracket ;)

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.