---
# System prepended metadata

title: Algo App challenges
tags: [ACC, Algo App, JS, Lesson Notes]

---

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

# Algo App challenges


## Links:
[GitHub Repo](https://github.com/Coding-Dojo-ACC/week02-introToPrograming) w/ Solutions
[ACC Main Notebook](/cu_zBn2ST2aVMXp560fjZw)
[HomeWork: ](http://learn.codingdojo.com/m/207/5594/36677)Git

## Due by Wednesday:
1. Algo app lessons 1-7 should already be done if not work on finishing up
2. The Challenges Section of the Algo app
3. Read through and work on assignments in Git

Feel free to reset the Algo app as many times as you want.  Take those lessons and type out those functions into a .js file and write it out line by line what it is doing.  Have a statement of what the overall function is doing as well. 

## Challenges

1. What is the purpose of the function
2. What will we need to have in the function
3. Return the result of the function

These should be the basic steps that you take with all functions you will create.  
 
 ### Challenge #1:
 Write a function that returns the sum of all odd numbers from 1 - 5000
 
 #### Lets break it down to our 3 steps above
 
 1. The purpose of our function is to add all the odd numbers from 1 - 5000
 2. What will we need:
     a. We will need to go through all the numbers between 1 and 5000 so we will need a for loop
     b. We will need to only add together the odd numbers so we will need an if statement to determin if it is odd or even.
     c. A way to add each odd number to the sum of the last loop through
 3. At the end we should just return the final sum.


#### 3 parts to a for loop
1. i = 1; => This is telling the loop where it needs to start. in this case we want it to start at 1
2. i = <= 5000; => This is telling the loop when to stop in this case we want it to stop when i is no longer less than or equal to 5000
3. i++ => This is saying add 1 after each loop through, basically telling us keep going even if the following statements are true or result in a return untill step 2 is reached.

In VS Code we came to the following solution:
```
function sum_add_5000(){
    var sum = 0;
    for (var i = 1; i <= 5000; i++) {
        if (i % 2 == 1) {
            sum = sum + i
        }
    }
    return sum;
}
console.log(sum_add_5000())
```
When we ran Code Runner our answer was 650000
Lets break down the function:

1. Create a function, call it sum_add_5000 and give it the following instructions
2. Create a new var call it sum and make it equal to 0
3. Tell it how to go through the parameters we will set by saying that i or the 1st loop through 1 is = 1 becasue thats is where we want to start; change i each loop till i is no longer less than or equal to 5000; and repeat this loop by adding 1 to i each time
4. On the 1st loop through (and each following) if i divided by 2 gives a remainder of 1 then do the following command (As there is no else statemnt it will just move on out of the if statement and on to the return and then continue the loop)
5. Our if statement was true so we need to take the current sum (1st loop through is 0) and then set it equal to current sum + what ever i is.  So 1st loop through it would look like this 0 = 0+1
6. Then we will return what sum is 1st loop through sum is now 1
7. Once we have satisfied the for loop and have a final return statement we will then log it out.  Having sum_add_5000() inside the console.log means that we are telling the console log what to actualy do run the function and print the return.  

So next loop through our if statement would be false as 2 does not produce a remainder of 1 so we go right to return sum which as we didn't execute the instructions in the if statement means sum is still 1

3rd loop through our if statement would be true so we will move on to those instructions.  sum = sum + i.  Since sum is currently 1 it will look like this 1 = 1+3 and return our new sum = 4

Next time our if statement is true would be 5 so it would then look like this 4 = 4+5 so our new sum is 9

## Conclusion
As you can see breaking down the code into pseudoCode takes a lot of lines compaired to the code its self but if you understand what it is doing then you can make sure that it is written in a way that will return the results you want.  

On the Repository there are more notes for the other challenges.  (or will be if you read this before I add them back in)

Feel free to leave a comment if you have questions. 

#### Back to [ACC Main Notebook](/cu_zBn2ST2aVMXp560fjZw?view)
