davidteren
8/29/2018 - 8:52 AM

04-Functions & Objects

function doSomeMath() {
	var a = 5;
	var b = 4;
	var sum = a + b;

	return sum;
}

var theResult = doSomeMath();

console.log("The result: ", theResult);
 
 // Dot Notation
object.someProperty

// Bracket Notation
object["someProperty"]

// Bracket Notation is useful when you have a property title like BG:image

object["BG:image"] 
function Course(title, instructor, level, published, views) {
    this.title = title;
    this.instructor = instructor;
    this.level = level;
    this.published = published;
    this.views = views;
    this.updateViews = function () {
        return ++this.views;
    }

}

var courses = [
    new Course("Javascript Essential Training", "Morten Rand-Hendriksen", 1, true, 0 ),
    new Course("Up and Running with ECMAScript 6", "Eve Porcello", 1, true, 123456 )
]


// var course01 =  new Course("Javascript Essential Training", "Morten Rand-Hendriksen", 1, true, 0 )
// var course02 =  new Course("Up and Running with ECMAScript 6", "eve Porcello", 1, true, 123456 )

console.log(courses[1].updateViews())


window.localStorage
  var course = new Object();


var course = {
    title: "JavaScript Essential Training",
    instructor: "Morten Rand-Hendriksen",
    level: 1,
    published: true,
    views: 0,
    updateViews: function() {
        return ++course.views;
    }
}


console.log(course);

function logScope() {
    let localVar = 2;

    if (localVar) {
        let localVar = "I'm different";
        console.log("nested localVar", localVar);
    }

    console.log("logScope local: ", localVar);

}


logScope();
function findBiggestFraction(a,b) {
    console.log("Fraction a: ", firstFraction);
    console.log("Fraction b: ", secondFraction);

    var result;

	a>b ? result = ["a",a] : result = ["b",b];
    return result;
}

var firstFraction = 7/16;
var secondFraction = 13/25;

var fractionResult = findBiggestFraction(firstFraction,secondFraction);
console.log("Fraction " + fractionResult[0] + " with a value of " + fractionResult[1] + " is the biggest.");
var theBiggest = function(a,b) {
    var result;
    a>b ? result = ["a", a] : result = ["b", b];
    return result;
}

console.log(theBiggest(7/9,13/25));
var a = 3/4;
var b = 5/7;

var theBiggest = function(a,b) {
    var result;
    a>b ? result = ["a", a] : result = ["b", b];
    return result;
}

console.log(theBiggest(7/9, 13/25));
function findBiggestFraction(a, b) {
    var result;
    a > b ? result = ["firstFraction", a] : result = ["secondFraction", b];
    return result;
}

var firstFraction = 3 / 4;
var secondFraction = 5 / 7;

var fractionResult = findBiggestFraction(firstFraction, secondFraction);

console.log("First fraction result: ", firstFraction);
console.log("Second fraction result: ", secondFraction);

console.log("Fraction: " + fractionResult[0] + " with a value of " + fractionResult[1] + " is the biggest!");
function findBiggestFraction(a, b) {
    a>b ? console.log("a: ", a) : console.log("b: ", b);
}

var firstFraction = 3/4;
var secondFraction = 5/7;

findBiggestFraction(firstFraction, secondFraction);
findBiggestFraction(7/16, 13/25);
findBiggestFraction(1/2, 3/4);
function findBiggestFraction() {
    a>b ? console.log("a: ", a ) : console.log("b: ", b)
}

var a = 3/4;
var b = 5/7;

findBiggestFraction();
// Regular function, called explicitly by name:
function multiply(x, y) {
    var result = x * y;
    console.log("3 multiplied by 4 is ", result);
}
multiply();

// Anonymous function stored in variable.
// Invoked by calling the variable as a function:
var divided = function() {
    var result = 3 / 4;
    console.log("3 divided by 4 is ", result);
}
divided();

// Immediately Invoked Function Expression.
// Runs as soon as the browser finds it:
(function() {
    var result = 12 / 0.75;
    console.log("12 divided by 0.75 is ", result);
}())