Skip to content
Read the problem

Lesson 1.2

Say it in your own words

6 min · 7 steps

  1. Step 1 of 7 · Explanation

    Before coding, restate the problem in one sentence: what goes in, what comes out, what the rule is.

    Problem: "Return how many days were warmer than the day before."
    Your version: "Count each i where temps[i] > temps[i - 1], starting at i = 1."

    The second sentence is almost code already. Then work one example by hand. For [20, 22, 21, 25], 22 is warmer than 20 and 25 is warmer than 21, so the answer is 2.

  2. Step 2 of 7 · Pick the approach

    Problem: "Return how many days were warmer than the day before."
    Which restatement is right?

    1. Count each i with temps[i] > temps[i - 1], starting at i = 1
    2. Count the days warmer than the first day
    3. Count each i with temps[i] > temps[i - 1], starting at i = 0
    4. Return the biggest rise in temperature
  3. Step 3 of 7 · Predict the result

    Work it by hand. What does it return for [20, 22, 22, 21, 25]?

    function solution(temps) {  let count = 0  for (let i = 1; i < temps.length; i++) {    if (temps[i] > temps[i - 1]) count++  }  return count}
    1. 2
    2. 3
    3. 4
  4. Step 4 of 7 · Trace the code

    For temps = [5, 8, 3], fill in i and count at each marked step.

    function solution(temps) {  let count = 0  for (let i = 1; i < temps.length; i++) {    if (temps[i] > temps[i - 1]) count++  }  return count}

    solution([5, 8, 3])

  5. Step 5 of 7 · Explanation

    The examples in the problem are rarely enough. Make your own small example, then write the output you expect before running the code.

    The most useful one is usually the smallest that still makes sense: one item, or two equal items.

  6. Step 6 of 7 · Predict the result

    Your own example, just one day. What does it return for [7]?

    function solution(temps) {  let count = 0  for (let i = 1; i < temps.length; i++) {    if (temps[i] > temps[i - 1]) count++  }  return count}
    1. 0
    2. 1
    3. 7
  7. Step 7 of 7 · Recap

    • Restate the problem in one sentence with the input, the rule, and the output.
    • Work the examples by hand. Write down every pair being compared.
    • Make your own small example and write its result before running the code.

    Pattern card

    Sentence first, then code
    When to use it: Every time you read a new problem. If the sentence isn't clear, the code won't be either.