Mobile Development and JavaScript | Module 3

Course Name: Mobile Development and JavaScript

Course Link: Mobile Development and JavaScript

These are Mobile Development and JavaScript Week 3 Coursera Answers


Programming Assignment: Building a functional program

// Task 1: Build a function-based console log message generator
function consoleStyler(color, background, fontSize, txt) {
    var message = "%c" + txt;
    var style = `color: ${color};`;
    style += `background: ${background};`;
    style += `font-size: ${fontSize};`;

    console.log(message, style);
}

// Task 2: Build another console log message generator
function celebrateStyler(reason) {
    var fontStyle = "color: tomato; font-size: 50px";

    if (reason == "birthday") {
        console.log(`%cHappy birthday`, fontStyle);
    } else if (reason == "champions") {
        console.log(`%cCongrats on the title!`, fontStyle);
    } else {
        console.log(message, style);
    }
}

// Task 3: Run both the consoleStyler and the celebrateStyler functions
consoleStyler('#1d5c63', '#ede6db', '40px', 'Congrats!');
celebrateStyler('birthday');

// Task 4: Insert a congratulatory and custom message
function styleAndCelebrate(color, background, fontSize, txt, reason) {
    consoleStyler(color, background, fontSize, txt);
    celebrateStyler(reason);
}

// Call styleAndCelebrate
styleAndCelebrate('ef7c8e', 'fae8e0', '30px', 'You made it!', 'champions');

These are Mobile Development and JavaScript Week 3 Coursera Answers


Programming Assignment: Building an object-oriented program

// Task 1: Code a Person class
class Person {
    constructor(name = "Tom", age = 20, energy = 100) {
        this.name = name;
        this.age = age;
        this.energy = energy;
    }

    sleep() {
        this.energy += 10;
    }

    doSomethingFun() {
        this.energy -= 10;
    }
}

// Task 2: Code a Worker class
class Worker extends Person {
    constructor(name = "Tom", age = 20, energy = 100, xp = 0, hourlyWage = 10) {
        super(name, age, energy);
        this.xp = xp;
        this.hourlyWage = hourlyWage;
    }

    goToWork() {
        this.xp += 10;
    }
}

// Task 3: Code an intern object, run methods
function intern() {
    const intern = new Worker("Bob", 21, 110, 0, 10);
    intern.goToWork();
    return intern;
}

// Task 4: Code a manager object, run methods
function manager() {
    const manager = new Worker("Alice", 30, 120, 100, 30);
    manager.doSomethingFun();
    return manager;
}

console.log(intern());
console.log(manager());

These are Mobile Development and JavaScript Week 3 Coursera Answers


Programming Assignment: Array and object iteration

// Task 1
var dairy = ['cheese', 'sour cream', 'milk', 'yogurt', 'ice cream', 'milkshake'];

function logDairy() {
    for (let item of dairy) {
        console.log(item);
    }
}

logDairy();

// Task 2
const animal = {
    canJump: true
};

const bird = Object.create(animal);
bird.canFly = true;
bird.hasFeathers = true;

function birdCan() {
    for (let key in bird) {
        if (bird.hasOwnProperty(key)) {
            console.log(`${key}: ${bird[key]}`);
        }
    }
}

birdCan();

// Task 3
function animalCan() {
    for (let key in bird) {
        if (bird.hasOwnProperty(key)) {
            console.log(`${key}: ${bird[key]}`);
        }
    }
    for (let key in animal) {
        if (animal.hasOwnProperty(key)) {
            console.log(`${key}: ${animal[key]}`);
        }
    }
}

animalCan();

These are Mobile Development and JavaScript Week 3 Coursera Answers

See also  Mobile Development and JavaScript | Module 4

Module 3 Quiz: Programming Paradigms

Q1. Which of the following variable declarations cannot be redeclared but can be reassigned?

  • let
  • var
  • const
  • There are no variable declarations which cannot be redeclared but can be reassigned.

Answer: let


Q2. Which of the following variable declarations can be redeclared and reassigned?

  • let
  • const
  • There are no variable declarations which can be redeclared and reassigned.
  • var

Answer: var


Q3. What will print out when the following code runs?
function scopeTest() { var y = 44; console.log(x); }
var x = 33;
scopeTest();

  • 33
  • 44
  • undefined
  • null
Answer: 33

These are Mobile Development and JavaScript Week 3 Coursera Answers


Q4. What will print out when the following code runs?
function scopeTest() { var y = 44; console.log(x); }
scopeTest();
var x = 33;

  • 44
  • undefined
  • 33
  • null
Answer: undefined

Q5. What is the function of extends in the following code block.

