Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Unknown data
Sooner or later, it was supposed to happen. Check out a new task from Muffin.
Third task: “Write a program for calculating the caloric value of my breakfast. I eat milk for breakfast. I want to enter the amount I ate and see its caloric value.”
Why didn’t he put it this way right from the start?! Let’s forgive Muffin. He is a nocturnal, solitary predator, fast and agile, but not as savvy as his colleagues, raccoons. So he will be making tasks more difficult slowly.
It won’t be easy to complete this task from the first attempt, so we will do it step by step.
We have already written code for a program that worked with known data. This data was defined right in the code of the program.
However, you will be mostly working with data unknown during the start of the program. For example, you will be capturing the user’s input and process it.
To begin with, let’s learn to receive data from the user. That is, from the boss a.k.a Muffin. To do it, let’s page him using a special command:
muffin.ask('question for the boss');
This program exists in our console only. It is not built into JavaScript because Muffin doesn’t allow the developers of the language to do it (in spite of many requests). If you build the command into the language, the boss’s pager will explode from so many messages coming in.
You will also learn how to create your own commands in programs, give them names and access them for operations. We’ll see how it’s done in future courses. Try collecting data from Muffin. Don’t forget that a question is a string, and you need to enclose it in quote marks: 'string'
.
- script.js
Thanks! We’ll fix everything at once!
Comments