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 d72bbe0

Browse files
readMe updated
1 parent 8b03615 commit d72bbe0

File tree

109 files changed

+3839
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+3839
-1
lines changed

‎README.md‎

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,85 @@ Contributions are not expected as this is a personal learning repository.
2929

3030
## License
3131

32-
This repository is intended for personal use and the content within is not subject to a formal license. However, please be respectful of any code or ideas that may be derived from other sources.
32+
This repository is intended for personal use and the content within is not subject to a formal license. However, please be respectful of any code or ideas that may be derived from other sources.
33+
34+
35+
# JavaScript Array Notes
36+
37+
## Fundamentals
38+
39+
* **Definition:** Arrays are ordered collections of values. They can hold values of any data type (including other arrays).
40+
* **Creation:**
41+
* `let myArray = [];` (Empty array)
42+
* `let myArray = [1, 2, "three", { key: "value" }];` (Array with mixed data types)
43+
* `let myArray = new Array(1, 2, 3);` (Using the `Array` constructor)
44+
* **Indexing:** Elements are accessed using zero-based indexing (e.g., `myArray[0]` for the first element).
45+
* **Length:** The `length` property returns the number of elements in the array.
46+
47+
## Common Array Methods
48+
49+
### Modification
50+
51+
* **`push(element)`:** Adds an element to the end of the array. Returns the new length.
52+
* **`pop()`:** Removes the last element from the array. Returns the removed element.
53+
* **`unshift(element)`:** Adds an element to the beginning of the array. Returns the new length.
54+
* **`shift()`:** Removes the first element from the array. Returns the removed element.
55+
* **`splice(startIndex, deleteCount, ...items)`:**
56+
* Removes elements from `startIndex` with `deleteCount`.
57+
* Inserts `items` at `startIndex`.
58+
* Returns an array of the removed elements.
59+
* **`fill(value, startIndex, endIndex)`:** Fills a portion of the array with a static value.
60+
* **`reverse()`:** Reverses the order of the elements in the array. Modifies the original array.
61+
* **`sort(compareFunction)`:** Sorts the elements of the array. Modifies the original array.
62+
* If `compareFunction` is omitted, elements are sorted lexicographically (string comparison).
63+
* For numeric sorting: `array.sort((a, b) => a - b);` (ascending), `array.sort((a,b) => b-a);` (descending)
64+
65+
### Access and Transformation
66+
67+
* **`slice(startIndex, endIndex)`:** Returns a shallow copy of a portion of the array. Does not modify the original array.
68+
* **`concat(...arrays)`:** Returns a new array that is the concatenation of the original array and the given arrays. Does not modify the original arrays.
69+
* **`join(separator)`:** Returns a string by concatenating all of the elements in an array, separated by the specified separator.
70+
* **`indexOf(element, startIndex)`:** Returns the first index at which a given element can be found in the array, or -1 if it is not present.
71+
* **`lastIndexOf(element, startIndex)`:** Returns the last index at which a given element can be found in the array, or -1 if it is not present.
72+
* **`includes(element, startIndex)`:** Determines whether an array includes a certain element, returning `true` or `false`.
73+
* **`toString()`:** Returns a string representing the array and its elements.
74+
75+
### Iteration
76+
77+
* **`forEach(callback)`:** Executes a provided function once for each array element.
78+
* **`map(callback)`:** Creates a new array with the results of calling a provided function on every element in the calling array.
79+
* **`filter(callback)`:** Creates a new array with all elements that pass the test implemented by the provided function.
80+
* **`reduce(callback, initialValue)`:** Executes a reducer function (provided by you) on each element of the array, resulting in a single output value.
81+
* **`reduceRight(callback, initialValue)`:** Applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.
82+
* **`some(callback)`:** Tests whether at least one element in the array passes the test implemented by the provided function. Returns a Boolean value.
83+
* **`every(callback)`:** Tests whether all elements in the array pass the test implemented by the provided function. Returns a Boolean value.
84+
* **`find(callback)`:** Returns the value of the first element in the provided array that satisfies the provided testing function.
85+
* **`findIndex(callback)`:** Returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.
86+
* **`entries()`:** Returns a new Array Iterator object that contains the key/value pairs for each index in the array.
87+
88+
## Destructuring Arrays
89+
90+
* `let [first, second, ...rest] = myArray;` (Extracts elements into variables)
91+
92+
## Spread Operator
93+
94+
* `let newArray = [...myArray, 4, 5];` (Creates a new array with elements from `myArray` and additional elements)
95+
96+
## Array-like Objects
97+
98+
* `arguments` object in functions.
99+
* DOM NodeLists.
100+
* Can be converted to arrays using `Array.from()` or the spread operator.
101+
102+
## Multidimensional Arrays
103+
104+
* Arrays containing other arrays.
105+
* Access elements using multiple indices (e.g., `my2DArray[row][column]`).
106+
107+
## Things to remember for Interviews
108+
109+
* Array methods are crucial. Practice using `map`, `filter`, `reduce`, `forEach`, and `sort`.
110+
* Understand the difference between methods that modify the original array and those that return a new array.
111+
* Be comfortable with array destructuring and the spread operator.
112+
* Know how to handle array-like objects.
113+
* Understand the time complexity of common array operations.

‎array/array_notes.js‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Arrays are ordered collections of values. They can hold values of any data type (including other arrays).
3+
*/
4+
let arr = []; // empty array
5+
arr = [1, 2, "three", { key: "value" }];
6+
console.log("myArray:", arr);
7+
console.log("myArray[0]:", arr[0]);
8+
console.log("myArray.length:", arr.length);
9+
10+
// Modification
11+
console.log("\n--- Modification ---");
12+
arr.push(4);
13+
console.log("push(4):", arr);
14+
arr.pop();
15+
console.log("pop():", arr);
16+
arr.unshift(0);
17+
console.log("unshift(0):", arr);
18+
arr.shift();
19+
console.log("shift():", arr);
20+
arr.splice(1, 1, "two-changed");
21+
console.log("splice(1, 1, 'two-changed'):", arr);
22+
arr.reverse();
23+
console.log("reverse():", arr);
24+
arr.sort();
25+
console.log("sort():", arr); // sorts alphabetically, so the object will be at the start
26+
27+
let numberArray = [5, 2, 9, 1, 5, 6];
28+
numberArray.sort((a, b) => a - b);
29+
console.log("numberArray.sort((a, b) => a - b):", numberArray);
30+
numberArray.sort((a, b) => b - a);
31+
console.log("numberArray.sort((a,b) => b-a):", numberArray);
32+
33+
// Access and Transformation
34+
console.log("\n--- Access and Transformation ---");
35+
let slicedArray = arr.slice(1, 3);
36+
console.log("slice(1, 3):", slicedArray);
37+
console.log("original myArray:", arr); // slice doesn't modify original
38+
39+
let concatenatedArray = arr.concat([5, 6]);
40+
console.log("concat([5, 6]):", concatenatedArray);
41+
console.log("original myArray:", arr); // concat doesn't modify original
42+
43+
console.log("join(', '):", arr.join(", "));
44+
console.log("indexOf('two-changed'):", arr.indexOf("two-changed"));
45+
console.log("includes('two-changed'):", arr.includes("two-changed"));
46+
47+
// Iteration
48+
console.log("\n--- Iteration ---");
49+
arr.forEach((element, index) => {
50+
console.log("forEach:", index, element);
51+
});
52+
53+
let mappedArray = arr.map((element) => {
54+
if (typeof element === 'string') {
55+
return element.toUpperCase();
56+
}
57+
return element;
58+
});
59+
console.log("map():", mappedArray);
60+
61+
let filteredArray = numberArray.filter((element) => element > 3);
62+
console.log("filter():", filteredArray);
63+
64+
let reducedValue = numberArray.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
65+
console.log("reduce():", reducedValue);
66+
67+
let someResult = numberArray.some((element) => element > 8);
68+
console.log("some():", someResult);
69+
70+
let everyResult = numberArray.every((element) => element > 0);
71+
console.log("every():", everyResult);
72+
73+
let foundElement = numberArray.find((element) => element === 5);
74+
console.log("find():", foundElement);
75+
76+
let foundIndex = numberArray.findIndex((element) => element === 9);
77+
console.log("findIndex():", foundIndex);
78+
79+
// Destructuring Arrays
80+
console.log("\n--- Destructuring Arrays ---");
81+
let [first, second, ...rest] = arr;
82+
console.log("destructuring:", first, second, rest);
83+
84+
// Spread Operator
85+
console.log("\n--- Spread Operator ---");
86+
let newArray = [...arr, 4, 5];
87+
console.log("spread:", newArray);
88+
89+
// Multidimensional Arrays
90+
console.log("\n--- Multidimensional Arrays ---");
91+
let multiArray = [[1, 2], [3, 4], [5, 6]];
92+
console.log("multiArray:", multiArray);
93+
console.log("multiArray[1][0]:", multiArray[1][0]);
94+
95+
// Array.from
96+
console.log("\n--- Array.from ---");
97+
function arrayFromExample() {
98+
const args = Array.from(arguments);
99+
console.log(args)
100+
}
101+
102+
arrayFromExample(1, 2, 3, 4);

‎old/BEDataTest/1.2.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
remove("Hi!") === "Hi"
2+
remove("Hi!!!") === "Hi!!"
3+
remove("!Hi") === "!Hi"
4+
remove("!Hi!") === "!Hi"
5+
remove("Hi! Hi!") === "Hi! Hi"
6+
remove("Hi") === "Hi"
7+
8+
function remove(string) {
9+
return string.endsWith('!') ? string.substr(0, string.length - 1) : string;
10+
}

‎old/BEDataTest/1.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
remove("Hi!") === "Hi"
2+
remove("Hi!!!") === "Hi!!"
3+
remove("!Hi") === "!Hi"
4+
remove("!Hi!") === "!Hi"
5+
remove("Hi! Hi!") === "Hi! Hi"
6+
remove("Hi") === "Hi"
7+
8+
function remove(str) {
9+
10+
// Assumed that str would be string anyway
11+
12+
last_ind_of = str.lastIndexOf('!')
13+
len_minus_one = str.length - 1
14+
//
15+
//the case where '!' doesn't exist or not at the end
16+
if (last_ind_of == -1 || last_ind_of < len_minus_one) {
17+
return str
18+
} else { // this else only covers when str is ended with '!'
19+
//console.log(str + " () " + str.substr(0, len_minus_one))
20+
return str.substr(0, len_minus_one);
21+
}
22+
23+
}

‎old/BEDataTest/2.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function vowelIndices(word) {
2+
let arr = [];
3+
for (i = 0; i < word.length; i++) {
4+
if (['a', 'e', 'i', 'o', 'u'].indexOf(word.charAt(i).toLowerCase()) !== -1) {
5+
arr.push(i + 1) // as indexed from [1..n]
6+
}
7+
}
8+
return arr
9+
}
10+
///*
11+
console.log(vowelIndices("aeioU"))
12+
console.log(vowelIndices("Mmmm"))
13+
console.log(vowelIndices("Apple"))
14+
console.log(vowelIndices("Super"))
15+
console.log(vowelIndices("Orange"))
16+
console.log(vowelIndices("supercalifragilisticexpialidocious"))
17+
//*/

‎old/BEDataTest/3.2.js‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
var bananas = function(s) {
2+
console.log(s)
3+
recursive(s, "banana", "", []);
4+
}
5+
6+
function recursive(s, ban, rslt_str, rslt_arr) {
7+
8+
if (ban.length == 2) {
9+
console.log(rslt_str)
10+
return;
11+
}
12+
/*
13+
if (rslt_str.length == s_len && ban.length == 0) {
14+
rslt_arr.push(rslt_str);
15+
}
16+
*/
17+
for (i = 0; i < ban.length; i++) {
18+
for (j = 0; j < s.length; j++) {
19+
if (ban[i] == s[j]) {
20+
rslt_str += ban[i];
21+
console.log("if: " + s.substr(j + 1, s.length), ban.substr(i + 1, ban.length), rslt_str);
22+
recursive(s.substr(j + 1, s.length), ban.substr(i + 1, ban.length), rslt_str, rslt_arr)
23+
} else {
24+
rslt_str += "-";
25+
console.log("else: " + s.substr(j + 1, s.length), ban.substr(i + 1, ban.length), rslt_str);
26+
recursive(s.substr(j + 1, s.length), ban.substr(i + 1, ban.length), rslt_str, rslt_arr)
27+
}
28+
}
29+
}
30+
}
31+
32+
bananas("bbananana");

‎old/BEDataTest/3.js‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var bananas = function(s) {
2+
// Your code here!
3+
let possible_comb = []
4+
let len_ban = "banana".length; // could be set manually to 6; as it is static
5+
let len_s = s.length; // later to be used many times, so stored in a vrble
6+
7+
//
8+
for (i = 0; i < "banana".length; i++) {
9+
possible_comb.push(new Array(len_s).fill(0));
10+
count = 1;
11+
for (j = i; j <= len_s - (len_ban - i); j++) {
12+
if ("banana".charAt(i) == s.charAt(j)) {
13+
"banana".charAt(i) == s.charAt(j)
14+
possible_comb[i][j] = count++
15+
}
16+
}
17+
}
18+
let result_arr = []
19+
let comb_ind = len_ban - 1; // node 3.js
20+
console.log(comb_ind)
21+
//
22+
while (comb_ind >= 0) {
23+
let result_str;
24+
25+
for (i = 0; i < len_ban; i++) {
26+
result_str = "";
27+
if (comb_ind == i) {
28+
console.log(i + " :if: " + j + " : " + result_str)
29+
for (j = i; j <= len_s - (len_ban - i); j++) {
30+
if (possible_comb[i][j] > 0) {
31+
result_str += "banana".charAt(i)
32+
possible_comb[i][j] = 0;
33+
console.log(i + " :if: " + j + " : " + result_str)
34+
break;
35+
}
36+
}
37+
} else if (possible_comb[i][j] > 0) {
38+
result_str += "banana".charAt(i)
39+
console.log(i + " :eif: " + j + " : " + result_str)
40+
} else {
41+
result_str += "-"
42+
console.log(i + " :else: " + j + " : " + result_str)
43+
}
44+
45+
}
46+
comb_ind--;
47+
console.log(comb_ind)
48+
result_arr.push(result_str)
49+
}
50+
51+
52+
console.log(result_arr)
53+
54+
return possible_comb
55+
}
56+
57+
bananas("bbananana");

0 commit comments

Comments
(0)

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