class Cake {
     constructor(lyr) { this.layers = lyr; 
     } 
getLayers() { return this.layers; } 
class WeddingCake extends Cake {
     constructor() { super(2); } getLayers() { return super.getLayers() * 5; } 
     } 
var result = new WeddingCake(); 
console.log(result.getLayers());
  • Replaces the class Cake with the class WeddingCake.
  • Creates a child of the class Cake.
  • Creates a parent of the class Cake.
  • Replaces the class WeddingCake with the class Cake.
Answer: Creates a child of the class Cake.

Q6. Which of the following statements describes the outcome of the code block below?

class Animal { } 
class Dog extends Animal {
     constructor() { this.noise = "bark"; } 
     makeNoise() { return this.noise; } 
     } 
class Wolf extends Dog {
     constructor() { super(); this.noise = "growl"; } 
     } 
var result = new Wolf(); 
console.log(result.makeNoise());
  • syntaxError
  • console.log(result.makeNoise()) will log undefined
  • growl is replaced by bark as the output of console.log(result.makeNoise())
  • bark is replaced by growl as the output of console.log(result.makeNoise())
Answer: growl is replaced by bark as the output of console.log(result.makeNoise())

These are Mobile Development and JavaScript Week 3 Coursera Answers

See also  Mobile Development and JavaScript | Module 1

Q7. What is the function of extends in the following code block.

class Animal { } 
class Dog extends Animal {
     constructor() { this.noise = "bark"; } 
     makeNoise() { return this.noise; } 
     } 
class Wolf extends Dog {
     constructor() { super(); this.noise = "growl"; } 
     } 
var result = new Wolf(); 
console.log(result.makeNoise());
  • Replaces the class Animal with the class Dog.
  • Creates a parent of the class Animal.
  • Creates a child Dog of the class Animal and a child Wolf of the class Dog.
  • Replaces the class Dog with the class Wolf.
Answer: Creates a child Dog of the class Animal and a child Wolf of the class Dog.

Q8. Which of the following snippets would output [3,4] to the console from the snippet:
let a, b, rest;
[a, b, …rest] = [1,2,3,4]

  • console.log(c+d)
  • console.log(…rest)
  • console.log(c,d)
  • console.log(rest)
Answer: console.log(rest)

Q9. What is the value of b in the following code snippet?
const [a, b] = [1,2,3,4]

  • 1
  • 2
  • [1,2,3,4]
  • undefined
Answer: 2

These are Mobile Development and JavaScript Week 3 Coursera Answers


Q10. What value will be printed out when the following code runs?
function count(food) { console.log(food.length) }
count(“Burgers”, “Fries”, null);

  • 3
  • “Burgers”, “Fries”, null
  • “Burgers”, “Fries”, undefined
  • 7
Answer: 7

Q10. What value will be printed out when the following code runs?
function count(…food) { console.log(food.length) } count(“Burgers”, “Fries”, null);

  • 2
  • 3
  • “Burgers”, “Fries”, undefined
  • “Burgers”, “Fries”, null
Answer: 3

Q12. Which of the following are JavaScript methods for querying the Document Object Model?

  • getElementsByClassName
  • getElementsById
  • getElementByIds
  • getElementByClassName
Answer: getElementsByClassName

These are Mobile Development and JavaScript Week 3 Coursera Answers


Q13. You can access the Document Object Model from what part of your web browser?

  • Console tab
  • Elements tab
  • You cannot access the Document Object Model from your web browser.
  • Sources tab
Answer: Console tab

Q14. JSON.parse is used to do which of the following?

  • Convert a JSON string to a JavaScript object.
  • Convert a JavaScript object to a JSON number.
  • Convert a JavaScript object to a JSON boolean.
  • Convert a JavaScript object to a JSON string.
Answer: Convert a JSON string to a JavaScript object., Convert a JavaScript object to a JSON string.

Q15. What will be the result of running this code?
const letter = “a” letter = “b”;
console.log(letter);

  • Uncaught SyntaxError: Invalid or unexpected token
  • a
  • b
  • Uncaught TypeError: Assignment to constant variable
Answer: Uncaught TypeError: Assignment to constant variable

These are Mobile Development and JavaScript Week 3 Coursera Answers

See also  Mobile Development and JavaScript | Module 2

Q16. A function that is called to create an instance of an object is called what?

  • Argument
  • Method
  • An object literal
  • Constructor
Answer: Constructor

These are Mobile Development and JavaScript Week 3 Coursera Answers


Q17. What is the keyword for a constructor function which details how an object will be built?

  • Construct
  • Extends
  • Function
  • New
Answer: New

These are Mobile Development and JavaScript Week 3 Coursera Answers


More Weeks of this course: Click Here

More Coursera Courses: http://progiez.com/coursera


Mobile Development and JavaScript Week 3 Coursera