Start with a decision, not a keyboard
A shop assistant needs to quote the price of three identical notebooks. Each costs 20 units. There are no discounts, taxes or delivery charges in this exercise.
Pause and write: What information is available? What result should be produced? Which assumptions would need confirmation for a real sale?
The inputs are quantity 3 and unit price 20. The expected total is 60. The exclusions above are exercise rules, not universal rules about shops. If they were not stated, you would need to ask.
If distinguishing a rule from an assumption still feels difficult, revisit Software Foundations.
From intention to executable instructions
An algorithm describes the steps: obtain the unit price, obtain the quantity, multiply them, report the total. Source code expresses those steps in a language with precise notation. An engine reads and executes JavaScript. A runtime is the surrounding environment that provides facilities the program can use.
A browser includes a JavaScript engine and facilities such as a console for displaying messages. JavaScript can also run outside a browser. In this module, we use only the browser console: no HTML, buttons, DOM or installations are needed.
The engine cannot decide whether the shop's rule is reasonable. It executes the instructions you express. A program can execute successfully and still answer the wrong question.
Set up your practice space
- Open a blank browser tab on a desktop computer.
- Open the browser menu, find Developer Tools (sometimes under More Tools or Web Developer), then choose Console. Menu names vary by browser.
- Type
console.log("Ready to reason");yourself and press Enter. - Look for the message
Ready to reason.
console.log asks the environment to display a value. The parentheses contain what to display. Quotes mark text, called a string. The semicolon ends this statement. A statement is an instruction; an expression, such as 3 * 20, produces a value.
The console may also display undefined as the result of the logging call. That is separate from the message you asked it to print; it is not a failure. Type the short examples yourself. You do not need to enable browser paste overrides or run code from an unfamiliar source.
If you cannot access developer tools yet, predict the outputs on paper and compare them with the explanations. Return later to execute them.
Predict, then run
Write the expected messages in order before entering these lines:
console.log("Quote started");
console.log(3 * 20);
console.log("Quote finished");
Explain the result
The messages are Quote started, 60, then Quote finished. These synchronous statements execute in order. Multiplication is evaluated before its result is passed to console.log.
The console displays information; it does not save an order or take payment. Avoid reading more behavior into a program than its instructions describe.
A misleading first implementation
console.log("3 * 20");
Pause: Does this calculate the price?
It prints the text 3 * 20. Without quotes, 3 * 20 is an arithmetic expression. With quotes, it is text. Clear human intention cannot compensate for a different instruction.
Guided exercise: trace the execution
Predict these outputs without running the code:
console.log(20);
console.log(20 * 3);
console.log("Total");
Correction
The outputs are 20, 60, Total. The word Total labels nothing automatically. To communicate more clearly, print the label before the amount. Readability includes what another person can understand from the output.
Independent exercise: a changed quantity
The customer now wants four notebooks. Write the expected total first. Change the relevant expression, run the program and compare. Then explain what should remain unchanged.
Correction and reasoning
The expected total is 80. The expression becomes 4 * 20. The unit price and the order of the surrounding messages stay unchanged. This tiny change already practices identifying which decision is affected by new information.
Common mistakes
- Quoting an expression when a calculation was intended.
- Confusing a displayed message with stored business data.
- Running the program before deciding what result is expected.
- Assuming every JavaScript program always runs in a browser.
Knowledge check
- Can a program run without errors and still be wrong?
- What is the difference between an algorithm and source code?
- Why predict the output first?
Answers
- Yes. It may faithfully implement the wrong rule.
- The algorithm expresses the steps; source code expresses them in a programming language.
- An independent prediction gives you something to compare against. Otherwise any output can look plausible.
What you learned
Execution follows explicit instructions. Your responsibility is to understand the intended behavior, express it precisely and check whether the observed result matches it.