From 148c042baf0ec8f52a13045de5750caaf1724412 Mon Sep 17 00:00:00 2001 From: Iman Feisali Date: Wed, 8 Jul 2020 16:26:41 +0100 Subject: [PATCH] mandatory homework done --- .../mandatory/1-practice/2-code-reading.md | 9 +++ .../mandatory/2-exercises/1-shopping-cart.js | 8 ++- .../mandatory/2-exercises/2-convertion.js | 9 +++ .../Homework/mandatory/2-exercises/3-atm.js | 27 +++++++- .../mandatory/2-exercises/4-music-player.js | 65 ++++++++++++------- 5 files changed, 91 insertions(+), 27 deletions(-) diff --git a/week-9/Homework/mandatory/1-practice/2-code-reading.md b/week-9/Homework/mandatory/1-practice/2-code-reading.md index 295964e8d..5b09643c5 100644 --- a/week-9/Homework/mandatory/1-practice/2-code-reading.md +++ b/week-9/Homework/mandatory/1-practice/2-code-reading.md @@ -15,6 +15,10 @@ Take a look at the following code: Explain why line 4 and line 6 output different numbers. +line 6 log first variable, the first variable is global variable and can be use evrywhere in code, +but om line 4 the second variable been logged which is local variable. +means it's just been defined inside the square bracets. + ## Question 2 Take a look at the following code: @@ -34,6 +38,9 @@ console.log(y) What will be the output of this code. Explain your answer in 50 words or less. +the first log's output is 10, it's been defined globally and used inside the function. +the second log's output is not defined because it tries to log a local variable outside the function. + ## Question 3 Take a look at the following code: @@ -61,3 +68,5 @@ console.log(y); ``` What will be the output of this code. Explain your answer in 50 words or less. +first console.log output is 9, +second console.log is an object: x: 9 diff --git a/week-9/Homework/mandatory/2-exercises/1-shopping-cart.js b/week-9/Homework/mandatory/2-exercises/1-shopping-cart.js index d58a05143..5ce3847c5 100644 --- a/week-9/Homework/mandatory/2-exercises/1-shopping-cart.js +++ b/week-9/Homework/mandatory/2-exercises/1-shopping-cart.js @@ -11,9 +11,15 @@ The output of running your code should be: class ShoppingCart { // Add your code here - + constructor() { + this.myBasket = []; + } + addItem(item) { + this.myBasket.push(item); + } cartContains() { // Use console.log() to output everything contained in your cart + console.log(this.myBasket); } } diff --git a/week-9/Homework/mandatory/2-exercises/2-convertion.js b/week-9/Homework/mandatory/2-exercises/2-convertion.js index d70ca4fcd..0a61cf57c 100644 --- a/week-9/Homework/mandatory/2-exercises/2-convertion.js +++ b/week-9/Homework/mandatory/2-exercises/2-convertion.js @@ -22,6 +22,15 @@ */ // Write your code here +class Person { + constructor(name) { + this.name = name; + } + + greeting() { + console.log("Hi! I'm " + this.name + "."); + } +} // Do not edit this section const simon = new Person("simon"); diff --git a/week-9/Homework/mandatory/2-exercises/3-atm.js b/week-9/Homework/mandatory/2-exercises/3-atm.js index 768bce40e..027ef6e49 100644 --- a/week-9/Homework/mandatory/2-exercises/3-atm.js +++ b/week-9/Homework/mandatory/2-exercises/3-atm.js @@ -12,8 +12,29 @@ */ class ATM { - // Add your code here - + // Add your code here + constructor(amount) { + this.amount = amount; + this.balance = 100; + } + + make_deposit(amount) { + this.balance += amount; + console.log(`Your New Balance Is ${this.balance}`); + } + + check_balance() { + console.log(`Your Balance Is ${this.balance}`); + } + + make_withdrawl(amount) { + if (amount > this.balance || amount < 0) { + console.log(`Sorry You Dont Have Enough Money In Your Account`); + } else { + this.balance -= amount; + console.log(`Your New Balance Is ${this.balance}`); + } + } } let atm = new ATM(); // Create the ATM @@ -22,4 +43,4 @@ atm.make_deposit(200); atm.check_balance(); atm.make_withdrawl(100); -atm.make_withdrawl(500); // Your ATM should be able to handle this scenario \ No newline at end of file +atm.make_withdrawl(500); // Your ATM should be able to handle this scenario diff --git a/week-9/Homework/mandatory/2-exercises/4-music-player.js b/week-9/Homework/mandatory/2-exercises/4-music-player.js index b820e5bf3..642093c65 100644 --- a/week-9/Homework/mandatory/2-exercises/4-music-player.js +++ b/week-9/Homework/mandatory/2-exercises/4-music-player.js @@ -18,36 +18,55 @@ This means the order the songs are played in will be random, but each song will */ - class MusicPlayer { - // Add your code here - + // Add your code here + constructor() { + this.playList = []; + this.i = 0; + } + add(song, singer) { + this.playList.push(`${song} by ${singer}`); + } + play() { + if (this.playList.length == 0) { + this.playList[this.i] = "There Is No Song To Play"; + } else { + console.log(`Currently playing: ` + this.playList[this.i]); + } + } + skip() { + this.i += 1; + if (this.i == this.playList.length) { + this.i = 0; + } else if (this.playList.length == 0) { + this.playList[this.i] = "There Is No Song To Play"; + } + console.log(`Currently playing: ` + this.playList[this.i]); + } + previous() { + this.i -= 1; + if (this.i < 0) { + this.i = this.playList.length - 1; + } else if (this.playList.length == 0) { + this.playList[this.i] = "There Is No Song To Play"; + } + console.log(`Currently playing: ` + this.playList[this.i]); + } } let myMusicPlayer = new MusicPlayer(); // Create an empty playlist -// Add some songs to your playlist -myMusicPlayer.add("Bohemian Rhapsody","Queen"); -myMusicPlayer.add("Yesterday","The Beatles"); -myMusicPlayer.add("Vogue","Madonna"); - -myMusicPlayer.play(); // Output: "Currently playing: Bohemian Rhapsody by Queen" - -myMusicPlayer.skip(); // Output: "Currently playing: Yesterday by The Beatles" - -myMusicPlayer.previous(); // Output: "Currently playing: Bohemian Rhapsody by Queen" - -myMusicPlayer.skip(); // Output: "Currently playing: Yesterday by The Beatles" - -myMusicPlayer.skip(); // Output: "Currently playing: Vogue by Madonna" - - - - - - +//Add some songs to your playlist +myMusicPlayer.add("Bohemian Rhapsody", "Queen"); +myMusicPlayer.add("Yesterday", "The Beatles"); +myMusicPlayer.add("Vogue", "Madonna"); +myMusicPlayer.play(); //Output: "Currently playing: Bohemian Rhapsody by Queen"; +myMusicPlayer.skip(); // Output: "Currently playing: Yesterday by The Beatles" +myMusicPlayer.previous(); // Output: "Currently playing: Bohemian Rhapsody by Queen" +myMusicPlayer.skip(); // Output: "Currently playing: Yesterday by The Beatles" +myMusicPlayer.skip(); // Output: "Currently playing: Vogue by Madonna"