Day 4 — Studying after a hard month.

Written by demetrisgourtzilidis | Published 2017/12/25
Tech Story Tags: javascript | p1xt-guides | data-science

TLDRvia the TL;DR App

Day 4 came up late because this was also the time that I moved into another country, home and job as well as I enrolled into my online bachelor degree. Today I finally found some time to start again my coding journey. So here it is, Day 4.

Interesting Facts:

“The JavaScript engine actually compiles the program on the fly and then immediately runs the compiled code.”

Constants:

“Another common usage of variables is for centralizing value setting. This is more typically called constants, when you declare a variable with a value and intend for that value to not change throughout the program.

const TAX_RATE = 0.08;”

Variables:

“You’ll need the keyword var in every program, as it's the primary way you declare (aka create) _var_iables. “var a;” “

Output:

“the log( b ) part is referred to as a function call. What's happening is we're handing the b variable to that function… the console. part is an object reference where the log(..) function is located.”

“Another way of creating output that you can see is to run an “alert( b );” statement. “

“using console.log(..) is generally going to make learning about coding and running your programs in the console easier than using alert(..), because you can output many values at once without interrupting the browser interface.”

Input:

“there’s an easier way to get input. Use the “age = prompt( “Please tell me your age:” );” function.”

Operators:

“Math: + (addition), - (subtraction), * (multiplication), and / (division), as in a * 3.”

“The = equals operator is used for assignment. as in a = 2

“Compound Assignment: +=, -=, *=, and /= are compound operators that combine a math operation with assignment, as in a += 2 (same as a = a + 2).”

“Increment/Decrement: ++ (increment), -- (decrement), as in a++ (similar to a = a + 1).”

“Object Property Access: . as in console.log().”

“Equality: == (loose-equals), === (strict-equals), != (loose not-equals), !== (strict not-equals), as in a == b.”

“Comparison: < (less than), > (greater than), <= (less than or loose-equals), >= (greater than or loose-equals), as in a <= b.”

“Logical: && (and), || (or), as in a || b that selects either a or b.”

“For much more detail, and coverage of operators not mentioned here, see the Mozilla Developer Network (MDN)’s “Expressions and Operators” (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators).”

Variable Types:

“When you need to do math, you want a "number”.”

“When you need to print a value on the screen, you need a "string" (one or more characters, words, sentences).”

When you need to make a decision in your program, you need a "boolean" (true or false).

Converting Between Variable Types:

var a = "42";var b = Number( a );

Commenting:

// This is a single-line comment

/* But this is a multi line comment. */

Blocks:

“in code we often need to group a series of statements together, which we often call a block. In JavaScript, a block is defined by wrapping one or more statements inside a curly-brace pair { .. }"

Conditionals (aka decisions):

if (..) { ..} elseif (..){

..

} else (..) {

..

}

Loops:

while (..) {..}

do { ..} while (..);

“We can use JavaScript’s break statement to stop a loop.

while (true) { if (..) { break; } ..}

for (initialization clause; conditional test clause; update clause) { ..}

Functions:

“Your program will almost certainly want to break up the code’s tasks into reusable pieces, instead of repeatedly repeating yourself repetitiously. The way to do this is to define a function.”

“A function is generally a named section of code that can be “called” by name, and the code inside it will be run each time. Consider:

function functionName() { ..;}

..

functionName();”

“Functions can optionally take arguments (aka parameters) — values you pass in. And they can also optionally return a value back.”

function functionName( value ) { ..;}

..

functionName( value );”

function functionName( value ) { .. return value;}

..

A = functionName( value );

Scope:

“scope (technically called lexical scope). In JavaScript, each function gets its own scope. Scope is basically a collection of variables as well as the rules for how those variables are accessed by name. Only code inside that function can access that function’s scoped variables. A variable name has to be unique within the same scope — there can’t be two different a variables sitting right next to each other. But the same variable name a could appear in different scopes. Also, a scope can be nested inside another scope.”

function outer() { ..

function inner() { ..

}

inner();

..}

outer();

Practice:

  • Write a program to calculate the total price of your phone purchase. You will keep purchasing phones (hint: loop!) until you run out of money in your bank account. You’ll also buy accessories for each phone as long as your purchase amount is below your mental spending threshold.
  • After you’ve calculated your purchase amount, add in the tax, then print out the calculated purchase amount, properly formatted.
  • Finally, check the amount against your bank account balance to see if you can afford it or not.
  • You should set up some constants for the “tax rate,” “phone price,” “accessory price,” and “spending threshold,” as well as a variable for your “bank account balance.””
  • You should define functions for calculating the tax and for formatting the price with a “$” and rounding to two decimal places.
  • Bonus Challenge: Try to incorporate input into this program, perhaps with the prompt(..) covered in "Input" earlier. You may prompt the user for their bank account balance, for example.

Have fun and be creative!

Conclusion:

This is where I, Demetris, stop. If you follow my journey considering taking the time to complete the above practice exercise before you go into the next “day”. In the beginning of the next day you will also get my own solution to this problem as well as the recommended solution.

Please do comment down below all your thoughts about my journey and If you find my posts here helpful to your own journey.

Thanks for being with me all this time.


Published by HackerNoon on 2017/12/25