New article, new snippets, new collection

This commit is contained in:
Angelos Chalaris
2023-05-14 12:21:52 +03:00
parent 26a6b8f874
commit b4e1bdd7f8
8 changed files with 204 additions and 4 deletions

View File

@ -0,0 +1,37 @@
---
title: Initialize array until
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: neon-desk-2
dateModified: 2023-06-22T05:00:00-04:00
---
Initializes and fills an array with values generated by a function, until a condition is met.
- Create an empty array, `arr`, an index variable `i` and an element `el`.
- Use a `do...while` loop to add elements to the array, using the `mapFn` function, until the `conditionFn` function returns `true` for the given index `i` and element `el`.
- The condition function, `conditionFn`, takes three arguments: the current index, the previous element and the array itself.
- The mapping function, `mapFn`, takes three arguments: the current index, the current element and the array itself.
```js
const initializeArrayUntil = (conditionFn, mapFn) => {
const arr = [];
let i = 0;
let el = undefined;
do {
el = mapFn(i, el, arr);
arr.push(el);
i++;
} while (!conditionFn(i, el, arr));
return arr;
};
```
```js
initializeArrayUntil(
(i, val) => val > 10,
(i, val, arr) => (i <= 1 ? 1 : val + arr[i - 2])
); // [1, 1, 2, 3, 5, 8, 13]
```

View File

@ -0,0 +1,37 @@
---
title: Initialize array while
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: neon-desk-1
dateModified: 2023-06-20T05:00:00-04:00
---
Initializes and fills an array with values generated by a function, while a condition is met.
- Create an empty array, `arr`, an index variable `i` and an element `el`.
- Use a `while` loop to add elements to the array, using the `mapFn` function, as long as the `conditionFn` function returns `true` for the given index `i` and element `el`.
- The condition function, `conditionFn`, takes three arguments: the current index, the previous element and the array itself.
- The mapping function, `mapFn`, takes three arguments: the current index, the current element and the array itself.
```js
const initializeArrayWhile = (conditionFn, mapFn) => {
const arr = [];
let i = 0;
let el = mapFn(i, undefined, arr);
while (conditionFn(i, el, arr)) {
arr.push(el);
i++;
el = mapFn(i, el, arr);
}
return arr;
};
```
```js
initializeArrayWhile(
(i, val) => val < 10,
(i, val, arr) => (i <= 1 ? 1 : val + arr[i - 2])
); // [1, 1, 2, 3, 5, 8]
```

View File

@ -3,18 +3,19 @@ title: Initialize array with values
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: flower-portrait-1
dateModified: 2020-10-20T23:02:01+03:00
---
Initializes and fills an array with the specified values.
- Use `Array.from()` to create an array of the desired length, `Array.prototype.fill()` to fill it with the desired values.
- Use the `Array()` constructor to create an array of the desired length.
- Use `Array.prototype.fill()` to fill it with the desired values.
- Omit the last argument, `val`, to use a default value of `0`.
```js
const initializeArrayWithValues = (n, val = 0) =>
Array.from({ length: n }).fill(val);
const initializeArrayWithValues = (n, val = 0) => Array(n).fill(val);
```
```js

View File

@ -0,0 +1,27 @@
---
title: Initialize mapped array
type: snippet
language: javascript
tags: [array]
author: chalarangelo
cover: yellow-shoes
dateModified: 2023-06-13T05:00:00-04:00
---
Initializes and fills an array with the specified values, using a mapping function.
- Use the `Array()` constructor to create an array of the desired length.
- Use `Array.prototype.fill()` to fill it with `null` values.
- Use `Array.prototype.map()` to fill it with the desired values, using the provided function, `mapFn`.
- Omit the second argument, `mapFn`, to map each element to its index.
```js
const initializeMappedArray = (n, mapFn = (_, i) => i) =>
Array(n).fill(null).map(mapFn);
```
```js
initializeMappedArray(5); // [0, 1, 2, 3, 4]
initializeMappedArray(5, i => `item ${i + 1}`);
// ['item 1', 'item 2', 'item 3', 'item 4', 'item 5']
```