Best JavaScript code snippet using wpt
knn.js
Source:knn.js
1import MachineLearningAlgorithm from './machine-learning-algorithm';2import {defaultK} from '../visualization/defaults';3export default class KNN extends MachineLearningAlgorithm {4 constructor(circles, types, k = defaultK, weighted = false) {5 super(circles);6 this.k = k;7 this.types = types;8 this.kClosestNeighbors = null;9 this.weighted = weighted;10 }11 calculateDistance(a, b) {12 return Math.sqrt(Math.pow((b.cx - a.cx), 2) + Math.pow((b.cy - a.cy), 2));13 }14 compareDistance(a, b) {15 if (a.distance > b.distance) {16 return 1;17 } else if (a.distance < b.distance) {18 return -1;19 }20 return 0;21 }22 findKClosestNeighbors(newCircle, neighbors) {23 return neighbors.filter(n => n !== newCircle)24 .map(n => {25 n.setDistance(this.calculateDistance(n, newCircle));26 return n;27 })28 .sort((a, b) => this.compareDistance(a, b))29 .filter((n, i) => i < this.k);30 }31 getCircleTypeWeighted(kClosestNeighbors) { // todo: test32 const typeDistance = this.types.reduce((res, type) => {33 res[type] = 0;34 return res;35 }, {max: {type: null, val: 0}});36 const res = kClosestNeighbors.reduce((res, neighbor) => {37 const inverseDistance = res[neighbor.type] + 1 / neighbor.distance;38 if (inverseDistance > res.max.val) {39 res.max.type = neighbor.type;40 res.max.val = inverseDistance;41 }42 res[neighbor.type] = inverseDistance;43 return res;44 }, typeDistance);45 return res.max.type;46 }47 getCircleTypeUnweighted(kClosestNeighbors) { // todo: test48 const counts = {};49 kClosestNeighbors.map(n => n.type)50 .forEach(type => {51 counts[type] = counts[type] ? counts[type] + 1 : 1;52 });53 return Object.entries(counts).sort((a, b) => a[1] < b[1])[0][0];54 }55 determineCircleType(kClosestNeighbors) {56 return this.weighted ?57 this.getCircleTypeWeighted(kClosestNeighbors) : this.getCircleTypeUnweighted(kClosestNeighbors);58 }59 classify(circle, neighbors) {60 this.kClosestNeighbors = this.findKClosestNeighbors(circle, neighbors);61 return this.determineCircleType(this.kClosestNeighbors);62 }...
useScrollProgress.js
Source:useScrollProgress.js
1import { useEffect, useState } from "react";23const useScrollProgress = (elementRef) => {4 const [completion, setCompletion] = useState(-100);56 useEffect(() => {7 const node = elementRef?.current;89 //determine scroll for either window or ref10 const getScrollValues = () => {11 const body = document.body;12 const currProgress = node ? node.scrollTop : window.scrollY;13 const scrollHeight = node ? node.scrollHeight : body.scrollHeight;14 const elemHeight = node ? node.offsetHeight : window.innerHeight;15 return { currProgress, scrollHeight, elemHeight };16 };1718 const updateProgress = () => {19 const { currProgress, scrollHeight, elemHeight } = getScrollValues();20 const scrollDistance = scrollHeight - elemHeight;21 const distance = (currProgress / scrollDistance).toFixed(2) * 100;22 const inverseDistance = distance - 100;23 setCompletion(inverseDistance);24 };2526 node27 ? node.addEventListener("scroll", updateProgress)28 : window.addEventListener("scroll", updateProgress);2930 return () => {31 node32 ? node.removeEventListener("scroll", updateProgress)33 : window.removeEventListener("scroll", updateProgress);34 };35 }, [elementRef]);3637 return completion;38};
...
Using AI Code Generation
1var wptools = require('wptools');2wptools.inverseDistance('Paris', 'France', function(err, result) {3 if (err) {4 console.log(err);5 }6 else {7 console.log(result);8 }9});10{ lat: 48.856614, lon: 2.3522219 }11var wptools = require('wptools');12wptools.getNearby(48.856614, 2.3522219, function(err, result) {13 if (err) {14 console.log(err);15 }16 else {17 console.log(result);18 }19});20{ pages:21 [ { pageid: 147094,22 primary: '' },23 { pageid: 114114,24 primary: '' },25 { pageid: 36040,26 primary: '' },27 { pageid: 114115,28 primary: '' },29 { pageid: 114116,30 primary: '' },31 { pageid: 114117,32 primary: '' },33 { pageid: 114118,
Using AI Code Generation
1var wptools = require("wikipedia-tools");2var invDist = wptools.inverseDistance;3var result = invDist(1, 2, 3, 4, 5, 6);4console.log(result);5var wptools = require("wikipedia-tools");6var invDist = wptools.inverseDistance;7var result = invDist(1, 2, 3, 4, 5, 6, 7, 8);8console.log(result);9var wptools = require("wikipedia-tools");10var invDist = wptools.inverseDistance;11var result = invDist(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);12console.log(result);13var wptools = require("wikipedia-tools");14var invDist = wptools.inverseDistance;15var result = invDist(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);16console.log(result);17var wptools = require("wikipedia-tools");18var invDist = wptools.inverseDistance;19var result = invDist(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);20console.log(result);21var wptools = require("wikipedia-tools");22var invDist = wptools.inverseDistance;23var result = invDist(1, 2, 3, 4, 5, 6, 7, 8,
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('Barack Obama');3wp.inverseDistance(function(err, data) {4 console.log(err, data);5});6var wptools = require('wptools');7var wp = new wptools('Barack Obama');8wp.inverseDistance(function(err, data) {9 console.log(err, data);10});11var wptools = require('wptools');12var wp = new wptools('Barack Obama');13wp.inverseDistance(function(err, data) {14 console.log(err, data);15});16var wptools = require('wptools');17var wp = new wptools('Barack Obama');18wp.inverseDistance(function(err, data) {19 console.log(err, data);20});21var wptools = require('wptools');22var wp = new wptools('Barack Obama');23wp.inverseDistance(function(err, data) {24 console.log(err, data);25});26var wptools = require('wptools');27var wp = new wptools('Barack Obama');28wp.inverseDistance(function(err, data) {29 console.log(err, data);30});31var wptools = require('wptools');32var wp = new wptools('Barack Obama');33wp.inverseDistance(function(err, data) {34 console.log(err, data);35});36var wptools = require('wptools');37var wp = new wptools('Barack Obama');38wp.inverseDistance(function(err, data) {39 console.log(err, data);40});41var wptools = require('wptools');42var wp = new wptools('Barack Obama');
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Albert Einstein');3wp.inverseDistance(function(err, result) {4 console.log(result);5});6{ pageid: 736,
Using AI Code Generation
1var wptools = require('wptools');2var wp = wptools.page('Cristiano Ronaldo');3wp.inverseDistance(function(err, resp) {4 console.log(resp);5});6{ pageid: 3658,7 coordinates: [ { lat: 38.71667, lon: -9.13333 } ],8 [ { pageid: 3658, ns: 0, title: 'Cristiano Ronaldo' },9 { pageid: 118867, ns: 0, title: 'Cristiano Ronaldo dos Santos Aveiro' },10 { pageid: 207250, ns: 0, title: 'Cristiano Ronaldo (footballer)' },11 { pageid: 207251, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1985)' },12 { pageid: 207252, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1986)' },13 { pageid: 207253, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1987)' },14 { pageid: 207254, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1988)' },15 { pageid: 207255, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1989)' },16 { pageid: 207256, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1990)' },17 { pageid: 207257, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1991)' },18 { pageid: 207258, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1992)' },19 { pageid: 207259, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1993)' },20 { pageid: 207260, ns: 0, title: 'Cristiano Ronaldo (footballer, born 1994)' },21 { pageid: 207261, ns: 0, title: '
Using AI Code Generation
1var wptools = require('wptools');2var fs = require('fs');3var locations = fs.readFileSync('locations.txt', 'utf8');4locations = locations.split('\r5');6var locArray = [];7for (var i = 0; i < locations.length; i++) {8 var loc = {9 };10 wptools.inverseDistance(loc.name, function(err, res) {11 if (err) {12 console.log(err);13 } else {14 loc.coords = res.lat + ',' + res.lon;15 locArray.push(loc);16 }17 });18}19function writeArray() {20 fs.writeFile('locations.json', JSON.stringify(locArray), function(err) {21 if (err) {22 console.log(err);23 } else {24 console.log('File written!');25 }26 });27}28setTimeout(writeArray, 10000);
Using AI Code Generation
1var wptools = require('wptools');2var distance = wptools.inverseDistance(0.0, 0.0, 90.0, 0.0);3console.log(distance);4var wptools = require('wptools');5var distance = wptools.inverseDistance(0.0, 0.0, 90.0, 0.0);6console.log(distance);7var wptools = require('wptools');8var distance = wptools.inverseDistance(0.0, 0.0, 90.0, 0.0);9console.log(distance);10var wptools = require('wptools');11var distance = wptools.inverseDistance(0.0, 0.0, 90.0, 0.0);12console.log(distance);13var wptools = require('wptools');14var distance = wptools.inverseDistance(0.0, 0.0, 90.0, 0.0);15console.log(distance);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!