New files with JavaScript exercises

parent 200620ec
......@@ -2,14 +2,14 @@ class Bank{
constructor() {
this.customers = {};
}
};
}
Bank.prototype.addCustomer = function(customer) {
this.customers[customer] = 0;
}
let bank = new Bank();
let bank = new Bank();
bank.addCustomer('Sheldon');
......
<!DOCTYPE html>
<html>
<head>
<meta charset = 'utf-8'>
<title>Calculator</title>
</head>
<body>
<section>
<input type='text' id='first-number'> <!--- first field where to insert the first number to add -->
<input type='text' id='second-number'> <!--- second field where to insert the second number to add -->
<button onclick='add()'>Add them</button> <!--- button to add the number that receives the function from Calculator.js -->
</section>
<h2 id='sum-two-numbers'></h2> <!--- field that shows the result after clicking on the button -->
</body>
<script src='Calculator.js'></script>
</html>
\ No newline at end of file
function add() {
let firstNumber = document.getElementById('first-number').value; // getting the value of the first field on html
let secondNumber = document.getElementById('second-number').value; // getting the value of the first field on html
let first = parseFloat(firstNumber); // transforming the written number (string) into a number
let second = parseFloat(secondNumber); // transforming the written number (string) into a number
let sumTwoNumbers = first + second; // sum
document.getElementById("sum-two-numbers").innerHTML = sumTwoNumbers; // return the sum to the correct field in html
}
class Player {
constructor() {
this.tracks = [];
this.actualTrack = 0;
}
}
class Track {
constructor(artist, title, album) {
this.artist = artist;
this.title = title;
this.album = album;
}
}
let player = new Player();
let driveTrack = new Track('Incubus', 'Drive', 'Make Yourself');
let laBambaTrack = new Track('Ritchie Valens', 'La Bamba', 'La Bamba');
Player.prototype.add = function(string) {
this.tracks.push(string);
}
Player.prototype.play = function() {
console.log(`Playing: ${this.tracks[this.actualTrack].title} by ${this.tracks[this.actualTrack].artist}`);
}
Player.prototype.next = function() {
if (this.actualTrack < this.tracks.length-1) {
this.actualTrack = this.actualTrack + 1;
}
else {
this.actualTrack = 0;
}
}
Player.prototype.previous = function() {
if (this.actualTrack > 0) {
this.actualTrack = this.actualTrack - 1;
}
else {
this.actualTrack = this.tracks.length -1;
}
}
player.add(driveTrack); // add 'Incubus', 'Drive', 'Make Yourself'
player.play(); // display "Drive by Incubus"
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment