mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-06-19 21:09:51 +08:00
refactor(curriculum): functions quiz (#58381)
Co-authored-by: Dario-DC <105294544+Dario-DC@users.noreply.github.com>
This commit is contained in:
parent
fcd713c66c
commit
201ea4760f
@ -17,23 +17,113 @@ To pass the quiz, you must correctly answer at least 18 of the 20 questions belo
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the correct syntax for a function declaration?
|
||||
Which of the following is the correct way to declare a function in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`var myFunction = function() {};`
|
||||
```js
|
||||
func greet () {
|
||||
console.log("Hello there!");
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
`myFunction: function() {}`
|
||||
```js
|
||||
defineFunction greet () {
|
||||
console.log("Hello there!");
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
`function = myFunction();`
|
||||
```js
|
||||
def greet () {
|
||||
console.log("Hello there!");
|
||||
}
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
`function myFunction() {}`
|
||||
```js
|
||||
function greet () {
|
||||
console.log("Hello there!");
|
||||
}
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What happens when you call (or execute) a function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
The code inside the function will run and `null` will always be returned.
|
||||
|
||||
---
|
||||
|
||||
Nothing will happen when you call a function.
|
||||
|
||||
---
|
||||
|
||||
A `TypeError` will be thrown and the program will crash.
|
||||
|
||||
#### --answer--
|
||||
|
||||
The code inside the function will run and a value will be returned.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is the correct way to call a function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
```js
|
||||
exampleFunction;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
exampleFunction>>;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
()exampleFunction();
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
```js
|
||||
exampleFunction();
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the role of the `return` keyword?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
The `return` keyword will execute the function.
|
||||
|
||||
---
|
||||
|
||||
The `return` keyword will throw a type error.
|
||||
|
||||
---
|
||||
|
||||
The `return` keyword serves no purpose in functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
The `return` keyword returns a value from the function.
|
||||
|
||||
### --question--
|
||||
|
||||
@ -44,7 +134,7 @@ Which of the following is a valid function expression?
|
||||
#### --distractors--
|
||||
|
||||
```js
|
||||
function getSum(x, y) {
|
||||
expression function getSum(x, y) {
|
||||
return x + y;
|
||||
};
|
||||
```
|
||||
@ -68,7 +158,7 @@ function = getSum(x, y) {
|
||||
#### --answer--
|
||||
|
||||
```js
|
||||
getSum = function(x, y) {
|
||||
const getSum = function(x, y) {
|
||||
return x + y;
|
||||
};
|
||||
```
|
||||
@ -77,408 +167,393 @@ getSum = function(x, y) {
|
||||
|
||||
#### --text--
|
||||
|
||||
Where can function expressions be used?
|
||||
What are function arguments?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Only within other functions
|
||||
Values that represents the absence of a value.
|
||||
|
||||
---
|
||||
|
||||
Only as part of an object literal
|
||||
Special values that act similar to anonymous functions.
|
||||
|
||||
---
|
||||
|
||||
Only in the global scope
|
||||
Placeholder values used inside of the function.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Anywhere a variable can be used
|
||||
Real values passed to a function when it is called.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the difference between a function expression and a function declaration?
|
||||
What are function parameters?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Function expressions can be hoisted, while function declarations cannot.
|
||||
Floating point values used inside of the function.
|
||||
|
||||
---
|
||||
|
||||
Both function expressions and function declarations can be hoisted.
|
||||
Values passed to a function when it is called.
|
||||
|
||||
---
|
||||
|
||||
Neither function expressions nor function declarations can be hoisted.
|
||||
Values that represent the intentional absence of the value.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Function declarations can be hoisted, while function expressions cannot.
|
||||
Values listed inside the function definition.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What does it mean for functions to be first-class citizens in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
They can be assigned to variables.
|
||||
|
||||
---
|
||||
|
||||
They can be passed as arguments to other functions.
|
||||
|
||||
---
|
||||
|
||||
They can be returned from functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is a common use case for first-class functions in JavaScript?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Implementing object-oriented programming
|
||||
|
||||
---
|
||||
|
||||
Defining global variables
|
||||
|
||||
---
|
||||
|
||||
Creating custom data types
|
||||
|
||||
#### --answer--
|
||||
|
||||
Creating higher-order functions
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is an example of a first-class function?
|
||||
|
||||
#### --distractors--
|
||||
What value will be returned in the following code example?
|
||||
|
||||
```js
|
||||
function greet(name) {
|
||||
console.log("Hello, " + name);
|
||||
function greet () {
|
||||
console.log("Hello there!");
|
||||
}
|
||||
|
||||
greet();
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`NaN`
|
||||
|
||||
---
|
||||
|
||||
`TypeError`
|
||||
|
||||
---
|
||||
|
||||
`null`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`undefined`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is the correct syntax for an arrow function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
```js
|
||||
const sum <<>> (num1, num2) => num1 + num2
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
var greet = function(name) {
|
||||
console.log("Hello, " + name);
|
||||
};
|
||||
const sum === (num1, num2) === num1 + num2
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
const greet = (name) => {
|
||||
console.log("Hello, " + name);
|
||||
const sum >=> (num1, num2) => num1 + num2
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
```js
|
||||
const sum = (num1, num2) => num1 + num2
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What will be result for the following code?
|
||||
|
||||
```js
|
||||
function greet() {
|
||||
const developer = "Jessica";
|
||||
console.log("Hello there!");
|
||||
}
|
||||
|
||||
console.log(developer);
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Nothing will happen and the code will run normally.
|
||||
|
||||
---
|
||||
|
||||
The string `"Hello there!"` will be logged to the console.
|
||||
|
||||
---
|
||||
|
||||
The string `"Hello!"` will be logged to the console.
|
||||
|
||||
#### --answer--
|
||||
|
||||
All of the answers are correct.
|
||||
You will get a reference error because `developer` is not defined globally.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is not a common use case for first-class functions?
|
||||
When can you omit a set of parentheses around a parameter list for an arrow function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Defining object methods
|
||||
You should always omit the parentheses around a parameter list.
|
||||
|
||||
---
|
||||
|
||||
Implementing closures
|
||||
You are never supposed to omit the parentheses for an arrow function.
|
||||
|
||||
---
|
||||
|
||||
Creating callbacks
|
||||
You should only omit the parentheses when you are working with multiple parameters.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Creating custom data types
|
||||
You should only omit the parentheses when you only have one parameter.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the syntax for an arrow function?
|
||||
When can you omit the curly braces and `return` keyword for an arrow function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`myFunction: function() {}`
|
||||
You should always omit the curly braces and `return` keyword.
|
||||
|
||||
---
|
||||
|
||||
`function myFunction() {}`
|
||||
You should never omit the curly braces and `return` keyword.
|
||||
|
||||
---
|
||||
|
||||
`const myFunction = function() {};`
|
||||
When the function body has multiple lines of code.
|
||||
|
||||
#### --answer--
|
||||
|
||||
`myFunction = () => { ... };`
|
||||
When the function body consists of a single expression.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the difference between a regular function and an arrow function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Arrow functions do not have their own 'this' binding.
|
||||
|
||||
---
|
||||
|
||||
Arrow functions cannot be used as constructors.
|
||||
|
||||
---
|
||||
|
||||
Arrow functions cannot be used with the 'new' keyword.
|
||||
|
||||
#### --answer--
|
||||
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
You can also call arrow function as
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Lambda function
|
||||
|
||||
---
|
||||
|
||||
Fat arrow function
|
||||
|
||||
---
|
||||
|
||||
Neither Lambda function or Fat arrow function
|
||||
|
||||
#### --answer--
|
||||
|
||||
Both Lambda function and Fat arrow function
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the benefit of using arrow functions?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
You can omit curly braces and return keyword if your output is going to be a single line.
|
||||
|
||||
---
|
||||
|
||||
They provide easy syntax while writing promises and callbacks.
|
||||
|
||||
---
|
||||
|
||||
They have short syntax and increase readability.
|
||||
|
||||
#### --answer--
|
||||
|
||||
All of the answers are correct.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the difference between a function and a method?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
There is no difference between functions and methods.
|
||||
|
||||
---
|
||||
|
||||
Functions and methods are properties of objects
|
||||
|
||||
---
|
||||
|
||||
Functions are properties of objects, while methods are standalone functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Methods are properties of objects, while functions are standalone functions.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Can a method be used outside of its object?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Yes, methods can be used anywhere a function can be used.
|
||||
|
||||
---
|
||||
|
||||
Methods can be used outside of their object, but only if they are bound to a specific 'this' value.
|
||||
|
||||
---
|
||||
|
||||
Methods can be used outside of their object, but only if they are defined as arrow functions.
|
||||
|
||||
#### --answer--
|
||||
|
||||
No, methods can only be used within their object.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is an example of a method?
|
||||
|
||||
#### --distractors--
|
||||
What will be the result for the following code?
|
||||
|
||||
```js
|
||||
function greet(name) {
|
||||
console.log("Hello, " + name);
|
||||
function exampleFunction () {
|
||||
|
||||
}
|
||||
exampleFunction();
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
The program will crash.
|
||||
|
||||
---
|
||||
|
||||
JavaScript will skip over that piece of code.
|
||||
|
||||
---
|
||||
|
||||
An error message will display.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Nothing will happen.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
Which of the following is the correct way to use default parameters?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
```js
|
||||
function sum (num1, num2 <<>> 12) {
|
||||
return num1 + num2
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```js
|
||||
var greet = function(name) {
|
||||
console.log("Hello, " + name);
|
||||
};
|
||||
function sum (num1, num2 === 12) {
|
||||
return num1 + num2
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
All of them.
|
||||
```js
|
||||
function sum (num1, num2 >> 12) {
|
||||
return num1 + num2
|
||||
}
|
||||
```
|
||||
|
||||
#### --answer--
|
||||
|
||||
```js
|
||||
const person = {
|
||||
name: "Alice",
|
||||
greet: function() {
|
||||
console.log("Hello, my name is " + this.name);
|
||||
}
|
||||
};
|
||||
function sum (num1, num2 = 12) {
|
||||
return num1 + num2
|
||||
}
|
||||
```
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the benefit of using methods instead of functions?
|
||||
What will be the result for the following code?
|
||||
|
||||
```js
|
||||
const developer = "Jessica";
|
||||
|
||||
function greet() {
|
||||
console.log("Hello, " + developer)
|
||||
}
|
||||
|
||||
greet();
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
Methods provide a more organized way to group related functionality.
|
||||
The string `"Hello, developer"` will be logged to the console.
|
||||
|
||||
---
|
||||
|
||||
Methods can be accessed using dot notation, which can make code more readable.
|
||||
An error message will display in the console.
|
||||
|
||||
---
|
||||
|
||||
Methods have access to the properties and methods of their object.
|
||||
Nothing will display in the console.
|
||||
|
||||
#### --answer--
|
||||
|
||||
All of the answers are correct.
|
||||
The string `"Hello, Jessica"` will be logged to the console.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the difference between arguments and parameters?
|
||||
What happens if you try to remove the curly braces for a regular function?
|
||||
|
||||
#### --distractors--
|
||||
|
||||
There is no difference between arguments and parameters.
|
||||
An alert box will display on the page with an error.
|
||||
|
||||
---
|
||||
|
||||
Parameters are the actual values passed to a function when it is called, while arguments are the names of variables used in a function definition.
|
||||
The program will crash and not start again.
|
||||
|
||||
---
|
||||
|
||||
Arguments and parameters are used interchangeably.
|
||||
Nothing will happen.
|
||||
|
||||
#### --answer--
|
||||
|
||||
Parameters are the names of variables used in a function definition, while arguments are the actual values passed to the function when it is called.
|
||||
You will get a syntax error.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What happens if a function is called with more arguments than it expects?
|
||||
What will be the result for the following code?
|
||||
|
||||
```js
|
||||
const sum = (num1, num2) => num1 + num2
|
||||
console.log(sum(0, 0) + 10);
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
The extra arguments will be assigned to the remaining parameters.
|
||||
An error message will display in the console.
|
||||
|
||||
---
|
||||
|
||||
The function will throw an error.
|
||||
The number `0` will be logged to the console.
|
||||
|
||||
---
|
||||
|
||||
The function will use the default values specified in the function definition.
|
||||
Nothing will be logged to the console.
|
||||
|
||||
#### --answer--
|
||||
|
||||
The extra arguments will be ignored.
|
||||
The number `10` will be logged to the console.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the correct syntax for defining a function with default parameter values?
|
||||
What will be the output for the following code?
|
||||
|
||||
```js
|
||||
const exampleFunction = (param1, param2) => param1 + param2;
|
||||
console.log(exampleFunction(3, "Something"));
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
`function myFunction(param1 = value1, param2) { ... }`
|
||||
`null`
|
||||
|
||||
---
|
||||
|
||||
`function myFunction(param1, param2 = value2) { ... }`
|
||||
`undefined`
|
||||
|
||||
---
|
||||
|
||||
`function myFunction(param1: value1, param2: value2) { ... }`
|
||||
`3 + "Something"`
|
||||
|
||||
#### --answer--
|
||||
|
||||
`function myFunction(param1 = value1, param2 = value2) { ... }`
|
||||
`"3Something"`
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What is the default value for a missing argument in a function call?
|
||||
What will be the result for the following code?
|
||||
|
||||
```js
|
||||
const sum = (num1, num2) => num1 + num2
|
||||
console.log(sum(0, 0) + num2);
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
An error message saying `num1` is not defined.
|
||||
|
||||
---
|
||||
|
||||
The number `0` will be displayed in the console.
|
||||
|
||||
---
|
||||
|
||||
`undefined` will be displayed in the console.
|
||||
|
||||
#### --answer--
|
||||
|
||||
An error message saying `num2` is not defined.
|
||||
|
||||
### --question--
|
||||
|
||||
#### --text--
|
||||
|
||||
What will be the output for the following code?
|
||||
|
||||
```js
|
||||
const divideTwoNumbers = (num1, num2) => num1 / num2;
|
||||
console.log(divideTwoNumbers(3, 0));
|
||||
```
|
||||
|
||||
#### --distractors--
|
||||
|
||||
@ -494,5 +569,5 @@ What is the default value for a missing argument in a function call?
|
||||
|
||||
#### --answer--
|
||||
|
||||
`undefined`
|
||||
`Infinity`
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user