PProtokol
Algorithms & Data Structures/Thinking Algorithmically
outline — lesson 1 of 10

Level 1 · Complete beginner · 12 min

Thinking Algorithmically

Learn to think in clear steps a computer can follow, before writing any code.

What you will learn

  • Define what an algorithm is
  • Break a real-world task into precise steps
  • Recognize inputs, outputs, and edge cases in a problem
  • Translate a step-by-step plan into a small JavaScript function

The idea

An algorithm is just a precise, ordered list of steps for solving a problem. Before you write any code, you should be able to describe your solution in plain steps that a person -- or a machine -- could follow exactly.

Why does this matter?

Jumping straight to code without a plan leads to bugs and wasted time. Thinking algorithmically first means you separate deciding WHAT to do from figuring out HOW to write it in JavaScript.

A real-world analogy

A recipe is an algorithm: precise steps (crack two eggs, whisk, heat pan) in a specific order, using given ingredients (inputs) to produce a dish (output). Vague recipes like "cook until nice" are bad algorithms -- so are vague code plans.

See it in code

From plain steps to code
// Problem: find the largest number in a list
// Steps:
// 1. Assume the first number is the largest so far
// 2. Look at each remaining number
// 3. If it is bigger than our current largest, update it
// 4. Return the largest found

function findLargest(numbers) {
  let largest = numbers[0];
  for (let i = 1; i < numbers.length; i++) {
    if (numbers[i] > largest) {
      largest = numbers[i];
    }
  }
  return largest;
}

console.log(findLargest([3, 7, 2, 9, 4]));

Line by line

  • let largest = numbers[0];

    We start with a reasonable guess: the first item. This matches step 1 of our plan exactly.

  • for (let i = 1; i < numbers.length; i++) {

    We start at index 1 since index 0 is already our starting guess -- this is step 2.

  • if (numbers[i] > largest) { largest = numbers[i]; }

    Step 3 in code: compare and update only when we find something bigger.

  • return largest;

    Step 4: hand back the final answer once the loop finishes.

What do you think happens?

What would findLargest([5]) (a single-item array) return?

Have a guess before you read on. Guessing wrong is part of learning it.

Try it yourself

Write plain-English steps (not code) for an algorithm that finds the SMALLEST number in a list. Then write the JavaScript function findSmallest(numbers) implementing your steps.

Worth knowing

Common mistake: writing code before you have a clear plan, then getting stuck on "how do I even start". Always ask: what are my inputs, what is my output, and what are the exact steps in between -- including edge cases like an empty list.

The proper words for it

Algorithm
A precise, ordered sequence of steps that solves a problem.
Input
The data an algorithm receives to work with.
Edge case
An unusual or extreme input, like an empty list, that a good algorithm still handles correctly.

Where you'll meet this

GPS apps, search engines, and recommendation feeds are all algorithms: precise steps that take your input (location, search terms, viewing history) and produce an output (a route, results, suggestions).

Lesson recap

  • An algorithm is a precise, ordered list of steps
  • Plan in plain English before writing code
  • Always identify inputs, outputs, and edge cases
  • A clear plan translates almost directly into code

Still fuzzy on any of this?

That's normal, and it's not a dead end. Pick a different way to hear it.

Quick check

1. What best describes an algorithm?

2. Why plan an algorithm in plain English before coding?

3. What is an \"edge case\"?

mini challenge · javascriptnot run

Range Sum Planner

Write a function sumInRange(numbers, low, high) that returns the sum of every number in the array that is between low and high, inclusive. Plan your steps before coding.

main.jsjavascriptnot run
First, pass the quick check and pass every required coding challenge to unlock "Arrays and Lists as Building Blocks".

Your tutor

patient by design

No question is too basic here. Ask anything — I'll explain it in plain words and guide you rather than handing over answers.