JavaScript: Default Arguments
Consider the round() function, which rounds an integer:
const result = round(10.25, 0); // 10We pass two parameters to it: the number to round and the precision of rounding. 0 means that it will round to an integer, and the decimal part will simply be discarded.
In most cases, we need to round exactly to an integer (and not to tenths, for example), so designers of the round() function have made the second parameter optional and set it to a default value of 0. So, you can choose to not specify the second parameter and the result will be the same:
const result = round(10.25); // 10We can specify precision by passing another parameter:
// rounding to one decimal place
const result = round(10.25, 1); // 10.3If a JavaScript function accepts optional arguments, they always come after the required ones. Their number varies depending on the function itself, but they always go next to each other and at the end of the list of arguments.
Instructions
Round up the value of the constant number to two decimal places and print the result.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
JavaScript: Default Arguments
Consider the round() function, which rounds an integer:
const result = round(10.25, 0); // 10We pass two parameters to it: the number to round and the precision of rounding. 0 means that it will round to an integer, and the decimal part will simply be discarded.
In most cases, we need to round exactly to an integer (and not to tenths, for example), so designers of the round() function have made the second parameter optional and set it to a default value of 0. So, you can choose to not specify the second parameter and the result will be the same:
const result = round(10.25); // 10We can specify precision by passing another parameter:
// rounding to one decimal place
const result = round(10.25, 1); // 10.3If a JavaScript function accepts optional arguments, they always come after the required ones. Their number varies depending on the function itself, but they always go next to each other and at the end of the list of arguments.
Instructions
Round up the value of the constant number to two decimal places and print the result.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
// @ts-check
import { expect, test, vi } from 'vitest';
test('calling functions default params', async () => {
const consoleLogSpy = vi.spyOn(console, 'log');
await import('./index.js');
const firstArg = consoleLogSpy.mock.calls.join('\n');
expect(firstArg).toBe('37.8\n2426.76\n607');
});Teacher's solution will be available in:
20:00
