mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-14 04:00:42 -04:00
feat: add sum all prime challenge lab (#62084)
This commit is contained in:
@@ -3522,6 +3522,12 @@
|
||||
"In this lab, you will practice using higher order functions to find the symmetric difference between two arrays."
|
||||
]
|
||||
},
|
||||
"lab-sum-all-primes-calculator": {
|
||||
"title": "Build a Prime Number Sum Calculator",
|
||||
"intro": [
|
||||
"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."
|
||||
]
|
||||
},
|
||||
"review-javascript-higher-order-functions": {
|
||||
"title": "JavaScript Higher Order Functions Review",
|
||||
"intro": [
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build a Sum All Primes Calculator
|
||||
block: lab-sum-all-primes-calculator
|
||||
superBlock: full-stack-developer
|
||||
---
|
||||
|
||||
## Introduction to the Build a Sum All Primes Calculator
|
||||
|
||||
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.
|
||||
@@ -0,0 +1,89 @@
|
||||
---
|
||||
id: a3bfc1673c0526e06d3ac698
|
||||
title: Build a Sum All Primes Calculator
|
||||
challengeType: 26
|
||||
dashedName: build-a-sum-all-primes-calculator
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this lab, you will build a calculator that takes a number and returns the sum of all prime numbers that are less than or equal to that number.
|
||||
|
||||
**Objective**: Fulfill the user stories below and get all the tests to pass to complete the lab.
|
||||
|
||||
**User Stories:**
|
||||
|
||||
1. You should have a `sumPrimes` function that accepts a number as an argument.
|
||||
1. The `sumPrimes` function should return the sum of all prime numbers less than or equal to the provided number.
|
||||
1. If the input number is less than `2`, the function should return `0`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `sumPrimes` function.
|
||||
|
||||
```js
|
||||
assert.isFunction(sumPrimes);
|
||||
```
|
||||
|
||||
`sumPrimes(10)` should return `17`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumPrimes(10), 17);
|
||||
```
|
||||
|
||||
`sumPrimes(5)` should return `10`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumPrimes(5), 10);
|
||||
```
|
||||
|
||||
`sumPrimes(2)` should return `2`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumPrimes(2), 2);
|
||||
```
|
||||
|
||||
`sumPrimes(0)` should return `0`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumPrimes(0), 0);
|
||||
```
|
||||
|
||||
`sumPrimes(977)` should return `73156`.
|
||||
|
||||
```js
|
||||
assert.strictEqual(sumPrimes(977), 73156);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sumPrimes(num) {
|
||||
function isPrime(n) {
|
||||
if (n < 2) return false;
|
||||
for (let i = 2; i <= Math.sqrt(n); i++) {
|
||||
if (n % i === 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
|
||||
for (let i = 2; i <= num; i++) {
|
||||
if (isPrime(i)) {
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"name": "Build a Sum All Primes Calculator",
|
||||
"isUpcomingChange": false,
|
||||
"dashedName": "lab-sum-all-primes-calculator",
|
||||
"helpCategory": "JavaScript",
|
||||
"blockLayout": "link",
|
||||
"blockType": "lab",
|
||||
"usesMultifileEditor": true,
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "a3bfc1673c0526e06d3ac698",
|
||||
"title": "Build a Sum All Primes Calculator"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -401,6 +401,7 @@
|
||||
"lab-book-organizer",
|
||||
"lab-sorted-index-finder",
|
||||
"lab-symmetric-difference",
|
||||
"lab-sum-all-primes-calculator",
|
||||
"review-javascript-higher-order-functions",
|
||||
"quiz-javascript-higher-order-functions"
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user