Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2b94d37

Browse files
docs(array): fix examples
1 parent 31dbc32 commit 2b94d37

File tree

2 files changed

+31
-45
lines changed

2 files changed

+31
-45
lines changed

‎book/chapters/array.adoc

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,20 @@ Searching by index takes a constant time, *O(1)*, to retrieve values out of the
126126
.Search by index
127127
[source, javascript]
128128
----
129-
include::{codedir}/data-structures/arrays/array.js[tag=searchByIndex]
129+
/**
130+
* Search for array's element by index
131+
*
132+
* @example Given array = [2, 5, 1, 9, 6, 7, -1];
133+
* searchByIndex(array, 3); //↪️ 9
134+
* searchByIndex(array, 6); //↪️ -1
135+
* searchByIndex(array, 13); //↪️ undefined
136+
* @param {array} array
137+
* @param {number} index
138+
* @returns {any} value or undefined if not found
139+
*/
140+
function searchByIndex(array, index) {
141+
return array[index];
142+
}
130143
----
131144

132145
Finding out if a value is in the array or not is a different story.
@@ -136,7 +149,22 @@ Finding out if a value is in the array or not is a different story.
136149
.Search by value
137150
[source, javascript]
138151
----
139-
include::{codedir}/data-structures/arrays/array.js[tag=searchByValue]
152+
/**
153+
* Search for array's element by value
154+
*
155+
* @example Given array = [2, 5, 1, 9, 6, 7];
156+
* searchByValue(array, 9); //↪️ 3
157+
* searchByValue(array, 13); //↪️ -1
158+
* @param {array} array
159+
* @param {any} value
160+
*/
161+
function searchByValue(array, value) {
162+
for (let index = 0; index < array.length; index++) {
163+
const element = array[index];
164+
if (element === value) return index;
165+
}
166+
return -1;
167+
}
140168
----
141169

142170
We would have to loop through the whole array (worst case) or until we find it: *O(n)*.

‎src/data-structures/arrays/array.js

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable */
2-
const assert = require('assert');
32

3+
// see: book/chapters/array.adoc
44

55
const array = [2, 5, 1, 9, 6, 7]; // JavaScript
66
array[4]; //=> 6
@@ -31,45 +31,3 @@ array.splice(2, 1); // delete 1 element at position 2
3131

3232
// Deleting last element from the array
3333
array.pop(); // => array: [2, 5, 1, 9, 6]
34-
35-
36-
// tag::searchByIndex[]
37-
/**
38-
* Search for array's element by index
39-
*
40-
* @example Given array = [2, 5, 1, 9, 6, 7];
41-
* searchByIndex(array, 3); //↪️ 9
42-
* searchByIndex(array, 13); //↪️ -1
43-
* @param {array} array
44-
* @param {number} index
45-
* @returns {any} value or -1 if not found
46-
*/
47-
function searchByIndex(array, index) {
48-
return array[index] || -1;
49-
}
50-
// end::searchByIndex[]
51-
52-
assert.equal(searchByIndex(array, 3), 9);
53-
assert.equal(searchByIndex(array, 13), -1);
54-
55-
// tag::searchByValue[]
56-
/**
57-
* Search for array's element by value
58-
*
59-
* @example Given array = [2, 5, 1, 9, 6, 7];
60-
* searchByValue(array, 9); //↪️ 3
61-
* searchByValue(array, 13); //↪️ -1
62-
* @param {array} array
63-
* @param {any} value
64-
*/
65-
function searchByValue(array, value) {
66-
for (let index = 0; index < array.length; index++) {
67-
const element = array[index];
68-
if (element === value) return index;
69-
}
70-
return -1;
71-
}
72-
// end::searchByValue[]
73-
74-
assert.equal(searchByValue(array, 9), 3);
75-
assert.equal(searchByValue(array, 13), -1);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /