← Trail· Lesson 1 of 10
See this lesson in TypeScript →
What is an algorithm
Turn a problem into clear steps: input, processing and output.

An algorithm is a sequence of steps that solves a problem. A recipe is an everyday example: order matters, and each instruction must be clear enough to follow without guessing.
Input → processing → output
To calculate change when the payment covers the price:
- Input: receive the amount paid and the price.
- Processing: subtract the price from the amount paid.
- Output: show the change.
Write the steps in your own words before typing. Reversing the subtraction changes the answer.
Try the console
const names a value, - subtracts, and console.log displays the output in the Console panel. Change the price. The example uses Portuguese identifiers: valorPago means amount paid, preco price, and troco change.
Execute o código para ver a saída aqui.Exercise: fix the change
calcularTroco(valorPago, preco) calculates change in the wrong order. Change the expression after return to subtract the price from the amount paid. Assume sufficient payment.
The function shell is ready: the names in parentheses receive input, and return gives back the output. Only edit the calculation; functions get their own lesson later. Keep the function name.
Click Verificar (Check). The exact-payment case can pass even with reversed subtraction, which is why we check several inputs. Feedback is in Portuguese.
Execute o código para ver a saída aqui.Edite o JavaScript e clique em Verificar.
Takeaway
A single passing example is not enough. Also test exact payment and decimal amounts. Your code runs in your browser; the checker does not save or submit it.