Loading...
More JavaScript Posts
function Clock(props) {return (<div><h1>Hello, world!</h1><h2>It is {props.date.toLocaleTimeString()}.</h2></div>);}function tick() {ReactDOM.render(<Clock date={new Date()} />,document.getElementById('root'));}setInterval(tick, 1000);
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]
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)
/*** @param {number[]} nums* @param {number} target* @return {number[]}*/var twoSum = function(nums, target) {for(let i = 0; i < nums.length; i++) {for(let j = 0; j < nums.length; j++) {if(nums[i] + nums[j] === target && i !== j) {return [i, j]}}}};
const arithmeticProgression = (n, lim) =>Array.from({ length: Math.ceil(lim / n) }, (_, i) => (i + 1) * n );arithmeticProgression(5, 25); // [5, 10, 15, 20, 25]
const MongoClient = require('mongodb').MongoClient;const assert = require('assert');// Connection URLconst url = 'mongodb://localhost:27017';// Database Nameconst dbName = 'myproject';// Use connect method to connect to the serverMongoClient.connect(url, function(err, client) {assert.equal(null, err);console.log("Connected successfully to server");const db = client.db(dbName);client.close();});