Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update setting-the-length-of-an-array.md #2535

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ Types of change:

### Fixed

## January 28th 2021

### Changed
- [Javascript - Setting The Length of An Array - Update information about empty array items](https://github.com/enkidevs/curriculum/pull/2535)

## January 27th 2021

### Added
Expand Down Expand Up @@ -137,7 +142,7 @@ Types of change:
## January 11th 2021

### Fixed
- [Go - Assignment Operators - Fix incorrect RQ answer](https://github.com/enkidevs/curriculum/pull/2536)
- [Go - Assignment Operators - Fix incorrect RQ answer](https://github.com/enkidevs/curriculum/pull/2536

## January 5th 2021

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ category: hack
tags:
- introduction
- obscura
links:
- >-
[Array Holes](https://medium.com/better-programming/what-are-holes-in-arrays-3ac5fcbcd1c){website}
practiceQuestion:
formats:
- fill-in-the-gap
Expand Down Expand Up @@ -44,11 +47,14 @@ a.length = 4;
// a = [
// "one",
// "two",
// undefined,
// undefined
// <2 empty items>
// ]
```

Extending an array without adding values would create empty item slots, also called *holes*. An array with *holes* is also referred to as a *sparse* array.

> 💡 Check out the *Learn More* section for more information about *sparse* arrays and *holes*.

Finally, to truncate to zero:

```javascript
Expand All @@ -74,9 +80,9 @@ arr.length = 4;
```

- length
- `[1,2,undefined,undefined]`
- `[1, 2, <1 empty item>, <1 empty item>]`
- size
- `[1,2,undefined,undefined,undefined]`
- `[1, 2, undefined, undefined]`


---
Expand All @@ -95,7 +101,7 @@ names.length = 0;
// names = ???
```

- `["John","Mary","Tom", undefined]`
- `["John", "Mary", "Tom", <1 empty item>]`
- `[ ]`
- `[undefined,undefined,undefined, undefined]`
- `["John","Mary","Tom"]`
Expand Down