Skip to content

Commit

Permalink
add forEach test and implementation b00tc4mp#160
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbarzi committed Oct 2, 2024
1 parent 5cf19fc commit e646ebe
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
25 changes: 25 additions & 0 deletions staff/manuel-barzi/playground/arrays/Array.prototype.forEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
console.log('TEST Array.prototype.forEach')

console.log('CASE print characters in array')

var chars = ['a', 'b', 'c']

// chars.forEach(function (element) { console.log(element) })
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

var printElement = function (element) { console.log(element) }

chars.forEach(printElement)

console.log('CASE sum numbers from array')

var nums = [100, 200, 300]
var result = 0

nums.forEach(function (num) {
result += num
})
console.log(result)
// 600
24 changes: 24 additions & 0 deletions staff/manuel-barzi/playground/iterables/forEach.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var forEach = function (iterable, callback) {
for (var i = 0; i < iterable.length; i++) {
var element = iterable[i]

callback(element)
}
}

console.log('TEST Array.prototype.forEach')

console.log('CASE print characters in iterable')

var chars = { 0: 'a', 1: 'b', 2: 'c', length: 3 }

// forEach(chars, function (element) { console.log(element) })
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

var printElement = function (element) {
console.log(element)
}

forEach(chars, printElement)
35 changes: 35 additions & 0 deletions staff/manuel-barzi/playground/iterables/forEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var forEach = function (iterable, callback) {
for (var i = 0; i < iterable.length; i++) {
var element = iterable[i]

callback(element)
}
}

console.log('TEST Array.prototype.forEach')

console.log('CASE print characters in iterable')

var chars = { 0: 'a', 1: 'b', 2: 'c', length: 3 }

// forEach(chars, function (element) { console.log(element) })
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

var printElement = function (element) {
console.log(element)
}

forEach(chars, printElement)

console.log('CASE sum numbers from iterable')

var nums = { 0: 100, 1: 200, 2: 300, length: 3 }
var result = 0

forEach(nums, function (num) {
result += num
})
console.log(result)
// 600
45 changes: 45 additions & 0 deletions staff/manuel-barzi/playground/raids/Raid.prototype.forEach.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
var Raid = function () { this.length = 0 }

Raid.prototype.forEach = function (callback) {
for (var i = 0; i < this.length; i++) {
var element = this[i]

callback(element)
}
}

console.log('TEST Array.prototype.forEach')

console.log('CASE print characters in iterable')

var chars = new Raid
chars[0] = 'a'
chars[1] = 'b'
chars[2] = 'c'
chars.length = 3

// forEach(chars, function (element) { console.log(element) })
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

var printElement = function (element) {
console.log(element)
}

chars.forEach(printElement)

console.log('CASE sum numbers from iterable')

var nums = new Raid
nums[0] = 100
nums[1] = 200
nums[2] = 300
nums.length = 3
var result = 0

nums.forEach(function (num) {
result += num
})
console.log(result)
// 600

0 comments on commit e646ebe

Please sign in to comment.