Loading…
Everything will be ready in few seconds
- Theory
- Theory
- Comments
Build it yourself!
Meow!
Stop messing around, time to work!
We have a new project, we need to write a computer store. It’s a serious customer: everything must be done in the best possible way. Here is a small technical assignment:
- The user selects the computer, and the program logs the result of the order.
- The message should include technical characteristics and the final price of the product.
- There is a base price for any computer, and the final amount depends on each technical indicator of the device.
I’m waiting for the result! Don’t let me down!
As before, we will analyze the task to understand what the program will consist of.
Obviously, we won’t be able to cope without a function. We will transfer the selected computer to the function and get the final amount. When performing calculations, we must take into account the base price of the device and the cost of each characteristic. For example, the prices of computers with a screen of 13 and 15 inches will be different. It turns out, each device has indicators: the base price and technical characteristics (RAM, processor and display size). In other words, we have several properties pertaining to one entity, a computer. Have you guessed where we will store it? That’s right, in the object.
As a result, we’ll get a computer configurator. In the object, we’ll store the computer and its characteristics, and the object will be stored in the body of the function. When choosing a device, we will call the configurator function, which will write to the computer’s object its characteristics, calculate the price and return it.
First, create the buildComputer
function and the computer
object inside it, and then check that the function returns it.
- script.js
Thanks! We’ll fix everything at once!
The code has changed, click “Run” or turn autorun on.
Result
- Declare function
buildComputer
with parametersmemory
,display
,processor
. - Inside the function, create object
computer
with propertiesbasicPrice: 5000
,processor: processor
,display: display
,memory: memory
. - Return the
computer
object from the function. - Call the function using arguments
8
,13
,'i7'
and record the result of the call in variablemyComputer
. - Log the
myComputer
variable in the console.
Comments