---
# System prepended metadata

title: Break it down
tags: [ACC, JS, Lesson Notes]

---

###### tags: `Lesson Notes` `JS` `ACC`

# Break it down
Word problems are hard.  What is it asking for, what is information that I don't need?  We all ask these questions. So lets break some down

## #1
Create a function that prints the even numbers to an array and odd numbers to another array, from 0 up to and including 10.  Print the array at each step and then the final answer.

### Lets Pseudocode this:
1. It's asking us to create a function
2. Goes through 0-10
3. Prints the even numbers to 1 array
4. Prints the odd numbers to another array
5. Asks us to print each array at each step and then the final arrays

### Building the function with comments:
Step #1 build the basics
```
function evenOdd() {  // starting the funciton by giving it a name
    var even = [] //creating the even array
    var odd = [] // creating the odd array
    for()  // our for loop
    if(){  // 1st if statement
        
        console.log() // since it wants it to print every time through we need this here
    } else { // our second or in this case if 1st is false do this statement
        
        console.log() // Again it wants us to print every time through so we need this.
    }
    console.log(even) // Printing final results of the even array
    console.log(odd) // Printing final results of the odd array
}
evenOdd() // How we will activate the function
```
### Step #2 Fill in the rest of the instructions
```
function evenOdd(){
    var even = []
    var odd = []
    for(let i = 0; i <=10; i++) {
        if (i % 2 == 0) {
            even.push(i)
            console.log(even)
        } else {
            odd.push(i)
            console.log(odd)
        }
    }
    console.log("Final even results", even)
    console.log("Final odd results", odd)
}
evenOdd()
```
### Common questions:
1. Where to place the console.logs.  
    a. Well that all depends on when in the running of the function you want to see something.  
    b. So the 1st console log is going to run every time the if statement is true...which according  to our directions is what was wanted
    c. The second console log is going to run everytime the else statement is true..again is what we want.
    d. So how about the last 2 console logs.   Well we want those to be triggered when our for loop is finished.  So if we follow the brackets they need to be after that for loop is closed.  
2. Why can't I have the last 2 console logs after we start the function like after the evenOdd()? 
    a. The var even and var odd are not global variables.  so we will get an undefined error.  If we put those 2 var's outside of the function then we could put the console logs where they are now or outside the function
    

See the visual of this function [here (part of pythontutor.com's site)](http://pythontutor.com/javascript.html#code=function%20ifLoop%28%29%20%7B%0A%20%20var%20even%20%3D%20%5B%5D%0A%20%20var%20odd%20%3D%20%5B%5D%0A%20%20for%20%28let%20i%20%3D%200%3B%20i%20%3C%3D10%3B%20i%2B%2B%29%7B%0A%20%20%20%20if%20%28i%20%25%202%20%3D%3D%200%29%7B%0A%20%20%20%20%20%20even.push%28i%29%0A%20%20%20%20%20%20console.log%28even%29%0A%20%20%20%20%20%20%7D%20else%20%7B%0A%20%20%20%20%20%20%20%20odd.push%28i%29%0A%20%20%20%20%20%20%20%20console.log%28odd%29%0A%20%20%20%20%20%20%7D%0A%20%20%7D%0A%20%20%20%20console.log%28%22Final%20even%20results%22,%20even%29%0A%20%20%20%20console.log%28%22Final%20odd%20results%22,%20odd%29%0A%7D%0AifLoop%28%29&curInstr=0&mode=display&origin=opt-frontend.js&py=js&rawInputLstJSON=%5B%5D)
