dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
96 lines
1.6 KiB
Markdown
96 lines
1.6 KiB
Markdown
---
|
|
id: 598e8944f009e646fc236146
|
|
title: Understanding Undefined Value returned from a Function
|
|
challengeType: 1
|
|
forumTopicId: 301177
|
|
dashedName: understanding-undefined-value-returned-from-a-function
|
|
---
|
|
|
|
# --description--
|
|
|
|
A function can include the `return` statement but it does not have to. In the case that the function doesn't have a `return` statement, when you call it, the function processes the inner code but the returned value is `undefined`.
|
|
|
|
**Example**
|
|
|
|
```js
|
|
let sum = 0;
|
|
|
|
function addSum(num) {
|
|
sum = sum + num;
|
|
}
|
|
|
|
addSum(3);
|
|
```
|
|
|
|
`addSum` is a function without a `return` statement. The function will change the global `sum` variable but the returned value of the function is `undefined`.
|
|
|
|
# --instructions--
|
|
|
|
Create a function `addFive` without any arguments. This function adds 5 to the `sum` variable, but its returned value is `undefined`.
|
|
|
|
# --hints--
|
|
|
|
`addFive` should be a function.
|
|
|
|
```js
|
|
assert(typeof addFive === 'function');
|
|
```
|
|
|
|
Once both functions have run, the `sum` should be equal to `8`.
|
|
|
|
```js
|
|
assert(sum === 8);
|
|
```
|
|
|
|
Returned value from `addFive` should be `undefined`.
|
|
|
|
```js
|
|
assert(addFive() === undefined);
|
|
```
|
|
|
|
Inside the `addFive` function, you should add `5` to the `sum` variable.
|
|
|
|
```js
|
|
assert(
|
|
__helpers.removeWhiteSpace(addFive.toString()).match(/sum=sum\+5|sum\+=5/)
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// Setup
|
|
let sum = 0;
|
|
|
|
function addThree() {
|
|
sum = sum + 3;
|
|
}
|
|
|
|
// Only change code below this line
|
|
|
|
|
|
// Only change code above this line
|
|
|
|
addThree();
|
|
addFive();
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
let sum = 0;
|
|
|
|
function addThree() {
|
|
sum = sum + 3;
|
|
}
|
|
|
|
function addFive() {
|
|
sum = sum + 5;
|
|
}
|
|
|
|
addThree();
|
|
addFive();
|
|
```
|