Skip to content

Commit

Permalink
add push and pop tests b00tc4mp#160
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbarzi committed Sep 26, 2024
1 parent 6851e53 commit da13321
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
21 changes: 21 additions & 0 deletions staff/manuel-barzi/playground/arrays/Array.prototype.pop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
console.log('TEST Array.prototype.pop')

console.log('CASE extract tomato from veggies')

var veggies = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']
var veggie = veggies.pop()

console.log(veggies)
// ['broccoli', 'cauliflower', 'cabbage', 'kale']
console.log(veggie)
// tomato

console.log('CASE extract yellow from colors')

var colors = ['red', 'green', 'blue', 'yellow']
var color = colors.pop()

console.log(colors)
// ['red', 'green', 'blue']
console.log(color)
// yellow
21 changes: 21 additions & 0 deletions staff/manuel-barzi/playground/arrays/Array.prototype.push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
console.log('TEST Array.prototype.push')

console.log('CASE add 400 to nums')

var nums = [100, 200, 300]
var length = nums.push(400)

console.log(nums)
// [100, 200, 300, 400] (4)
console.log(length)
// 4

console.log('CASE add banana to fruits')

var fruits = ['apple', 'orange', 'raspberry', 'pineapple']
var length = fruits.push('banana')

console.log(fruits)
// ['apple', 'orange', 'raspberry', 'pineapple', 'banana'] (5)
console.log(length)
// 5
36 changes: 36 additions & 0 deletions staff/manuel-barzi/playground/iterables/pop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var pop = function (iterable) {
/*
extract last element from iterable
descrease length from iterable
delete last element from iterable
return extracted element
*/

var last = iterable[iterable.length - 1]

delete iterable[iterable.length - 1]

iterable.length--

return last
}

console.log('CASE extract tomato from veggies')

var veggies = { 0: 'broccoli', 1: 'cauliflower', 2: 'cabbage', 3: 'kale', 4: 'tomato', length: 5 }
var veggie = pop(veggies)

console.log(veggies)
// { 0: 'broccoli', 1: 'cauliflower', 2: 'cabbage', 3: 'kale', length: 4 }
console.log(veggie)
// tomato

console.log('CASE extract yellow from colors')

var colors = { 0: 'red', 1: 'green', 2: 'blue', 3: 'yellow', length: 4 }
var color = pop(colors)

console.log(colors)
// { 0: 'red', 1: 'green', 2: 'blue', length: 3 }
console.log(color)
// yellow
28 changes: 28 additions & 0 deletions staff/manuel-barzi/playground/iterables/push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var push = function (iterable, element) {
iterable[iterable.length] = element
iterable.length++

return iterable.length
}

console.log('TEST push')

console.log('CASE add 400 to nums')

var nums = { 0: 100, 1: 200, 2: 300, length: 3 }
var length = push(nums, 400)

console.log(nums)
// { 0: 100, 1: 200, 2: 300, 3: 400, length: 4 }
console.log(length)
// 4

console.log('CASE add banana to fruits')

var fruits = { 0: 'apple', 1: 'orange', 2: 'raspberry', 3: 'pineapple', length: 4 }
var length = push(fruits, 'banana')

console.log(fruits)
// { 0: 'apple', 1: 'orange', 2: 'raspberry', 3: 'pineapple', 4: 'banana', length: 5 }
console.log(length)
// 5

0 comments on commit da13321

Please sign in to comment.