Algo App challenges
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
- What is the purpose of the function
- What will we need to have in the function
- 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
- The purpose of our function is to add all the odd numbers from 1 - 5000
- 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
- At the end we should just return the final sum.
3 parts to a for loop
- i = 1; => This is telling the loop where it needs to start. in this case we want it to start at 1
- 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
- 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:
When we ran Code Runner our answer was 650000
Lets break down the function:
- Create a function, call it sum_add_5000 and give it the following instructions
- Create a new var call it sum and make it equal to 0
- 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
- 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)
- 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
- Then we will return what sum is 1st loop through sum is now 1
- 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.