This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>HTML 5 Boilerplate</title> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function getElementsByClassName( | |
element: Element, | |
classNames: string | |
): Array<Element> { | |
const elementsArr: Element[] = []; | |
function traverseNode(el: Element) { | |
if (el.hasChildNodes()) { | |
el.childNodes.forEach((node) => { | |
const nodeEl = node as Element; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function textSearch(text: string, query: string): string { | |
if (query.length === 0) return text; | |
const regex = new RegExp(query, 'gi'); | |
const matches = Array.from(text.matchAll(regex)); | |
console.log(matches); | |
let offset = 0; | |
if (matches.length === 0) return text; | |
matches.forEach((match) => { | |
const textString = text; | |
const index = match.index + offset; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface JQuery { | |
toggleClass: (className: string, state?: boolean) => JQuery; | |
addClass: (className: string) => JQuery; | |
removeClass: (className: string) => JQuery; | |
} | |
export default function $(selector: string): JQuery { | |
const element = document.querySelector(selector); | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function identicalDOMTrees(nodeA: Node, nodeB: Node): boolean { | |
//traversing two root nodes at the same time and compare them | |
if (nodeA.nodeType !== nodeB.nodeType) return false; | |
if (nodeA.nodeType === Node.TEXT_NODE) | |
return nodeA.textContent === nodeB.textContent; | |
// We can assume it's an element node from here on. | |
const elA = nodeA as Element; | |
const elB = nodeB as Element; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Element = { tag: string; children: Array<string | Element> }; | |
export default function serializeHTML(element: Element): string { | |
let html: string[] = []; | |
html.push(`<${element.tag}>`); | |
function traverseHTML(children: (string | Element)[]) { | |
children.forEach((child: string | Element) => { | |
let tag = '\n\t'; | |
if (typeof child === 'string') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function getElementsByTagName( | |
el: Element, | |
tagName: string, | |
): Array<Element> { | |
const elements: Array<Element> = []; | |
function traverseNode(el: Node) { | |
const nodes = el.childNodes; | |
nodes.forEach((node) => { | |
if (node.nodeName === tagName.toUpperCase()) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import $ from './functions/jQuery.css'; | |
document.body.innerHTML = '<button>Click me</button>'; | |
$('button') | |
.css('color', 'red') | |
.css('backgroundColor', 'tomato') | |
.css('fontSize', '16px'); | |
$('button').css('color'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function gainMaxValue(security_val: number[], k: number): number { | |
let maxValue = Number.NEGATIVE_INFINITY; | |
for (let startNode = 0; startNode < security_val.length; startNode++) { | |
let currentNode = startNode; | |
let currentValue = security_val[startNode]; | |
while (currentNode + k < security_val.length) { | |
currentNode += k; | |
currentValue += security_val[currentNode]; | |
if (currentValue && currentValue > maxValue) maxValue = currentValue; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function generateHashtag(str: string): string | boolean { | |
if (str.length === 0) return false; | |
const formattedStr = str.split(' ').map(capitalise).join(''); | |
if (formattedStr.length >= 140 || formattedStr.length === 0) return false; | |
return `#${formattedStr}`; | |
} | |
function capitalise(str: string): string { | |
if (str.length === 0) return ''; | |
const capital = str[0].toUpperCase() + str.substring(1, str.length); |