Skip to content

Instantly share code, notes, and snippets.

@smart-onion
Created May 17, 2025 12:49
Show Gist options
  • Save smart-onion/c9be150e696dfbc854db54135efe5a0e to your computer and use it in GitHub Desktop.
Save smart-onion/c9be150e696dfbc854db54135efe5a0e to your computer and use it in GitHub Desktop.
JS3
// Task PrintMachine
class PrintMachine{
constructor(fontSize, fontColor, fontFamily, element = document.body) {
this.fontSize = fontSize;
this.fontColor = fontColor;
this.fontFamily = fontFamily;
this.element = element;
}
print(text) {
let div = document.createElement("div")
div.style.fontSize = this.fontSize + 'px';
div.style.color = this.fontColor;
div.style.fontFamily = this.fontFamily;
div.textContent = text;
this.element.prepend(div);
document.write(text); // as requested
}
}
//let print = new PrintMachine(24, 'red', 'Arial');
//print.print('Hello world!');
// Task News board
class NewsBoard {
constructor(title, text, tags) {
this.title = title;
this.text = text;
this.tags = tags || [];
this.date = new Date();
}
#getTags() {
return this.tags.map(value => {
return `<a href="#">#${value}</a>`;
}).join(' ');
}
#getDate(){
let currentDay = new Date();
let diffTime = currentDay - this.date;
let diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 0) {
return 'Today';
}
else if(diffDays < 7) {
return `${diffDays} days ago`;
}
else return this.date.toDateString();
}
print() {
let card = document.createElement("div");
let title = document.createElement("h2");
title.textContent = this.title;
let text = document.createElement("p");
text.textContent = this.text;
let tags = document.createElement("p");
let date = document.createElement("p");
tags.innerHTML = this.#getTags();
date.textContent = this.#getDate();
card.appendChild(title);
card.appendChild(date);
card.appendChild(text);
card.appendChild(tags);
document.body.appendChild(card);
}
}
let newsBoard = new NewsBoard('Hello world', "some test text", ["news", "test", "hello"]);
newsBoard.date = new Date(2025, 4, 11);
newsBoard.print();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment