JavaScript: Simplified function syntax
Compared to some (primarily functional) languages, function definition in JavaScript looks rather cumbersome:
const square = (x) => {
return x ** 2;
};It uses a lot of extra characters and the word return. Since version ES6, an alternative, shorter syntax has appeared in the language, making it much easier to understand and shorten code in some cases.
// It takes a little time to get used to it,
// but after a while it'll save your life
const double = (x) => x ** 2;There are two differences from the full definition, it omits the curly brackets and the return statement. The shortened version carries out the return automatically. It means that there is exactly one computable expression inside such a function, and its result will be immediately returned.
Note that the differences are purely syntactic, there are no differences in terms of usage. An example with two arguments:
The full version
const sum = (a, b) => {
return a + b;
};The shortened version
const sum = (a, b) => a + b;Note the absence of curly brackets. Developers unaccustomed to this syntax sometimes write const sum = (a, b) => { a + b }; and then have a hard time understanding why it doesn't work. The answer is simple: if there are curly brackets, then this isn't the shortened form, which means if you want the function to return a value, you will have to use return.
Instructions
Write the capitalize() function that takes a non-empty string and converts the first letter of the first word to uppercase:
const name = 'arya';
console.log(capitalize(name)); // => "Arya"To get a substring (or character) from a string, use the slice() method:
'welcome'.slice(2, 5); // "lco"To convert the string to uppercase, use the toUpperCase() method:
'welcome'.toUpperCase(); // "WELCOME"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: Simplified function syntax
Compared to some (primarily functional) languages, function definition in JavaScript looks rather cumbersome:
const square = (x) => {
return x ** 2;
};It uses a lot of extra characters and the word return. Since version ES6, an alternative, shorter syntax has appeared in the language, making it much easier to understand and shorten code in some cases.
// It takes a little time to get used to it,
// but after a while it'll save your life
const double = (x) => x ** 2;There are two differences from the full definition, it omits the curly brackets and the return statement. The shortened version carries out the return automatically. It means that there is exactly one computable expression inside such a function, and its result will be immediately returned.
Note that the differences are purely syntactic, there are no differences in terms of usage. An example with two arguments:
The full version
const sum = (a, b) => {
return a + b;
};The shortened version
const sum = (a, b) => a + b;Note the absence of curly brackets. Developers unaccustomed to this syntax sometimes write const sum = (a, b) => { a + b }; and then have a hard time understanding why it doesn't work. The answer is simple: if there are curly brackets, then this isn't the shortened form, which means if you want the function to return a value, you will have to use return.
Instructions
Write the capitalize() function that takes a non-empty string and converts the first letter of the first word to uppercase:
const name = 'arya';
console.log(capitalize(name)); // => "Arya"To get a substring (or character) from a string, use the slice() method:
'welcome'.slice(2, 5); // "lco"To convert the string to uppercase, use the toUpperCase() method:
'welcome'.toUpperCase(); // "WELCOME"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 } from 'vitest';
import f from './index.js';
test('define functions short syntax', () => {
const expected1 = 'Daenerys';
const actual1 = f('daenerys');
expect(f(actual1)).toEqual(expected1);
expect(f('hexlet')).toEqual('Hexlet');
});Teacher's solution will be available in:
20:00
