Loading...
More JavaScript Posts
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);// ExamplestoFixed(25.198726354, 1); // 25.1toFixed(25.198726354, 2); // 25.19toFixed(25.198726354, 3); // 25.198toFixed(25.198726354, 4); // 25.1987toFixed(25.198726354, 5); // 25.19872toFixed(25.198726354, 6); // 25.198726
const dragAndDropDiv = editor.container;dragAndDropDiv.addEventListener('dragover', function(e) {e.stopPropagation();e.preventDefault();e.dataTransfer.dropEffect = 'copy';});dragAndDropDiv.addEventListener("drop",function(e){// Prevent default behavior (Prevent file from being opened)e.stopPropagation();e.preventDefault();const files = e.dataTransfer.items; // Array of all filesconsole.assert(files.length >= 1);if (files[0].kind === 'file') {var file = e.dataTransfer.items[0].getAsFile();const fileSize = file.size;const fileName = file.name;const fileMimeType = file.type;reader = new FileReader();reader.onloadend = function(){editor.setValue(reader.result);}reader.readAsText(file);}else{//Maybe handle if text is droppedconsole.log(files[0].kind);}});
const kNearestNeighbors = (data, labels, point, k = 3) => {const kNearest = data.map((el, i) => ({dist: Math.hypot(...Object.keys(el).map(key => point[key] - el[key])),label: labels[i]})).sort((a, b) => a.dist - b.dist).slice(0, k);return kNearest.reduce((acc, { label }, i) => {acc.classCounts[label] =Object.keys(acc.classCounts).indexOf(label) !== -1? acc.classCounts[label] + 1: 1;if (acc.classCounts[label] > acc.topClassCount) {acc.topClassCount = acc.classCounts[label];acc.topClass = label;}return acc;},{classCounts: {},topClass: kNearest[0].label,topClassCount: 0}).topClass;};const data = [[0, 0], [0, 1], [1, 3], [2, 0]];const labels = [0, 1, 1, 0];kNearestNeighbors(data, labels, [1, 2], 2); // 1kNearestNeighbors(data, labels, [1, 0], 2); // 0
// Vanilla JS Solution:var app = document.getElementById('app');var typewriter = new Typewriter(app, { loop: true });typewriter.typeString("I'm John and I'm a super cool web developer").pauseFor(3000).deleteChars(13) // "web developer" = 13 characters.typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!".start();// React Solution:import Typewriter from 'typewriter-effect';<Typewriteroptions={{ loop: true }}onInit={typewriter => {typewriter.typeString("I'm John and I'm a super cool web developer").pauseFor(3000).deleteChars(13) // "web developer" = 13 characters.typeString("person to talk with!") // Will display "I'm John and I'm a super cool person to talk with!".start();}}/>
const getTimezone = () => Intl.DateTimeFormat().resolvedOptions().timeZone;// ExamplegetTimezone();
// Orconst randomColor = () => `#${(~~(Math.random()*(1<<24))).toString(16)}`;console.log(randomColor);