# [4] Non-top down (only way I can think of summing up loops and if-else)

Today jumped into a whole new section of JS, this idea of logic inside of the code. Now every line is not hit, or some lines are hit over and over, interesting…

DAY 4> awesome> platforms> node.js> resources> tutorials> Nodeschool.io> javascripting> Rounding numbers, numbers to strings, if statements & for loops

Today was a big whole list of ideas learned, starting with this idea of rounding numbers, Math.round(7.6) (returns 8) does the trick. It rounds whatever number is put in. I could see this helping with estimations or equations of the sort, but I don’t find it to difficult of a topic so I will leave rounding there.

Next was altering the type of data that was given. Specifically going from a number to the text version of that number, so 6 to “6”. So 6+6 would equal 12, but from my understanding, “6”+”6" would equal “66” because the strings are being pushed together to become one. So if you want a string of a number, you use .toString(), before the dot you have a variable that is a number.

var n = 10;
n = n.toString();
console.log(n) <- prints ‘10’ as a string

Going into JS I didn’t really think about the idea of going from one type of data to another, but it is nice knowing you can now. Don’t know how to go backwards but it will come with time, or a Google search.

A little further down the rabbit hole, we hit if-else statements, a seemingly very important piece of JavaScript. They allow for you to execute one block of code in a specific scenario, or another if the first scenario does not apply. They look like this

var x = 10;
if (x>5) {
console.log(‘x is greater than 5’); <- this one is printed
}
else {
console.log(‘x is less than or equal to 5’);
}

What is happening here is that the top curly braced code runs if x is greater than 5, and the bottom one runs if that is not the case. If one thing, do this thing, else, do this other thing. It allows for the writer to block off specific sections for times when they are needed.

I picture this as a fundamental aspect of JavaScript, because without it, every bit of code will be run every time, and sometimes that just isn’t supposed to happen.

The last area covered today was this idea of looping. In JS, if you want code to run more than once, you use a for loop, they look like this

for(var i = 0;i<10;i++){
console.log(‘bees’); <- will be printed 10 times
}

This little guy has some odd syntax so I am going to break it down. First you start with the word ‘for’, marking that this is, in fact, a for loop.

Then in parenthesis, you have to do 3 things, first, set a variable to loop through, I guess the basic name for this is ‘i’. This is a variable set to a number.

Second, you set the time you would like for the loop to end, here we want the loop to end when i is not less than 10 any more, because i starts at 0, we know this loop will run at least once.

Lastly, we need to make sure i goes up every time the loop is run, so “i++” makes i go up one every time the loop runs, allowing the loop not to run for infinity. My loop prints the word ‘bees’ 10 times, once for each time the loop goes.

Well, a big step forward today, much learned, but almost done with this section, I think it may take me two more days to complete the basic JS section, movin on up in the world.