feat(curriculum): add smallest common multiple lab (#62407)

This commit is contained in:
Hillary Nyakundi
2025-09-29 15:26:14 +03:00
committed by GitHub
parent a627116efd
commit a7c71569c0
5 changed files with 126 additions and 4 deletions

View File

@@ -3582,6 +3582,12 @@
"In this lab you will build a sum all primes calculator that takes a number and returns the sum of all prime numbers that are less than or equal to that number."
]
},
"lab-range-based-lcm-calculator": {
"title": "Implement a Range-Based LCM Calculator",
"intro": [
"In this lab, you will create a function that takes an array of two numbers and returns the least common multiple (LCM) of those two numbers and all the numbers between them."
]
},
"lab-deep-flattening-tool": {
"title": "Create a Deep Flattening Tool",
"intro": [
@@ -4723,10 +4729,7 @@
"In this lab, you will implement the Depth-First Search Algorithm."
]
},
"lab-n-queens-problem": {
"title": "",
"intro": [""]
},
"lab-n-queens-problem": { "title": "", "intro": [""] },
"review-graphs-and-trees": {
"title": "Graphs and Trees Review",
"intro": [

View File

@@ -0,0 +1,9 @@
---
title: Introduction to the Implement a Range-Based LCM Calculator
block: lab-range-based-lcm-calculator
superBlock: full-stack-developer
---
## Introduction to the Implement a Range-Based LCM Calculator
"In this lab, you will create a function that takes an array of two numbers and returns the least common multiple (LCM) of those two numbers and all the numbers between them.

View File

@@ -0,0 +1,94 @@
---
id: ae9defd7acaf69703ab432ea
title: Implement a Range-Based LCM Calculator
challengeType: 26
dashedName: implement-a-range-based-lcm-calculator
---
# --description--
In this lab, you will create a function that takes an array of two numbers and returns the least common multiple (LCM) of those two numbers and all the numbers between them.
**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab.
**User Stories**
1. You should have a `smallestCommons` function that accepts an array of two numbers as an argument.
1. The `smallestCommons` function should return the smallest common multiple that is evenly divisible by both numbers and all sequential numbers in the range between them.
1. The function should handle input where the two numbers are not in numerical order.
# --hints--
You should have a `smallestCommons` function.
```js
assert.isFunction(smallestCommons);
```
`smallestCommons([1, 5])` should return a number.
```js
assert.isNumber(smallestCommons([1, 5]));
```
`smallestCommons([1, 5])` should return `60`.
```js
assert.strictEqual(smallestCommons([1, 5]), 60);
```
`smallestCommons([5, 1])` should return `60`.
```js
assert.strictEqual(smallestCommons([5, 1]), 60);
```
`smallestCommons([2, 10])` should return `2520`.
```js
assert.strictEqual(smallestCommons([2, 10]), 2520);
```
`smallestCommons([1, 13])` should return `360360`.
```js
assert.strictEqual(smallestCommons([1, 13]), 360360);
```
`smallestCommons([23, 18])` should return `6056820`.
```js
assert.strictEqual(smallestCommons([23, 18]), 6056820);
```
# --seed--
## --seed-contents--
```js
```
# --solutions--
```js
function smallestCommons(arr) {
let [min, max] = arr.sort((a, b) => a - b);
function gcd(a, b) {
return b === 0 ? a : gcd(b, a % b);
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
let multiple = min;
for (let i = min + 1; i <= max; i++) {
multiple = lcm(multiple, i);
}
return multiple;
}
```

View File

@@ -0,0 +1,15 @@
{
"name": "Implement a Range-Based LCM Calculator",
"isUpcomingChange": false,
"dashedName": "lab-range-based-lcm-calculator",
"helpCategory": "JavaScript",
"blockLayout": "link",
"blockType": "lab",
"usesMultifileEditor": true,
"challengeOrder": [
{
"id": "ae9defd7acaf69703ab432ea",
"title": "Implement a Range-Based LCM Calculator"
}
]
}

View File

@@ -411,6 +411,7 @@
"lab-value-remover-function",
"lab-matching-object-filter",
"lab-sum-all-primes-calculator",
"lab-range-based-lcm-calculator",
"lab-deep-flattening-tool",
"lab-all-true-property-validator",
"review-javascript-higher-order-functions",