forked from b00tc4mp/isdi-bootcamp-202409
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add forEach test and implementation b00tc4mp#160
- Loading branch information
1 parent
5cf19fc
commit e646ebe
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
staff/manuel-barzi/playground/arrays/Array.prototype.forEach.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
45
staff/manuel-barzi/playground/raids/Raid.prototype.forEach.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |