wdi-hk-sep-2014 / notes

Notes for WDI course
3 stars 5 forks source link

Quick challenge on removeAt #4

Closed harryworld closed 10 years ago

harryworld commented 10 years ago

Write a function called removeAt that takes two arguments: an array and a number n. The function should return an array with the element at the nth position removed.

removeAt([25, 33, 6, 2, 3], 2)
> [25, 6, 2, 3]
hexaballs commented 10 years ago
removeAt = (array, num) ->
  array.splice(num-1, 1)
  array

console.log removeAt [25, 33, 6, 2, 3], 2
johnlok commented 10 years ago

testArray = [1...10] removeAt = (array, n) -> array.splice(n-1,1)

removeAt testArray, 4 console.log testArray

songyeep commented 10 years ago
removeAt = (array, n) ->
    array.splice(n-1, 1)
    array

arrayOne = [25, 33, 6, 2, 3]

console.log removeAt arrayOne, 2
#-> [25, 6, 2, 3]
kshimatsu commented 10 years ago

removeAt = (array, n) -> array.splice(n-1, 1) array

jasonwhlaw commented 10 years ago

removeAt = (array, n) -> array.splice(n-1,1) array

arrayTest = [25, 33, 6, 2, 3]

console.log removeAt(arrayTest, 2)

-> [25, 6, 2, 3]

makzimillian commented 10 years ago

removeAt = (array, n) -> array.splice(n-1, 1) array
console.log removeAt([25, 33, 6, 2, 3], 2)

jasonwhlaw commented 10 years ago

removeAt = (array, n) -> array.splice(n-1,1) array

arrayTest = [25, 33, 6, 2, 3]

console.log removeAt(arrayTest, 2)

titaniumtails commented 10 years ago

array = [25, 33, 6, 2, 3]

removeAt = (array, 1) -> array.splice(pos-1, 1) array

removeAt(array, 2)

==OR===

array = [25, 33, 6, 2, 3]

removeAt = (array, pos, howmany) -> array.splice(pos-1, howmany) array

removeAt(array, 2, 1)

AeroWong commented 10 years ago

Array = [25, 33, 6, 2, 3]

removeAt = (array, n) -> array.splice(n-1, 1)

removeAt(array, n)

yogafire commented 10 years ago

removeAt = (array, n) -> array.splice(n-1, 1) array console.log removeAt([25, 33, 6, 2, 3], 2)

harryworld commented 10 years ago
removeAt = (array, n) ->
  array.splice(n - 1, 1)
  array

console.log removeAt([25, 33, 6, 2, 3], 2)
sushiogoto commented 10 years ago

In javascript:

removeAt = function(array, n) { for(var i = array.length; i >= 0; i--) { if(array[i] === n) { array.splice(i, 1); }; }; return array }

hello = [1, 2, 3];

removeAt(hello, 2);