Free JavaScript course. Sign Up for tracking progress →

JavaScript: Defining with expressions

Variables are helpful not only for storing and reusing data but also for simplifying complex calculations. Consider an example: you need to convert euros into yuans through dollars. Banks often do this kind of conversion via an intermediate currency when shopping abroad.

First, convert 50 euros into dollars. Suppose that one euro is $1.25:

let dollarsCount = 50 * 1.25;
console.log(dollarsCount); // => 62.5

Last lesson, we assigned a specific value to a variable. Here, when we declare our variable, let dollarsCount = 50 * 1.25; we have an expression to the right of the equals sign. The interpreter computes the result - 62.5, and records it in a variable. An interpreter doesn't care about what is in front of it: 62.5 or 50 * 1.25, both are expressions to compute. And they result in the same value, 62.5.

Any string is an expression. The concatenation of strings is also an expression. The interpreter processes an encountered expression and produces, as a result, the value of the expression. Here are some sample expressions with comments on the right containing the resulting value.

62.5             // 62.5
50 * 1.25        // 62.5
120 / 10 * 2     // 24

'hello'          // "hello"
'Good' + 'will'  // "Goodwill"

The code structure rules (language grammar) ensure that any calculation (not just math, but also things like string concatenation) can take place when an expression is expected, and the program will continue. For this reason, it's impossible to describe and show all use cases of all operations.

Programs consist of numerous combinations of expressions and understanding this concept is a major step on your journey.

Based on the above, consider whether this code will work?

let who = "dragon's" + 'mother';
console.log(who);

Run it on repl.it and experiment a little with it.

Now, back to our currency program. We'll record the dollar value in yuans as a separate variable. Calculate the value of 50 euros in dollars by multiplying them by 1.25. Suppose that 1 dollar is 6.91 yuans:

let yuansPerDollar = 6.91;
let dollarsCount = 50 * 1.25; // 62.5
let yuansCount = dollarsCount * yuansPerDollar; // 431.875

console.log(yuansCount);

Now, let's merge our output with some text via concatenation:

let yuansPerDollar = 6.91;
let dollarsCount = 50 * 1.25; // 62.5
let yuansCount = dollarsCount * yuansPerDollar; // 431.875

console.log('The price is ' + yuansCount + ' yuans');
The price is 431.875 yuans

Any variable can be part of any expression. During the computation, the variable's name is replaced with its value.

The interpreter calculates the value of dollarsCount before using it in other expressions. When it comes to using the variable, Javascript "knows" the value it has already computed.

Instructions

Write a program that takes the initial amount of euros stored in the variable eurosCount, converts euros to dollars, and prints it. Then, it should convert the result to yuans and print it on a new line.

Sample output for 100 euros:

125
863.75

Suppose that:
- 1 euro = 1.25 dollars
- 1 dollar = 6.91 yuans

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.


If you got stuck and don't know what to do, you can ask a question in our community