Skip to content
Open

HW1 #11

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
12 changes: 9 additions & 3 deletions src/components/DivElement.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Import HTMLElement here
import HTMLElement from './HTMLElement';

// Define class here
class DivElement extends HTMLElement {
constructor(content) {
super(content);

Choose a reason for hiding this comment

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

Suggested change
super(content);
super('div', content);

And then you don't need the next two lines.

this.tag = 'div';
this.content = content;
}
}

// Export class here
export default DivElement;
17 changes: 14 additions & 3 deletions src/components/HTMLElement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
// Define class here
class HTMLElement {
constructor(tag, content) {
this.tag = tag;
this.content = content;
}

// Export class here
export default {};
render() {
const sTag = `<${this.tag}>`;
const eTag = `</${this.tag}>`;
const elem = sTag + this.content + eTag;
return elem;

Choose a reason for hiding this comment

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

Consider putting all this on one line.

}
}

export default HTMLElement;
30 changes: 26 additions & 4 deletions src/rolodex/rolodexPrinter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import people from './people.json';

people.forEach(function (person) {
people.forEach((person) => {
const names = person.name.split(' ');
const firstName = names[0];
const lastName = names[1];
const email = person.email;
const phone = person.phone;
const { email } = person;
const { phone } = person;
console.log(`First name: ${firstName}\nLast name: ${lastName}\nEmail: ${email}\nPhone number: ${phone}\n`);
});

console.log('****** Refactor the code to use template literals *******');
people.forEach((person) => {
const firstName = ` ${person.name.split(' ')[0]} `;
const lastName = ` ${person.name.split(' ')[1]} `;
const email = ` ${person.email} `;
const phone = ` ${person.phone}`;
console.log(`First name: ${firstName}\nLast name: ${lastName}\nEmail: ${email}\nPhone number: ${phone}\n`);
});

console.log('****** Refactor the code to use object destructuring ******');
people.forEach((person) => {

Choose a reason for hiding this comment

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

Suggested change
people.forEach((person) => {
people.forEach(({name, email, phone}) => {

const { name, email, phone } = person;
const [firstName, lastName] = name.split(' ');
console.log(`First name: ${firstName}\nLast name: ${lastName}\nEmail: ${email}\nPhone number: ${phone}\n`);
});

console.log('First name: ' + firstName + '\nLast name: ' + lastName + '\nEmail: ' + email + '\nPhone number: ' + phone + '\n');
console.log('******Refactor the code to use array destructuring. *****');
people.forEach((person) => {
const [, name, , email, , phone] = Object.values(person);
const [firstName, lastName] = name.split(' ');
console.log(`First name: ${firstName}\nLast name: ${lastName}\nEmail: ${email}\nPhone number: ${phone}\n`);
});
46 changes: 33 additions & 13 deletions src/timer/Timer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,37 @@
function Timer(seconds) {
this.seconds = seconds;
}
//* Examine the code in these two files and determine each script's behavior.
// function Timer(seconds) {
// this.seconds = seconds;
// }
// Timer.prototype.start = function () {
// var instance = this;
// var timerInterval = setInterval(function () {
// if (instance.seconds === 0) {
// clearInterval(timerInterval);
// }
// console.log(instance.seconds);
// instance.seconds -= 1;
// }, 1000);
// };
// --- * Refactor the code to use the ES6 class syntax.
class Timer {
constructor(seconds) {
this.seconds = seconds;
}

Timer.prototype.start = function () {
var instance = this;
var timerInterval = setInterval(function () {
if (instance.seconds === 0) {
clearInterval(timerInterval);
}
start() {
// * Refactor the code to use `let/const`.
// * Refactor the code to avoid assigning `this` to a variable.
let instance = this.seconds;

Choose a reason for hiding this comment

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

You don't need to reference instance, you can just reference this.seconds


console.log(instance.seconds);
instance.seconds -= 1;
}, 1000);
};
// * Refactor the code to use arrow function(s).
const timerInterval = setInterval(() => {
if (instance === 0) {

Choose a reason for hiding this comment

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

Suggested change
if (instance === 0) {
if (this.seconds === 0) {

clearInterval(timerInterval);
}
console.log(instance);
instance -= 1;
}, 1000);
}
}

export default Timer;