Lambda School Pre-course 8: Learn to use Math, Properties, Methods and Global Objects in JavaScript

There are nine units in Lambda School’s Full Stack Web Development Pre-course. I’m going to summarize what the pre-course covers for each of them. This material is basically ground-floor information that students need to know before they start the full course.

precourse8

Learn to use Math, Properties, Methods and Global Objects in JavaScript

All of my adult life, and most of my younger life, I’ve heard that you need to study math to be a programmer. I’ve worked with developers for years who are wizards at math, but I’m not particularly adept, although I’m not incompetent either. This course might begin to show me how necessary math is to programming, and if it is – what types of math and to what areas of programming.

At work, we do use math in our software for performing clinical calculations. For example, there are several standards for determining percentage of weight change (gain or loss) and related to that are calculations for caloric intake and calories burned that are part of our nutritional assessment, for dieticians. This has modifiers for if the patient has certain conditions, like recovering from various injuries (say, skeletal trauma has a particular weight that modifies the total score), activity factor and other things. Data like this then computes things like maintenance calories (how many calories a patient needs each day to stay at their current weight), estimated protein needs, estimated fluid needs, and so on.

Nurses have their own math-related concerns that they track with our software, often related to pressure ulcers or other wounds, patterns of oral consumption – which can affect weight (a big concern for both nursing and dietary), and formulas related to ADL’s (Activities of Daily Living) which can impact how much money a nursing home is paid for support, per resident. The finance side has a TON of math which ties in to every clinical department’s work – social work’s psychological assessments, therapeutic activities’ work with the resident, and so on. There are a lot of modifiers to consider when viewing a patient’s entire situation and the impact it has on revenue for a facility as well as tracking changes when moving to and from hospitals and other nursing homes.

With that out of the way, JavaScript, like all other programming languages that I’m aware of, has the ability to solve math problems. Solving math is actually one of the core functions of a computer. I know that numbers control everything on a screen, to some extent. The pixels we look at are on a 2-dimensional grid. 3D graphics require a bit of processing and there are definitely equations running in the background determining how these moving 3-dimensional objects maintain form and motion – which then adds a physics engine on top of that, which is powered by more math, and so on.

As someone who hasn’t programmed on that level, I’ve never had to deal with any of this at work. Aside from using numbers to add padding, space, control font sizes, and that sort of thing to various sections of our application’s interface, I can’t say I’ve thought about anything even as basic as a Pythagorean Theorem since high school. I’ve researched formulas for assessment sections that need them, but all of this is math that was done by someone else that I helped determine needed to be incorporated into our program. The development team took it to the next level and actually… did the math and made it work successfully. Maybe this course will change my perception about how heavily math is needed for programming. Right now, I think that acting on the value of a piece of data, and on user input, is more important.

Math Operators

JavaScript has operators that perform the 4 basic math functions that we all had to learn as children: addition, subtraction, multiplication and division. There’s a 5th basic function as well now, called modulo. Its just the remainder, left over from doing division – so the modulo of 5/2 is 1, because 2 into 5 is 2, with 1 left over. The keyboard characters that we use for basic math are as follows:

+ for addition: 1 + 1 = 2
- for subtraction: 3 - 2 = 1
* for multiplication: 3 * 2 = 6
/ for division, and: 10 / 2 = 5
% for modulo: 9 % 3 = 0

Properties and Methods

As per the Lambda School text:

Primitive data types (and other data types) have built-in functionality known as properties and methods. These extend the functionality of the primitive data types and allow us to gather information about them, or manipulate them in some way. Both properties and methods will be accessed using the dot notation where we give the name of the variable, a dot, then the name of the property or method.

variableName.propertyName;

Properties

Properties are essentially metadata about a variable – things like the length (number of characters) in a string, for example. According to w3schools, its possible to add custom properties to a variable. For example, a variable called electricBass could have properties like brand, stringNum, handedness, and so on.

In accordance with the dot notation described in the Lambda School text, those properties would be accessed by the following names:

electricBass.brand
electricBass.stringNum
electricBass.handedness

Going along with the bass analogy, some possible values for brand could be “Fender”, “Gibson”, “Schecter” or “Ibanez”. Values for stringNum could include 4, 5, 6, 7 or 8 (yes, I know Jaco only needed 4 strings). Values for handedness could be “left” or “right”.

The example below gives us the length of a string, or how many characters it contains (spaces count).

const favoriteSteak = 'ribeye';
favoriteSteak.length; //6

Methods

A method is essentially a function or action that can be embedded or placed inside of a variable (or other object, apparently). Methods allow customized actions to be performed on specific variables or data types. They’re called like properties, but with an additional set of parenthesis at the end. For example:

var dialogue = "Hello! My name is Inigo Montoya. You killed my father... 
  prepare to die!";
var x = message.toUpperCase();

The above would return:

HELLO! MY NAME IS INIGO MONTOYA. YOU KILLED MY FATHER... 
PREPARE TO DIE!

