mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
feat(curriculum): add functions calculator workshop (#55949)
Co-authored-by: Sem Bauke <sem@freecodecamp.org>
This commit is contained in:
@@ -1929,7 +1929,12 @@
|
||||
"wjzt": { "title": "149", "intro": [] },
|
||||
"tsdq": { "title": "150", "intro": [] },
|
||||
"kkix": { "title": "151", "intro": [] },
|
||||
"icxa": { "title": "152", "intro": [] },
|
||||
"workshop-calculator": {
|
||||
"title": "Build a Calculator",
|
||||
"intro": [
|
||||
"In this workshop, you will review working with functions by building a calculator."
|
||||
]
|
||||
},
|
||||
"hqba": { "title": "153", "intro": [] },
|
||||
"nnqa": { "title": "154", "intro": [] },
|
||||
"cktz": { "title": "155", "intro": [] },
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
title: Introduction to the Build a Calculator
|
||||
block: workshop-calculator
|
||||
superBlock: front-end-development
|
||||
---
|
||||
|
||||
## Introduction to the Build a Calculator
|
||||
|
||||
This is a test for the new project-based curriculum.
|
||||
@@ -0,0 +1,89 @@
|
||||
{
|
||||
"name": "Build a Calculator",
|
||||
"blockType": "workshop",
|
||||
"isUpcomingChange": true,
|
||||
"usesMultifileEditor": true,
|
||||
"hasEditableBoundaries": true,
|
||||
"dashedName": "workshop-calculator",
|
||||
"order": 152,
|
||||
"superBlock": "front-end-development",
|
||||
"challengeOrder": [
|
||||
{
|
||||
"id": "66cc06012e46aa6bc9b8c001",
|
||||
"title": "Step 1"
|
||||
},
|
||||
{
|
||||
"id": "66cc095dfe1682753d2ab030",
|
||||
"title": "Step 2"
|
||||
},
|
||||
{
|
||||
"id": "66cc0a9e06e00b75d6782be9",
|
||||
"title": "Step 3"
|
||||
},
|
||||
{
|
||||
"id": "66cc0ba5881acb7692cfc4de",
|
||||
"title": "Step 4"
|
||||
},
|
||||
{
|
||||
"id": "66cc0de2920bec775f610424",
|
||||
"title": "Step 5"
|
||||
},
|
||||
{
|
||||
"id": "66cc0f1ae40802781b2ea972",
|
||||
"title": "Step 6"
|
||||
},
|
||||
{
|
||||
"id": "66cc10958d72e57907f898b2",
|
||||
"title": "Step 7"
|
||||
},
|
||||
{
|
||||
"id": "66cc12fa504b0479dac479a0",
|
||||
"title": "Step 8"
|
||||
},
|
||||
{
|
||||
"id": "66cdf76685e4cb5a8726e27b",
|
||||
"title": "Step 9"
|
||||
},
|
||||
{
|
||||
"id": "66cc14ca14e65e7a9ebecb80",
|
||||
"title": "Step 10"
|
||||
},
|
||||
{
|
||||
"id": "66cc16abc30b367ba1d1f9ef",
|
||||
"title": "Step 11"
|
||||
},
|
||||
{
|
||||
"id": "66cc172af7159a7c67804544",
|
||||
"title": "Step 12"
|
||||
},
|
||||
{
|
||||
"id": "66cc1a3a39aef47d6473cb2f",
|
||||
"title": "Step 13"
|
||||
},
|
||||
{
|
||||
"id": "66cc1ccfefdd727e18c2ab20",
|
||||
"title": "Step 14"
|
||||
},
|
||||
{
|
||||
"id": "66cc1deb1f04647f2aabee2b",
|
||||
"title": "Step 15"
|
||||
},
|
||||
{
|
||||
"id": "66cc21d23238dc8240a8a182",
|
||||
"title": "Step 16"
|
||||
},
|
||||
{
|
||||
"id": "66cc26907ff6908402af0149",
|
||||
"title": "Step 17"
|
||||
},
|
||||
{
|
||||
"id": "66cc2739f687e484d50bb6f1",
|
||||
"title": "Step 18"
|
||||
},
|
||||
{
|
||||
"id": "66cc281d687975858049fd8d",
|
||||
"title": "Step 19"
|
||||
}
|
||||
],
|
||||
"helpCategory": "JavaScript"
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
---
|
||||
id: 66cc06012e46aa6bc9b8c001
|
||||
title: Step 1
|
||||
challengeType: 1
|
||||
dashedName: step-1
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In this workshop, you will review how to work with functions by building a calculator app.
|
||||
|
||||
In the previous lecture videos, you learned how to declare functions like this:
|
||||
|
||||
```js
|
||||
// regular function
|
||||
function myFunction(){
|
||||
|
||||
}
|
||||
|
||||
// arrow function
|
||||
const myArrowFunction = () => {
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
Start by creating a function called `addTwoAndSeven`.
|
||||
|
||||
You can choose to use the regular function syntax or the arrow function syntax.
|
||||
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a function called `addTwoAndSeven`.
|
||||
|
||||
```js
|
||||
assert.isFunction(addTwoAndSeven);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
---
|
||||
id: 66cc095dfe1682753d2ab030
|
||||
title: Step 2
|
||||
challengeType: 1
|
||||
dashedName: step-2
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
One of concepts you learned in the previous lecture videos was how to return values from a function.
|
||||
|
||||
Here is a reminder of how to return a value from a function:
|
||||
|
||||
```js
|
||||
function myFunction(){
|
||||
return 'Hello World';
|
||||
}
|
||||
```
|
||||
|
||||
Inside your `addTwoAndSeven` function, return the sum of `2` and `7`.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your `addTwoAndSeven` function should return the sum of `2` and `7`
|
||||
|
||||
```js
|
||||
assert.strictEqual(addTwoAndSeven(), 9);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
--fcc-editable-region--
|
||||
function addTwoAndSeven() {
|
||||
|
||||
}
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: 66cc0a9e06e00b75d6782be9
|
||||
title: Step 3
|
||||
challengeType: 1
|
||||
dashedName: step-3
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the previous lecture videos, you learned how to call(invoke) a function. Calling a function means to execute the code inside the function.
|
||||
|
||||
Here is a reminder of how to call a function:
|
||||
|
||||
```js
|
||||
function myFunction(){
|
||||
return 'Hello World';
|
||||
}
|
||||
|
||||
// function call
|
||||
myFunction();
|
||||
```
|
||||
|
||||
Add a `console.log` statement, and inside it, call the `addTwoAndSeven` function.
|
||||
|
||||
You should now see the sum of `2` and `7` logged to the console.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` in your code.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log(.*)/);
|
||||
```
|
||||
|
||||
You should call the `addTwoAndSeven` function inside the `console.log`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\(\s*addTwoAndSeven\(\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function addTwoAndSeven() {
|
||||
return 2 + 7;
|
||||
}
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
---
|
||||
id: 66cc0ba5881acb7692cfc4de
|
||||
title: Step 4
|
||||
challengeType: 1
|
||||
dashedName: step-4
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now, it is time to add another function.
|
||||
|
||||
Declare a function called `addThreeAndFour` that returns the sum of `3` and `4`.
|
||||
|
||||
Then call the `addThreeAndFour` function inside a `console.log` to see the result.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a function called `addThreeAndFour`.
|
||||
|
||||
```js
|
||||
assert.isFunction(addThreeAndFour);
|
||||
```
|
||||
|
||||
Your `addThreeAndFour` function should return the sum of `3` and `4`.
|
||||
|
||||
```js
|
||||
assert.equal(addThreeAndFour(), 7);
|
||||
```
|
||||
|
||||
You should call the `addThreeAndFour` function inside a `console.log`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\(\s*addThreeAndFour\(\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function addTwoAndSeven() {
|
||||
return 2 + 7;
|
||||
}
|
||||
|
||||
console.log(addTwoAndSeven());
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
---
|
||||
id: 66cc0de2920bec775f610424
|
||||
title: Step 5
|
||||
challengeType: 1
|
||||
dashedName: step-5
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Even though the functions work as expected, there is a lot of repetition in your code.
|
||||
|
||||
Since you are building a calculator, you don't want to have to create a function for every possible combination of numbers you want to add together.
|
||||
|
||||
It would be better to create a single reusable function that can add any two numbers together.
|
||||
|
||||
Remove all of the code you have written so far.
|
||||
|
||||
In the next step, you will review how to work with parameters and arguments in functions.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should not have a function called `addTwoAndSeven`.
|
||||
|
||||
```js
|
||||
assert.notMatch(code, /function\s+addTwoAndSeven\s*\(\s*\)\s*{/);
|
||||
```
|
||||
|
||||
You should not have a function call to `addTwoAndSeven`.
|
||||
|
||||
```js
|
||||
assert.notMatch(code, /console\.log\(\s*addTwoAndSeven\(\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
You should not have a function called `addThreeAndFour`.
|
||||
|
||||
```js
|
||||
assert.notMatch(code, /function\s+addThreeAndFour\s*\(\s*\)\s*{/);
|
||||
```
|
||||
|
||||
You should not have a function call to `addThreeAndFour`.
|
||||
|
||||
```js
|
||||
assert.notMatch(code, /console\.log\(\s*addThreeAndFour\(\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
--fcc-editable-region--
|
||||
function addTwoAndSeven() {
|
||||
return 2 + 7;
|
||||
}
|
||||
|
||||
console.log(addTwoAndSeven());
|
||||
|
||||
function addThreeAndFour() {
|
||||
return 3 + 4;
|
||||
}
|
||||
|
||||
console.log(addThreeAndFour());
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
---
|
||||
id: 66cc0f1ae40802781b2ea972
|
||||
title: Step 6
|
||||
challengeType: 1
|
||||
dashedName: step-6
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the previous lecture videos, you learned how to work with function parameters.
|
||||
|
||||
A function parameter is a variable that is defined in the function's declaration and acts as a placeholder.
|
||||
|
||||
Here is an example of a function that has a parameter:
|
||||
|
||||
```js
|
||||
// The parameter is 'name'
|
||||
function greetUser(name) {
|
||||
return `Hello, ${name}!`;
|
||||
}
|
||||
```
|
||||
|
||||
Declare a function called `calculateSum` that takes two parameters, `num1` and `num2`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should declare a function called `calculateSum`.
|
||||
|
||||
```js
|
||||
assert.isFunction(calculateSum);
|
||||
```
|
||||
|
||||
Your `calculateSum` function should take two parameters, `num1` and `num2`.
|
||||
|
||||
```js
|
||||
assert.match(code, /calculateSum\s*(?:=\s*)?\(\s*num1\s*,\s*num2\s*\)\s*/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
---
|
||||
id: 66cc10958d72e57907f898b2
|
||||
title: Step 7
|
||||
challengeType: 1
|
||||
dashedName: step-7
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Inside your `calculateSum` function, you will need to return the sum of the two parameters.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your `calculateSum` function should return the sum of the two parameters.
|
||||
|
||||
```js
|
||||
assert.equal(calculateSum(1, 2), 3);
|
||||
assert.equal(calculateSum(5, 5), 10);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
--fcc-editable-region--
|
||||
function calculateSum(num1, num2) {
|
||||
|
||||
}
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
---
|
||||
id: 66cc12fa504b0479dac479a0
|
||||
title: Step 8
|
||||
challengeType: 1
|
||||
dashedName: step-8
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The advantage of functions is that they can be called with different arguments, allowing you to reuse the same code with various values.
|
||||
|
||||
Here are some example function calls with different string arguments:
|
||||
|
||||
```js
|
||||
// function definition
|
||||
function greetUser(name) {
|
||||
console.log(`Hello ${name}!`);
|
||||
}
|
||||
|
||||
// function calls
|
||||
greetUser('John'); // 'Hello John!'
|
||||
greetUser('Jane'); // 'Hello Jane!'
|
||||
```
|
||||
|
||||
Add a `console.log` that calls the `calculateSum` function with the arguments `2` and `5`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` that calls the `calculateSum` function with the arguments `2` and `5`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\(calculateSum\(\s*2\s*,\s*5\)\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
id: 66cc14ca14e65e7a9ebecb80
|
||||
title: Step 10
|
||||
challengeType: 1
|
||||
dashedName: step-10
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now that your calculator can add two numbers together, it is time to create a function that will subtract two numbers.
|
||||
|
||||
Declare a function called `calculateDifference` that takes two parameters, `num1` and `num2`.
|
||||
|
||||
Inside the function, return the difference between the two parameters.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function called `calculateDifference`.
|
||||
|
||||
```js
|
||||
assert.isFunction(calculateDifference);
|
||||
```
|
||||
|
||||
Your `calculateDifference` function should have two parameters called `num1` and `num2`.
|
||||
|
||||
```js
|
||||
assert.match(code, /calculateDifference\s*(?:=\s*)?\(\s*num1\s*,\s*num2\s*\)\s*/);
|
||||
```
|
||||
|
||||
Your `calculateDifference` function should return the difference between the two parameters.
|
||||
|
||||
```js
|
||||
assert.equal(calculateDifference(5, 2), 3);
|
||||
assert.equal(calculateDifference(10, 5), 5);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
---
|
||||
id: 66cc16abc30b367ba1d1f9ef
|
||||
title: Step 11
|
||||
challengeType: 1
|
||||
dashedName: step-11
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now it is time to test your `calculateDifference` function.
|
||||
|
||||
Start by adding a `console.log` that calls the `calculateDifference` function with the arguments `22` and `5`.
|
||||
|
||||
Then, add a second `console.log` that calls the `calculateDifference` function with the arguments `12` and `1`.
|
||||
|
||||
Finally, add a third `console.log` that calls the `calculateDifference` function with the arguments `17` and `9`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` that calls the `calculateDifference` function with the arguments `22` and `5`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateDifference\s*\(\s*22\s*,\s*5\s*\)\s*\)\s*;?/);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateDifference` function with the arguments `12` and `1`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateDifference\s*\(\s*12\s*,\s*1\s*\)\s*\)\s*;?/);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateDifference` function with the arguments `17` and `9`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateDifference\s*\(\s*17\s*,\s*9\s*\)\s*\)\s*;?/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
---
|
||||
id: 66cc172af7159a7c67804544
|
||||
title: Step 12
|
||||
challengeType: 1
|
||||
dashedName: step-12
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Now it is time to add the multiplication functionality to your calculator.
|
||||
|
||||
Declare a function called `calculateProduct` that takes two parameters, `num1` and `num2`.
|
||||
|
||||
Inside the function, return the product of the two parameters.
|
||||
|
||||
Then, add a `console.log` that calls the `calculateProduct` function with the arguments `13` and `5`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function called `calculateProduct`.
|
||||
|
||||
```js
|
||||
assert.isFunction(calculateProduct);
|
||||
```
|
||||
|
||||
Your `calculateProduct` function should have two parameters called `num1` and `num2`.
|
||||
|
||||
```js
|
||||
assert.match(code, /calculateProduct\s*(?:=\s*)?\(\s*num1\s*,\s*num2\s*\)\s*/);
|
||||
```
|
||||
|
||||
Your `calculateProduct` function should return the product of the two parameters.
|
||||
|
||||
```js
|
||||
assert.equal(calculateProduct(5, 2), 10);
|
||||
assert.equal(calculateProduct(10, 5), 50);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateProduct` function with the arguments `13` and `5`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateProduct\s*\(\s*13\s*,\s*5\s*\)\s*\)\s*;?/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
---
|
||||
id: 66cc1a3a39aef47d6473cb2f
|
||||
title: Step 13
|
||||
challengeType: 1
|
||||
dashedName: step-13
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The next step is to add the division functionality to your calculator.
|
||||
|
||||
Declare a function called `calculateQuotient` that takes two parameters, `num1` and `num2`.
|
||||
|
||||
Inside the function, return the quotient of the two parameters.
|
||||
|
||||
Then, add a `console.log` that calls the `calculateQuotient` function with the arguments `7` and `11`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function called `calculateQuotient`.
|
||||
|
||||
```js
|
||||
assert.isFunction(calculateQuotient);
|
||||
```
|
||||
|
||||
Your `calculateQuotient` function should have two parameters called `num1` and `num2`.
|
||||
|
||||
```js
|
||||
assert.match(code, /calculateQuotient\s*(?:=\s*)?\(\s*num1\s*,\s*num2\s*\)\s*/);
|
||||
```
|
||||
|
||||
Your `calculateQuotient` function should return the quotient of the two parameters.
|
||||
|
||||
```js
|
||||
assert.equal(calculateQuotient(10, 2), 5);
|
||||
assert.equal(calculateQuotient(100, 10), 10);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateQuotient` function with the arguments `7` and `11`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateQuotient\s*\(\s*7\s*,\s*11\s*\)\s*\)\s*;?/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
---
|
||||
id: 66cc1ccfefdd727e18c2ab20
|
||||
title: Step 14
|
||||
challengeType: 1
|
||||
dashedName: step-14
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
Your `calculateQuotient` appears to working correctly but there is one case that you have not tested yet.
|
||||
|
||||
Add a `console.log` that calls the `calculateQuotient` function with the arguments `3` and `0`.
|
||||
|
||||
Make sure to take a close look at the output of this call.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` that calls the `calculateQuotient` function with the arguments `3` and `0`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateQuotient\s*\(\s*3\s*,\s*0\s*\)\s*\)\s*;?/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num1 / num2;
|
||||
}
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
---
|
||||
id: 66cc1deb1f04647f2aabee2b
|
||||
title: Step 15
|
||||
challengeType: 1
|
||||
dashedName: step-15
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
If you look in the console, you will see the `Infinity` value. `Infinity` is a special value in JavaScript that represents a number that is greater than any other number.
|
||||
|
||||
The division by zero is not a valid operation in mathematics.
|
||||
|
||||
To account for this edge case, you should update your `calculateQuotient` function to instead check if `num2` is zero.
|
||||
|
||||
If it is, the function should return the string `'Error: Division by zero'`. Otherwise, it should return the result of dividing `num1` by `num2`.
|
||||
|
||||
# --hints--
|
||||
|
||||
Your `calculateQuotient` function should return the string `'Error: Division by zero'` if `num2` is zero.
|
||||
|
||||
```js
|
||||
assert.strictEqual(calculateQuotient(10, 0), 'Error: Division by zero');
|
||||
assert.strictEqual(calculateQuotient(3, 0), 'Error: Division by zero');
|
||||
```
|
||||
|
||||
Your `calculateQuotient` function should return the result of dividing `num1` by `num2` if `num2` is not zero.
|
||||
|
||||
```js
|
||||
assert.strictEqual(calculateQuotient(10, 2), 5);
|
||||
assert.strictEqual(calculateQuotient(3, 3), 1);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num1 / num2;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
console.log(calculateQuotient(3, 0));
|
||||
```
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
---
|
||||
id: 66cc21d23238dc8240a8a182
|
||||
title: Step 16
|
||||
challengeType: 1
|
||||
dashedName: step-16
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
It would be nice to have your calculator calculate the square of a number. The square of a number is the number multiplied by itself.
|
||||
|
||||
To calculate the square of a number in JavaScript, you can use the <dfn>Math.pow()</dfn> method. The `Math.pow()` method takes two arguments: the base number and the exponent.
|
||||
|
||||
You can also use the exponentiation operator (`**`) to calculate the square of a number.
|
||||
|
||||
Here is an example:
|
||||
|
||||
```js
|
||||
// base number is 5
|
||||
// the exponent is 2
|
||||
Math.pow(5, 2); // 25
|
||||
|
||||
|
||||
// using the exponentiation operator
|
||||
5 ** 2; // 25
|
||||
```
|
||||
|
||||
Declare a function called `calculateSquare` that takes a `num` parameter and returns the square of `num`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function called `calculateSquare`.
|
||||
|
||||
```js
|
||||
assert.isFunction(calculateSquare);
|
||||
```
|
||||
|
||||
Your `calculateSquare` function should take a `num` parameter.
|
||||
|
||||
```js
|
||||
assert.match(code, /calculateSquare\s*(?:=\s*)?\(\s*num\s*\)/);
|
||||
```
|
||||
|
||||
Your `calculateSquare` function should return the square of `num`.
|
||||
|
||||
```js
|
||||
assert.equal(calculateSquare(5), 25);
|
||||
assert.equal(calculateSquare(10), 100);
|
||||
assert.equal(calculateSquare(3), 9);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num2 === 0 ? "Error: Division by zero" : num1 / num2;
|
||||
}
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
console.log(calculateQuotient(3, 0));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
---
|
||||
id: 66cc26907ff6908402af0149
|
||||
title: Step 17
|
||||
challengeType: 1
|
||||
dashedName: step-17
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
To test your `calculateSquare` function, you will need to call the function a couple of times.
|
||||
|
||||
Add a `console.log` that calls the `calculateSquare` function with the argument of `2`.
|
||||
|
||||
Then, add another `console.log` that calls the `calculateSquare` function with the argument of `9`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` that calls the `calculateSquare` function with the argument of `2`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateSquare\s*\(\s*2\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateSquare` function with the argument of `9`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateSquare\s*\(\s*9\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num2 === 0 ? "Error: Division by zero" : num1 / num2;
|
||||
}
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
console.log(calculateQuotient(3, 0));
|
||||
|
||||
function calculateSquare(num) {
|
||||
return num ** 2;
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
---
|
||||
id: 66cc2739f687e484d50bb6f1
|
||||
title: Step 18
|
||||
challengeType: 1
|
||||
dashedName: step-18
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The last piece of functionality for your calculator will be the square root functionality.
|
||||
|
||||
A square is a number multiplied by itself. So, the square root would be the number that when multiplied by itself gives the original number.
|
||||
|
||||
For example, the square root of `9` is `3` because `3 * 3 = 9`.
|
||||
|
||||
To get the square root of a number in JavaScript, you can use the <dfn>Math.sqrt()</dfn> method.
|
||||
|
||||
Here is an example:
|
||||
|
||||
```js
|
||||
// result: 3
|
||||
Math.sqrt(9);
|
||||
```
|
||||
|
||||
Declare a function called `calculateSquareRoot` that takes a `num` parameter and returns the square root of `num`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a function called `calculateSquareRoot`.
|
||||
|
||||
```js
|
||||
assert.isFunction(calculateSquareRoot);
|
||||
```
|
||||
|
||||
Your `calculateSquareRoot` function should take a `num` parameter.
|
||||
|
||||
```js
|
||||
assert.match(code, /calculateSquareRoot\s*(?:=\s*)?\(\s*num\s*\)/);
|
||||
```
|
||||
|
||||
Your `calculateSquareRoot` function should return the square root of `num`.
|
||||
|
||||
```js
|
||||
assert.equal(calculateSquareRoot(9), 3);
|
||||
assert.equal(calculateSquareRoot(16), 4);
|
||||
assert.equal(calculateSquareRoot(25), 5);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num2 === 0 ? "Error: Division by zero" : num1 / num2;
|
||||
}
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
console.log(calculateQuotient(3, 0));
|
||||
|
||||
function calculateSquare(num) {
|
||||
return num ** 2;
|
||||
}
|
||||
|
||||
console.log(calculateSquare(2));
|
||||
console.log(calculateSquare(9));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
---
|
||||
id: 66cc281d687975858049fd8d
|
||||
title: Step 19
|
||||
challengeType: 1
|
||||
dashedName: step-19
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
In the final step of the workshop, you will need to test out your `calculateSquareRoot` function.
|
||||
|
||||
Add a second `console.log` that calls the `calculateSquareRoot` function with the argument of `25`.
|
||||
|
||||
Then, add a third `console.log` that calls the `calculateSquareRoot` function with the argument of `100`.
|
||||
|
||||
And with those changes, your calculator app is complete!
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` that calls the `calculateSquareRoot` function with the argument of `25`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateSquareRoot\s*\(\s*25\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateSquareRoot` function with the argument of `100`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console\.log\s*\(\s*calculateSquareRoot\s*\(\s*100\s*\)\s*\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num2 === 0 ? "Error: Division by zero" : num1 / num2;
|
||||
}
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
console.log(calculateQuotient(3, 0));
|
||||
|
||||
function calculateSquare(num) {
|
||||
return num ** 2;
|
||||
}
|
||||
|
||||
console.log(calculateSquare(2));
|
||||
console.log(calculateSquare(9));
|
||||
|
||||
function calculateSquareRoot(num) {
|
||||
return Math.sqrt(num);
|
||||
}
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
console.log(calculateSum(10, 10));
|
||||
console.log(calculateSum(5, 5));
|
||||
|
||||
function calculateDifference(num1, num2) {
|
||||
return num1 - num2;
|
||||
}
|
||||
|
||||
console.log(calculateDifference(22, 5));
|
||||
console.log(calculateDifference(12, 1));
|
||||
console.log(calculateDifference(17, 9));
|
||||
|
||||
function calculateProduct(num1, num2) {
|
||||
return num1 * num2;
|
||||
}
|
||||
|
||||
console.log(calculateProduct(13, 5));
|
||||
|
||||
|
||||
function calculateQuotient(num1, num2) {
|
||||
return num2 === 0 ? "Error: Division by zero" : num1 / num2;
|
||||
}
|
||||
|
||||
console.log(calculateQuotient(7, 11));
|
||||
console.log(calculateQuotient(3, 0));
|
||||
|
||||
function calculateSquare(num) {
|
||||
return num ** 2;
|
||||
}
|
||||
|
||||
console.log(calculateSquare(2));
|
||||
console.log(calculateSquare(9));
|
||||
|
||||
function calculateSquareRoot(num) {
|
||||
return Math.sqrt(num);
|
||||
}
|
||||
|
||||
console.log(calculateSquareRoot(25));
|
||||
console.log(calculateSquareRoot(100));
|
||||
```
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
---
|
||||
id: 66cdf76685e4cb5a8726e27b
|
||||
title: Step 9
|
||||
challengeType: 1
|
||||
dashedName: step-9
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
To further test out your `calculateSum` function, you will need to call it with different arguments.
|
||||
|
||||
Add another `console.log` that calls the `calculateSum` function with the arguments `10` and `10`.
|
||||
|
||||
Below that `console.log`, add another `console.log` that calls the `calculateSum` function with the arguments `5` and `5`.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should have a `console.log` that calls the `calculateSum` function with the arguments `10` and `10`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console.log\(calculateSum\(10\s*,\s*10\)\)/);
|
||||
```
|
||||
|
||||
You should have a `console.log` that calls the `calculateSum` function with the arguments `5` and `5`.
|
||||
|
||||
```js
|
||||
assert.match(code, /console.log\(calculateSum\(5\s*,\s*5\)\)/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function calculateSum(num1, num2) {
|
||||
return num1 + num2;
|
||||
}
|
||||
|
||||
console.log(calculateSum(2, 5));
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
Reference in New Issue
Block a user