Skip to content
This repository has been archived by the owner on Oct 26, 2020. It is now read-only.

Iman week 9 #1023

Open
wants to merge 1 commit into
base: manchester3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions week-9/Homework/mandatory/1-practice/2-code-reading.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Take a look at the following code:

Explain why line 4 and line 6 output different numbers.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the PR to be against Manchester3 branch instead of master, ensure to check this as it may cause problems if people started adding comments :)

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:
Expand All @@ -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:
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is a bit tricky, search passing by value vs passing by reference

8 changes: 7 additions & 1 deletion week-9/Homework/mandatory/2-exercises/1-shopping-cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect


Expand Down
9 changes: 9 additions & 0 deletions week-9/Homework/mandatory/2-exercises/2-convertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
27 changes: 24 additions & 3 deletions week-9/Homework/mandatory/2-exercises/3-atm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,29 @@
*/

class ATM {
// Add your code here

// Add your code here
constructor(amount) {
this.amount = amount;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a small issue here, your constructure require an argument, but when you created an instance of the class in line 19 you didn't pass it.
The question will be, do you need the other variable ? 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
Expand All @@ -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
atm.make_withdrawl(500); // Your ATM should be able to handle this scenario
65 changes: 42 additions & 23 deletions week-9/Homework/mandatory/2-exercises/4-music-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work!


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"