Skip to content
Read the problem

Lesson 1.4

Plan first, then code

6 min · 7 steps

  1. Step 1 of 7 · Explanation

    Write a plan in 3 to 5 plain lines before coding. Each plan line later becomes one or two lines of code.

    Problem: "Return the longest word. On a tie, return the one that comes first."

    1. Keep the first word as best.
    2. Check each word after it.
    3. If it's longer than best, replace best.
    4. Return best.

    Line 3 says "longer", not "as long or longer". That's what keeps the tie rule.

  2. Step 2 of 7 · Arrange the code

    Problem: "Return the longest word. On a tie, return the one that comes first."
    Plan: keep the first word as best, check each word after it, replace best if that word is longer, then return best.

    Build the code. Two lines don't belong.

    return bestif (words[i].length >= best.length) {}for (let i = 1; i < words.length; i++) {best = words[i]let best = words[0]}if (words[i].length > best.length) {}for (let i = 1; i <= words.length; i++) {function solution(words) {
  3. Step 3 of 7 · Predict the result

    What does it return for ["cat", "dog", "ox"]?

    function solution(words) {  let best = words[0]  for (let i = 1; i < words.length; i++) {    if (words[i].length > best.length) {      best = words[i]    }  }  return best}
    1. "cat"
    2. "dog"
    3. "ox"
  4. Step 4 of 7 · Pick the approach

    Problem: "Return the index of the first negative number, or -1 if there isn't one."
    Which plan is right?

    1. Check each number. If it's negative, return its index. After the loop ends, return -1.
    2. Check each number. If it's negative, return its index. If not, return -1.
    3. Find the smallest number, then return its index.
    4. Check each number. If nums[i] <= 0, return its index. After the loop, return -1.
  5. Step 5 of 7 · Predict the result

    Problem: "Return the index of the first negative number, or -1 if there isn't one." What does this code return for [4, -2]?

    function solution(nums) {  for (let i = 0; i < nums.length; i++) {    if (nums[i] < 0) {      return i    } else {      return -1    }  }  return -1}
    1. -1
    2. 1
    3. 0
  6. Step 6 of 7 · Write the code

    solution(words)

    Turn this plan into code. Problem: "Return how many words in words start with the letter a."

    1. count starts at 0.
    2. Check each word.
    3. If its first letter is "a", add 1.
    4. Return count.
    function solution(words) {  // ["apel", "jeruk", "anggur"] -> 2 }
  7. Step 7 of 7 · Recap

    • Write a 3 to 5 line plan before the code. One plan line, one or two lines of code.
    • Words like "longer" or "first" in the plan decide between > and >=.
    • The "not found" return goes after the loop, not in an else inside it.

    Pattern card

    Plan in plain words
    When to use it: When you don't know where to start. If you can't write the plan, you can't write the code yet either.
    // 1. mulai / start// 2. ulangi / repeat// 3. putuskan / decide// 4. return