Global objects and methods

As per the Lambda School text:

JavaScript has a number of built-in objects for us to use. These global objects extend the functionality of the language for us for free. We have already seen, and have been using, the console object and its method log. Another one of these objects is Math. Math has a number of methods on it just like console has log. To add to this, some of our data types also have built-in methods.

Math.pow

The pow method in Math raises a number to its exponent. It uses two numbers to do this – the base number and the power to raise it to (square, cube, etc.). It looks like this:

Math.pow(2,2) = 4;
Math.pow(3,2) = 9;
Math.pow(3,3) = 27;

Rounding decimals in JavaScript

The Math object has many methods in it. Some of these are used for rounding numbers. They are Math.round, Math.floor and Math.ceil and will round decimal numbers to the nearest whole number.

  • Math.round works in the manner that most people are familiar with – if a decimal is .50 or higher, it rounds up to the next whole number. If its .49 or lower, it rounds down.
  • Math.floor always rounds down to the nearest whole number, so 99.99 would round to 99, not 100.
  • Math.ceil (or ceiling) does the opposite of .floor. It rounds up to the nearest whole number, regardless of the value of the decimal (as long as its not .00). So, 9.01 would round up to 10.
Math.round(6.5) = 7;
Math.round(6.45) = 6;
Math.floor(6.999) = 6;
Math.ceil(6.0001) = 7;

6 thoughts on “Lambda School Pre-course 8: Learn to use Math, Properties, Methods and Global Objects in JavaScript

  1. Although I am pretty competent in HTML and CSS, JavaScript is something that never seems to click completely for me. Partly I think it’s because I’ve worked with programming languages that I find much simpler to use, and so when I have to start using JavaScript it’s kind of jarring. I also had this bad experience of this one JavaScript course I took on edx which for some inane reason focused on making different colored balls dance around the screen in different ways. There was probably more to the course than that, but it felt like that consumed most of the time. And I just wanted to learn basic JavaScript for web development!

    I do like reading about how Lambda School lays out their pre-course. In general I like reading about how different schools teach web development concepts. I love learning, and I think maybe I’m always just looking for that little section that I may have previously missed. I’ve learned new things before about concepts I thought I knew very well, just because a certain course taught them in a different way.

    Liked by 1 person

    1. I’m glad that you’re enjoying some of the Lambda posts. I hope I can successfully complete the challenge and actually get into the main course before Thursday, which is the deadline! Even of I don’t make it though, I still have to write-up the rest of the pre-course for the blog.

      I’m completely new to HTML, CSS and JS. I haven’t programmed in more than 20 years, and back then what I used most was Pascal. JS feels… weird to me. Its definitely not as structured as Pascal. I’m starting to get used to its conventions, but I need to make a cheat sheet to reference so that I can internalize its syntax more quickly. Once I pass the challenge, I actually plan on hitting FreeCodeCamp, which I know you recommended. I want to learn as much as I can before Lambda starts on August 5th.

      I also tend to read about concepts that I already know – not just in programming, but in general, and pick up new perspectives about them because of it. It bores my wife a bit, but its fascinating for me. ;)

      Liked by 1 person

      1. I’m sure you’ll do it in time. I think HTML and CSS are easy to become competent at even if you’re new to them, though CSS can be deceptively difficult to get really good at. I don’t know that JS is meant to be hard, I just hate it. It’s so messy. It feels like I always have this sea of closing brackets whenever I write anything. I found a bracket colorizer extension for VS Code JUST for writing JS because I was getting too confused.

        And yeah, I think FreeCodeCamp is a great choice. I’m starting the JavaScript section over to try and focus on it properly. Hopefully this time it sinks in.

        I read about stuff I already know when learning languages too – I’ve been trying to learn Spanish and Dutch – and my husband is like, ‘don’t you already know that’? I try to explain but he just doesn’t get it :p

        Like

      2. If I can’t pass the challenge by Thursday, I’ll try again for the September cohort. Its demoralizing, but I can use the time to get a better base understanding of JS, at the very least.

        I understand what you’re saying about the sea of closing brackets. I’m not in love with them either, but its more for aesthetic reasons that anything. I’ve been coding the way that the examples do so far, but I think that once the real class begins, if I get into it, I’ll probably go back to how I used to with Pascal – I commented everything so I knew what it paired with and what functions did and other things. I was always explicit in my commenting.

        Regarding your husband, some people do assume a certain level of proficiency with certain skills, and once they see that level demonstrated, they don’t necessarily think that it needs to be improved or refined unless there’s a specific reason to do so. He might think that your knowledge of Dutch and Spanish are good enough and don’t need to be advanced unless you plan on visiting places that a high level of understanding of those is important. He might not be aware of the enjoyment you get out of refining what you know. But then again, I think everyone has areas that they’re good at, which they level up more – sometimes without even consciously doing so. He might have interests like this that get his attention but just not make the same correlation with yours, or maybe he values them differently.

        Liked by 1 person

Leave a comment