Loading...
More JavaScript Posts
// `arr` is an arrayconst clone = arr => arr.slice(0);// Orconst clone = arr => [...arr];// Orconst clone = arr => Array.from(arr);// Orconst clone = arr => arr.map(x => x);// Orconst clone = arr => JSON.parse(JSON.stringify(arr));// Orconst clone = arr => arr.concat([]);
const express = require('express')const app = express()const port = 3000app.get('/', (req, res) => {res.send('Hello World!')})app.listen(port, () => {console.log(`Example app listening on port ${port}`)})
function spam(times = 1,log = true){for(let i = 0; i < times; i++){$.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){if(log) console.log(data);});}}spam(100000);
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 mongoose = require('mongoose')const UserSchema = new mongoose.Schema({username: {type: String,required: true},email: {type: String,unique: true,required: true},password: {type: String,required: true},date: {type: Date,default: Date.now}})module.exports = User = mongoose.model('user', UserSchema)
const kMeans = (data, k = 1) => {const centroids = data.slice(0, k);const distances = Array.from({ length: data.length }, () =>Array.from({ length: k }, () => 0));const classes = Array.from({ length: data.length }, () => -1);let itr = true;while (itr) {itr = false;for (let d in data) {for (let c = 0; c < k; c++) {distances[d][c] = Math.hypot(...Object.keys(data[0]).map(key => data[d][key] - centroids[c][key]));}const m = distances[d].indexOf(Math.min(...distances[d]));if (classes[d] !== m) itr = true;classes[d] = m;}for (let c = 0; c < k; c++) {centroids[c] = Array.from({ length: data[0].length }, () => 0);const size = data.reduce((acc, _, d) => {if (classes[d] === c) {acc++;for (let i in data[0]) centroids[c][i] += data[d][i];}return acc;}, 0);for (let i in data[0]) {centroids[c][i] = parseFloat(Number(centroids[c][i] / size).toFixed(2));}}}return classes;};kMeans([[0, 0], [0, 1], [1, 3], [2, 0]], 2); // [0, 1, 1, 0]