feat(curriculum): add lab for implementing a sorted index finder (#61877)

This commit is contained in:
Sem Bauke
2025-08-25 14:48:27 +02:00
committed by GitHub
parent 3a9a4c9340
commit df9b21146f
5 changed files with 142 additions and 0 deletions

View File

@@ -3452,6 +3452,12 @@
"In this lab, you'll build a book organizer using higher order functions in JavaScript."
]
},
"lab-sorted-index-finder": {
"title": "Implement a Sorted Index Finder",
"intro": [
"In this lab, you will create a function that finds the index at which a given number should be inserted into a sorted array to maintain the array's sorted order."
]
},
"review-javascript-higher-order-functions": {
"title": "JavaScript Higher Order Functions Review",
"intro": [

View File

@@ -0,0 +1,9 @@
---
title: Introduction to Implement a Sorted Index Finder
block: lab-sorted-index-finder
superBlock: full-stack-developer
---
## Introduction to Implement a Sorted Index Finder
In this lab, you will create a function that finds the index at which a given number should be inserted into a sorted array to maintain the array's sorted order.

View File

@@ -0,0 +1,16 @@
{
"name": "Implement a Sorted Index Finder",
"isUpcomingChange": false,
"dashedName": "lab-sorted-index-finder",
"superBlock": "full-stack-developer",
"helpCategory": "JavaScript",
"challengeOrder": [
{
"id": "68a47dcdbce12193caf77012",
"title": "Implement a Sorted Index Finder"
}
],
"usesMultifileEditor": true,
"blockType": "lab",
"blockLayout": "link"
}

View File

@@ -0,0 +1,108 @@
---
id: 68a47dcdbce12193caf77012
title: Implement a Sorted Index Finder
challengeType: 26
dashedName: lab-sorted-index-finder
---
# --description--
In this lab you will create a function that returns the lowest index at which a value should be inserted into an array once it has been sorted in ascending order.
**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories:**
1. You should have a `getIndexToIns` function that takes two arguments: an array and a number.
2. You should use the `sort` method to sort the array in ascending order.
3. Your `getIndexToIns` function should return the lowest index at which the number should be inserted by using the `findIndex` method.
4. Your `getIndexToIns` function should always return a number.
**Hint:**
The `findIndex` method is a built-in array method in JavaScript. It takes a callback function and returns the index of the first element that satisfies the condition. Both `findIndex` and `sort` are higher-order functions.
**Examples:**
- `getIndexToIns([1, 2, 3, 4], 1.5)` should return `1` because `1.5` is greater than `1` (index `0`) and less than `2` (index `1`).
- `getIndexToIns([20, 3, 5], 19)` should return `2` because after sorting to `[3, 5, 20]`, `19` is less than `20` (index `2`) and greater than `5` (index `1`).
# --hints--
You should have a `getIndexToIns` function.
```js
assert.isFunction(getIndexToIns);
```
`getIndexToIns` should always return a number.
```js
assert.isNumber(getIndexToIns([10, 20, 30, 40, 50], 35));
```
Your function should use the `sort` method.
```js
assert.match(getIndexToIns.toString(), /\.sort\(/);
```
Your function should make use of the `findIndex` method.
```js
assert.match(getIndexToIns.toString(), /\.findIndex\(/);
```
`getIndexToIns([10, 20, 30, 40, 50], 35)` should return `3`.
```js
assert.strictEqual(getIndexToIns([10, 20, 30, 40, 50], 35), 3);
```
`getIndexToIns([10, 20, 30, 40, 50], 30)` should return `2`.
```js
assert.strictEqual(getIndexToIns([10, 20, 30, 40, 50], 30), 2);
```
`getIndexToIns([40, 60], 50)` should return `1`.
```js
assert.strictEqual(getIndexToIns([40, 60], 50), 1);
```
`getIndexToIns([3, 10, 5], 3)` should return `0`.
```js
assert.strictEqual(getIndexToIns([3, 10, 5], 3), 0);
```
`getIndexToIns([5, 3, 20, 3], 5)` should return `2`.
```js
assert.strictEqual(getIndexToIns([5, 3, 20, 3], 5), 2);
```
`getIndexToIns([2, 20, 10], 19)` should return `2`.
```js
assert.strictEqual(getIndexToIns([2, 20, 10], 19), 2);
```
# --seed--
## --seed-contents--
```js
```
# --solutions--
```js
function getIndexToIns(arr, num) {
const sorted = [...arr].sort((a, b) => a - b);
return sorted.findIndex(el => el >= num);
}
```

View File

@@ -823,6 +823,9 @@
{
"dashedName": "lab-book-organizer"
},
{
"dashedName": "lab-sorted-index-finder"
},
{
"dashedName": "review-javascript-higher-order-functions"
},