bhalash

Bug explanation.

Apr 14th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // An array is an ordered list of items, with a starting index of 0.
  2. var array = [];
  3.  
  4. // foo.push(bar) adds item bar to the end of array foo.
  5. array.push('cat');
  6. array.push('dog');
  7. array.push('bird');
  8.  
  9. console.log(array.length); // 3
  10. console.log(array[0]); // cat
  11. console.log(array[2]); // bird
  12. array.push('fish'); // Push 'fish' to 'array'
  13. console.log(array.length); // 4
  14.  
  15. // Empty the array-delete everything in it.
  16. array = [];
  17.  
  18. console.log(array.length); // 0
  19. array = ['cat', 'dog', 'bird', 'fish']; // Same as push, except all at once.
  20. console.log(array.length); // 4
  21.  
  22. // A multidimensional array is an array with an array.
  23. // Rather than a single item at each index, there is an array at each index.
  24.  
  25. // Same as above, except each array item is an array.
  26. array = [
  27.     ['cat', 'dog', 'bird', 'fish'],
  28.     ['apple', 'orange', 'melon', 'pear'],
  29.     ['blue', 'green', 'red', 'yellow']    
  30. ];
  31.  
  32. console.log(array[0][1]); // dog
  33. console.log(array.length); // 3
  34. console.log(array[1].length); // 4
  35. console.log(array[2][2]); // red
  36. console.log(array.[2][2].length); // 3 (3 characters in 'red')
  37.  
  38. // Every language, programming or spoken, is different.
  39. // I mistakenly believed that a multidimensional array had to be declared as such.
  40. // So I declared:
  41. array = [[]];
  42. // This declared an empty array, and added an empty array as the first element.
Advertisement
Add Comment
Please, Sign In to add comment