There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
The exercise tells me that tuples have fixed lengths. I wanted to see what happened if I tried to add values to it, I wasn’t able to add values by doing array[6] = ‘x’, but I was still able to do array.push(‘x’). When I printed to the console the length, it showed an increased length (now length 7 instead of 6), I’m also able to print the entire tuple, which includes my newly pushed value. I’m still unable to access the element by doing array[6], but when I do array.forEach(element, index), I’m able to print the value at array[6]. I’m curious as to why this is happening, any insight?
Maybe I’m missing something, but I’m still confused. If it created an array, why can I not access that element directly? It’s still giving me the “no element” error.
Still pretty hazy with it comes to TS. Will need to go back and reread the unit that describes TS tuple objects. Sorry I couldn’t be more help. My bad.
Specifically, we can’t assign an array to a tuple variable, even when the elements are of the correct type
Is it meant to say “we can’t assign a typed array to a tuple variable” or am I mistaken because in the used example, it looks like tup is being assigned an array, namely ['there', 'there']
let tup: [string, string] = ['hi', 'bye'];
let arr: string[] = ['there','there'];
tup = ['there', 'there']; // No Errors.
tup = arr; // Type Error! An array cannot be assigned to a tuple.
Sometime later in 2021 I found out that in Javascript you can create arbitrary properties onto an object (which includes arrays), and then assign values to those properties. kind of like a dictionary (Python terminology), or hashmap (Java terminology?). Although that was Javascript, and this tutorial was for TypeScript, there might be some similarities there. Just posting this as an “in case you were curious”. Started using codecademy again after a long hiatus, and saw a notification on this thread from the above post by micro, heh.
It’s been three years since you asked the question, but I think I can explain why this is happening. It will be useful to others.
When we run the TS compiler, it finds an error in the line:
console.log(favoriteCoordinates[6]);
Indeed, there is no element under index 6. For now…
This is because the command favoriteCoordinates.push(-6.825); has not been executed yet.
Thus, the compiler does not take into account the code that will be executed in the future and looks at the fact.
But we can use a trick that will not displease the compiler and get access to the last added element [6].