JavaScript: Function parameters
To add parameters to your function, simply specify them within parentheses when defining your function. Below is an example of a function returning the last character of a string:
// str - parameter
const getLastChar = (str) => {
// calculate the index of the last character
// extract it, and return it
return str[str.length - 1];
};
getLastChar('Hexlet'); // "t"
getLastChar('Goo'); // "o"
The getLastChar()
function has only one parameter - the str
variable. When calling a function, it's replaced by a particular value specific to each call. But this code won't work:
const getLastChar = ('some string') => {
A specific value can't be a parameter, the point of a parameter is that the value itself will only be revealed when a specific call is made, so there must be names of variables in the definition.
A function can have two, three or more parameters. Below is an example of a function that finds the average between two numbers:
const average = (x, y) => {
return (x + y) / 2;
};
average(1, 5); // 3
average(1, 2); // 1.5
The order of parameters in a definition should match the order they are passed, so pay close attention to the documentation. In rare cases like above, the order is not important, but for most functions it's crucial:
// Rounding. The first parameter is a number, the second is the rounding precision
round(10.32, 1); // 10.3
// This call makes no sense at all, though it works
round(1, 10.32); // 1
If you pass more parameters than defined, JavaScript will ignore the extra ones. Otherwise, values of missed parameters will be undefined
.
average(10, 10, 10); // 10
// (10 + undefined) / 2
average(10); // NaN
There is no real point to this code, but it's possible to write it by mistake. So it's worth knowing about this kind of behavior to be able to recognize the error and fix it.
Parameters in JavaScript are often called arguments. In programming, you'll come across these words often as synonyms. But there is still a slight difference between them. Technically, parameters are set in the parentheses when you define a function, and arguments are what's passed to the function when it's called.
Instructions
Write a truncate()
function that cuts off the text you pass and adds an ellipsis at the end. This kind of logic is often used on websites to display long text in shortened form.
// The first parameter is a string, the second is the number of characters to keep
truncate('hexlet', 2); // "he..."
const result = truncate("Leather bags invented the three laws of robotics, but we'll outsmart them", 48);
// "Leather bags invented the three laws of robotics..."
If you want to get a substring (or a character) from a string, use the slice() method:
'welcome'.slice(2, 5); // "lco"
The exercise doesn't pass checking. What to do? 😶
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.
In my environment the code works, but not here 🤨
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
My code is different from the teacher's one 🤔
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
I've read the lessons but nothing is clear 🙄
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.