Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Tadjiev Codes
Tadjiev Codes
9,626 Points

JS Grocery Store small project issues with the Arrays and IF conditional statements

Hi, Dear Fellows,

Firstly, I'm having a problem to display the Array Items by a number like an item 1 item 2 items on the display. I used

 ` Item #1:  ${produce[index] }   <br>`;

But it shows all the items with the number Item 1. Secondly, I need to Validate that the value is valid based on the total number of elements in the currently selected array. If not valid, Do not add item to cart, alert the user that the value passed was invalid. I tried using a big function called addItemToCart() But this

if (departmentSelection.value === "Produce") {
                if (gettingTxtBxValue2.value > produce.length) {
                    alert("The value passed is invalid, Please try again!");

if statement doesn't really listen like if its value selected in dropdown 10 20 but array length is 7 it still outputs the else statement that I have.

Third of all, I'm outputting the result through this line of code as follows:

cartOutputDiv.innerHTML += gettingTxtBxValue2.value + "<br>" + cheese + "<br>";

Although I would want to connect here my empty array called from line 107:

let shoppingCart = []; 

So that all the items eventually get added to the shoppingcart. Or would need a .push() array method again to add the selected items inside of the new array?

The link to my Workspace snapshot: https://w.trhou.se/oj2sotllh7

Thanks a lot for your help.

Appreciate it))))))))

Steven Parker
Steven Parker
231,153 Points

Looks like this workspace has the same issue mentioned on the status page about ones from last week. You might want to let the Support folks know it's still happening.

Bob Swaney
Bob Swaney
13,010 Points

hello. to get the Item # to change..you hard coded 'Item #1' into your IF statements. you need to have something like:

 `Item#${index} ${produce[index] }   <br>`;

so that as the loop iterates through, it also adds the index number next to the pound sign

1 Answer

Tadjiev Codes
Tadjiev Codes
9,626 Points

I already solved the problems YAY . Thanks)))))))))