Assistance with Arrays


Im been having a hard time with the string exercise (P1C5.1) I believe I am entering all the information correctly but when I run the code I receive error messages.
" 1. Under the first //TODO line, declare a string array with five elements called colors
2. Under the second //TODO line, fill the array colors with 5 elements. These elements should be the colors red, yellow, orange, green, blue.
3. Under the third //TODO line, replace the color green with emerald using the array index."
These are the instructions. If you can show me how it supposed to be entered it would be greatly appreciated.

Repl link:

code snippet
1 Like

Correct me if I’m wrong, but this sounds like a homework assignment.


No, I have been self-studying some coding using open classroom/Replit practice. I just haven’t been able to enter it correctly.

1 Like

What language is this in? Also, could you show us what you’ve got so far?

1 Like


1 Like

That is Java, not JavaScript, they are two very different languages despite their similar names.

Take a step back for a minute, how do you declare a variable as an array? You use the data type that you will use in the array, followed by square brackets, then the variable name:

datatype[] variable_name;

So if you want to declare an array of strings, you will need to use theStringdatatype to make this array.

When we declare an array, we can also optionally declare it’s size or assign to it directly.

To declare an array’s size, we use the assignment operator = after the variable name, followed by the new keyword, then square brackets and in those squares brackets we put the array’s length.

datatype variable_name = new datatype[size];

To assign an array instead, we use use the assignment operator, but this time, followed by curly brackets and inside those brackets will be our comma separated array items:

datatype variable_name = { item1, item2, etc };

Also remember, when we make a string, the string must be between quotation marks (").