Skip to content

Instantly share code, notes, and snippets.

@johntran
Created September 14, 2017 18:42
Show Gist options
  • Save johntran/2142c16d2b2ebbb234a03a4c91f92614 to your computer and use it in GitHub Desktop.
Save johntran/2142c16d2b2ebbb234a03a4c91f92614 to your computer and use it in GitHub Desktop.
/**
* Can you explain splice for me? I know what it is,
* but there aren't many example to use it so I don't understand
* everything that goes inside the parentheses
*/
/**
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
* Array.splice(x,y)
* x is what index you start at
* y is how many items you want to remove
*/
var fruits = ['kiwi', 'apple', 'blueberry'];
fruits.splice(1, 2);
/**
* apple is in index 1
* remove two items starting from 'apple'
* return ['kiwi'];
*/
/**
* Also, I don't get why this problem was wrong when I was putting in
* return facebookProfile.friends + 1 &
* return facebookProfile.friends - 1
* I thought ++ was +1 & -- was -1?
*
* Answer:
* Let's say facebookProfile.friends = 3.
* facebookProfile.friends + 1 returns 4.
* but when you do "facebookProfile.friends", it returns 3.
* you didn't add a friend. you just returned a value of friends and added one to it
*
* facebookProfile.friends++ is equal to
* facebookProfile.friends = facebookProfile.friends + 1
* you are reassigning facebookProfile.friends to equal its current value (3) plus 1
*
* x + 1 is not a reassignment
* x++ is a reassignemtn
* x = x+1 is a reassignment
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment