← Trail· Lesson 1 of 10
See this lesson in JavaScript →
What is an algorithm
Turn a problem into clear steps and give types to the inputs and output.

An algorithm is a sequence of steps that solves a problem. This trail uses TypeScript: JavaScript with types. Types do not change what the program does; they describe what kind of value each name holds, so your editor can flag mistakes before running.
Input → processing → output
- Input: the amount paid and the price, both
number. - Processing: subtract the price from the amount paid.
- Output: the change, also a
number.
A type annotation goes after a colon: const preco: number = 12.
Try it
console.log prints to the Console panel. The playground strips the type annotations and runs the resulting JavaScript in your browser. Identifiers are in Portuguese: valorPago is amount paid, preco price, troco change.
Execute o código para ver a saída aqui.Exercise
calcularTroco(valorPago: number, preco: number): number subtracts in the wrong order. Its types say it takes two numbers and returns a number. Edit only the return line so it returns amount paid minus price.
Keep the function name and click Verificar (Check). Feedback is in Portuguese.
Execute o código para ver a saída aqui.Edite o TypeScript e clique em Verificar.
Next step
Types guarantee you passed numbers, not that the math is right. You still need tests: types and tests complement each other.