Loading...
More JavaScript Posts
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
export const Main = () => {const [title, setTitle] = useState('');return (<ChildComponenttitle={title}onChange={e => setTitle(e.target.value)}/>);};export const ChildComponent = ({ title, onChange }) => {return (<Box component="form" onSubmit={() => {}} noValidate mt={5}><TextFieldfullWidthid="title"label="Title"name="title"autoComplete="title"autoFocuscolor="primary"variant="filled"InputLabelProps={{ shrink: true }}value={title}onChange={onChange}/></Box>);};
require("dotenv").config();const mongoose = require("mongoose");const db = process.env.MONGO_URI;const connectDB = async () => {try {await mongoose.connect(db, {useNewUrlParser: true,useUnifiedTopology: true,useFindAndModify: false,useCreateIndex: true});console.log("MongoDB Connected");} catch (err) {console.error(err.message);}};module.exports = connectDB;
// 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();}}/>
# Acronyms## Computer Numerical Control (CNC)- Typicalldfdfsdffdafdasgfdgfgfdfdafdasfdafda
const gcd = (...arr) => {const _gcd = (x, y) => (!y ? x : gcd(y, x % y));return [...arr].reduce((a, b) => _gcd(a, b));};gcd(8, 36); // 4gcd(...[12, 8, 32]); // 4