As I am pretty dumb and new to arrays, how do you check an added item's index number.
I'm having trouble due to the fact that I can only check an item already in the array, so how do you do it?
Is there a way to check an added item's index number thing (gamelab)
VarrienceLvl 25
- Edited
you mean... something like this?
var a = [1,2,3,4,5]
var b = a.push(6)-1;
console.log(a[b]);
or maybe perhaps indexOf? but that only finds it's first occurrence within the array, if there are duplicates you'll have to brute force it with a for loop
"undefined", but how does it work
VarrienceLvl 25
pluto Array.prototype.push shifts a piece of data into a current array, then returns the current .length attribute of the new array this function also accepts multiple parameters to be allowed to be put into the array but with this you'll only be able to access the last element added to the array.... as for the reason the -1, if you've ever worked with array indices you'll know that it always starts at 0, and the .length attribute is off by one.... by subtracting the return value from .push() and subtracting it by 1 we get the data you just appended to the array.... I don't know how to make this any simpler though.... debugging is a huge part of programming you should have understood that getting undefined from the array meant that you weren't in the correct index to read what you just added
pluto yes.... you can brute force your way through all the elements using a basic iterator, i refered to this via the first post, though asking for a way to check all of them contradicts your post, no?
var arr = [1,2,5,6,9,4,2,0];
for(var i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
personLvl 62
💀
do arrays act like sprites or are they like a value or something how do they collide or interact
simple explanation; one variable, 2 or more pieces of data
ackvonhuelioLvl 41
simple explanation; one value, more value, nested value, headache
personLvl 62
my two hours of sleep brain still cant figure this out