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 indexOf test and implementation; add test case for push with n ar…
…guments; b00tc4mp#160
- Loading branch information
1 parent
19854ba
commit dfa29f8
Showing
7 changed files
with
189 additions
and
4 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
staff/manuel-barzi/playground/arrays/Array.prototype.indexOf.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,17 @@ | ||
console.log('TEST Array.prototype.indexOf') | ||
|
||
console.log('CASE get index of c') | ||
|
||
var chars = ['a', 'b', 'c', 'b', 'a'] | ||
|
||
var index = chars.indexOf('c') | ||
console.log(index) | ||
// 2 | ||
|
||
console.log('CASE get index of c from index -2') | ||
|
||
var chars = ['a', 'b', 'c', 'b', 'a'] | ||
|
||
var index = chars.indexOf('c', -2) | ||
console.log(index) | ||
// -1 |
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
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 @@ | ||
var indexOf = function (iterable, searchElement) { | ||
/* | ||
iterate on iterable | ||
if searchElement found then return index | ||
otherwise return -1 | ||
*/ | ||
|
||
for (var i = 0; i < iterable.length; i++) { | ||
var element = iterable[i] | ||
|
||
if (element === searchElement) return i | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
console.log('TEST indexOf') | ||
|
||
console.log('CASE get index of c') | ||
|
||
var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 } | ||
|
||
var index = indexOf(chars, 'c') | ||
console.log(index) | ||
// 2 |
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,43 @@ | ||
var indexOf = function (iterable, searchElement, fromIndex) { | ||
/* | ||
iterate on iterable | ||
if searchElement found then return index | ||
otherwise return -1 | ||
*/ | ||
|
||
/* | ||
iterate on iterable from index fromIndex | ||
if searchElement found then return index | ||
otherwise return -1 | ||
*/ | ||
|
||
for ( | ||
var i = (fromIndex === undefined ? 0 : (fromIndex >= 0 ? fromIndex : fromIndex + iterable.length)); | ||
i < iterable.length; | ||
i++ | ||
) { | ||
var element = iterable[i] | ||
|
||
if (element === searchElement) return i | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
console.log('TEST indexOf') | ||
|
||
console.log('CASE get index of c') | ||
|
||
var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 } | ||
|
||
var index = indexOf(chars, 'c') | ||
console.log(index) | ||
// 2 | ||
|
||
console.log('CASE get index of c from index -2') | ||
|
||
var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 } | ||
|
||
var index = indexOf(chars, 'c', -2) | ||
console.log(index) | ||
// -1 |
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,43 @@ | ||
var indexOf = function (iterable, searchElement, fromIndex) { | ||
/* | ||
iterate on iterable | ||
if searchElement found then return index | ||
otherwise return -1 | ||
*/ | ||
|
||
/* | ||
iterate on iterable from index fromIndex | ||
if searchElement found then return index | ||
otherwise return -1 | ||
*/ | ||
|
||
for ( | ||
var i = (arguments.length === 2 ? 0 : (fromIndex >= 0 ? fromIndex : fromIndex + iterable.length)); | ||
i < iterable.length; | ||
i++ | ||
) { | ||
var element = iterable[i] | ||
|
||
if (element === searchElement) return i | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
console.log('TEST indexOf') | ||
|
||
console.log('CASE get index of c') | ||
|
||
var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 } | ||
|
||
var index = indexOf(chars, 'c') | ||
console.log(index) | ||
// 2 | ||
|
||
console.log('CASE get index of c from index -2') | ||
|
||
var chars = { 0: 'a', 1: 'b', 2: 'c', 3: 'b', 4: 'a', length: 5 } | ||
|
||
var index = indexOf(chars, 'c', -2) | ||
console.log(index) | ||
// -1 |
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,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 |
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