diff --git a/client/i18n/locales/english/intro.json b/client/i18n/locales/english/intro.json index dde93314c2f..81b3e6e0d59 100644 --- a/client/i18n/locales/english/intro.json +++ b/client/i18n/locales/english/intro.json @@ -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": [ diff --git a/client/src/pages/learn/full-stack-developer/lab-sorted-index-finder/index.md b/client/src/pages/learn/full-stack-developer/lab-sorted-index-finder/index.md new file mode 100644 index 00000000000..b091b6bc769 --- /dev/null +++ b/client/src/pages/learn/full-stack-developer/lab-sorted-index-finder/index.md @@ -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. diff --git a/curriculum/challenges/_meta/lab-sorted-index-finder/meta.json b/curriculum/challenges/_meta/lab-sorted-index-finder/meta.json new file mode 100644 index 00000000000..626065b71ab --- /dev/null +++ b/curriculum/challenges/_meta/lab-sorted-index-finder/meta.json @@ -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" +} \ No newline at end of file diff --git a/curriculum/challenges/english/25-front-end-development/lab-sorted-index-finder/68a47dcdbce12193caf77012.md b/curriculum/challenges/english/25-front-end-development/lab-sorted-index-finder/68a47dcdbce12193caf77012.md new file mode 100644 index 00000000000..c5a28bd4bd7 --- /dev/null +++ b/curriculum/challenges/english/25-front-end-development/lab-sorted-index-finder/68a47dcdbce12193caf77012.md @@ -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); +} +``` + diff --git a/curriculum/superblock-structure/full-stack.json b/curriculum/superblock-structure/full-stack.json index 72aed1595cf..07fc8909efb 100644 --- a/curriculum/superblock-structure/full-stack.json +++ b/curriculum/superblock-structure/full-stack.json @@ -823,6 +823,9 @@ { "dashedName": "lab-book-organizer" }, + { + "dashedName": "lab-sorted-index-finder" + }, { "dashedName": "review-javascript-higher-order-functions" },