Best JavaScript code snippet using best
sketch.js
Source:sketch.js
1// Daniel Shiffman2// Nature of Code: Intelligence and Learning3// https://github.com/shiffman/NOC-S184// This flappy bird implementation is adapted from:5// https://youtu.be/cXgA1d_E-jY&6// How big is the population7let totalPopulation = 500;8// All active birds (not yet collided with pipe)9let activeBirds = [];10// All birds for any given population11let allBirds = [];12// Pipes13let pipes = [];14// A frame counter to determine when to add a pipe15let counter = 0;16// All time high score17let highScore = 0;18let tempHighScore = 0;19// Training or just showing the current best20let runBest = false;21// let runBestButton;22let params = {23 speed: 1,24 toggleState: function () {25 runBest = !runBest;26 // Show the best bird27 if (runBest) {28 resetGame();29 gui.__controllers[1].name("continue training")30 // Go train some more31 } else {32 nextGeneration();33 gui.__controllers[1].name("run best")34 }35 }36}37let gui;38function setup() {39 // let canvas = createCanvas(600, 400);40 let canvas = createCanvasCustom({41 statsFunc: () => "High Score : " + tempHighScore + " , All time high score : " + highScore42 });43 gui = new dat.GUI()44 gui.add(params, 'speed').min(1).max(10).step(1)45 gui.add(params, 'toggleState').name('run best')46 // Create a population47 for (let i = 0; i < totalPopulation; i++) {48 let bird = new Bird();49 activeBirds[i] = bird;50 allBirds[i] = bird;51 }52}53// Toggle the state of the simulation54function toggleState() {55 runBest = !runBest;56 // Show the best bird57 if (runBest) {58 resetGame();59 runBestButton.html('continue training');60 // Go train some more61 } else {62 nextGeneration();63 runBestButton.html('run best');64 }65}66function draw() {67 background(0);68 // Should we speed up cycles per frame69 let cycles = params.speed70 // speedSpan.html(cycles);71 // How many times to advance the game72 for (let n = 0; n < cycles; n++) {73 // Show all the pipes74 for (let i = pipes.length - 1; i >= 0; i--) {75 pipes[i].update();76 if (pipes[i].offscreen()) {77 pipes.splice(i, 1);78 }79 }80 // Are we just running the best bird81 if (runBest) {82 bestBird.think(pipes);83 bestBird.update();84 for (let j = 0; j < pipes.length; j++) {85 // Start over, bird hit pipe86 if (pipes[j].hits(bestBird)) {87 resetGame();88 break;89 }90 }91 if (bestBird.bottomTop()) {92 resetGame();93 }94 // Or are we running all the active birds95 } else {96 for (let i = activeBirds.length - 1; i >= 0; i--) {97 let bird = activeBirds[i];98 // Bird uses its brain!99 bird.think(pipes);100 bird.update();101 // Check all the pipes102 for (let j = 0; j < pipes.length; j++) {103 // It's hit a pipe104 if (pipes[j].hits(activeBirds[i])) {105 // Remove this bird106 activeBirds.splice(i, 1);107 break;108 }109 }110 if (bird.bottomTop()) {111 activeBirds.splice(i, 1);112 }113 }114 }115 // Add a new pipe every so often116 if (counter % 75 == 0) {117 pipes.push(new Pipe());118 }119 counter++;120 }121 // What is highest score of the current population122 tempHighScore = 0;123 // If we're training124 if (!runBest) {125 // Which is the best bird?126 let tempBestBird = null;127 for (let i = 0; i < activeBirds.length; i++) {128 let s = activeBirds[i].score;129 if (s > tempHighScore) {130 tempHighScore = s;131 tempBestBird = activeBirds[i];132 }133 }134 // Is it the all time high scorer?135 if (tempHighScore > highScore) {136 highScore = tempHighScore;137 bestBird = tempBestBird;138 }139 } else {140 // Just one bird, the best one so far141 tempHighScore = bestBird.score;142 if (tempHighScore > highScore) {143 highScore = tempHighScore;144 }145 }146 // Draw everything!147 for (let i = 0; i < pipes.length; i++) {148 pipes[i].show();149 }150 if (runBest) {151 bestBird.show();152 } else {153 for (let i = 0; i < activeBirds.length; i++) {154 activeBirds[i].show();155 }156 // If we're out of birds go to the next generation157 if (activeBirds.length == 0) {158 nextGeneration();159 }160 }...
stockPrices.js
Source:stockPrices.js
...36 }37 return Math.max(...profit)38}39// wrapper function to run best and provide useful output40function runBest(arr){41 console.log(arr, "->", best(arr))42}43// run the sample cases44runBest([1, 2, 3, 4, 5])45runBest([2, 3, 10, 6, 4, 8, 1])46runBest([7, 9, 5, 6, 3, 2])47runBest([0, 100])48runBest([5, 4 , 3, 2, 1])49runBest([100])...
Using AI Code Generation
1var BestPathFinder = require('./BestPathFinder');2var bestPathFinder = new BestPathFinder();3var bestPath = bestPathFinder.runBest();4console.log("The best path is: " + bestPath);5var BestPathFinder = function() {6 this.runBest = function() {7 return "best path";8 };9};10module.exports = BestPathFinder;11var BestPathFinder = require('./BestPathFinder');12var bestPathFinder = new BestPathFinder();13var bestPath = bestPathFinder.runBest();14console.log("The best path is: " + bestPath);15var BestPathFinder = function() {16 this.runBest = function() {17 return "best path";18 };19};20module.exports = BestPathFinder;21var BestPathFinder = require('./BestPathFinder');22var bestPathFinder = new BestPathFinder();23var bestPath = bestPathFinder.runBest();24console.log("The best path is: " + bestPath);25var BestPathFinder = function() {26 this.runBest = function() {27 return "best path";28 };29};30module.exports = BestPathFinder;31var BestPathFinder = require('./BestPathFinder');32var bestPathFinder = new BestPathFinder();33var bestPath = bestPathFinder.runBest();34console.log("The best path is: " + bestPath);35var BestPathFinder = function() {36 this.runBest = function() {37 return "best path";
Using AI Code Generation
1var BestPath = require('./BestPath.js');2];3var bestPath = new BestPath(graph);4var path = bestPath.runBest(0, 2);5console.log(path);6var path = bestPath.runBest(0, 4);7console.log(path);8var path = bestPath.runBest(2, 4);9console.log(path);10var path = bestPath.runBest(2, 2);11console.log(path);12var path = bestPath.runBest(0, 0);13console.log(path);14var path = bestPath.runBest(0, 1);15console.log(path);16var path = bestPath.runBest(1, 0);17console.log(path);18var path = bestPath.runBest(4, 4);19console.log(path);20var path = bestPath.runBest(4, 0);21console.log(path);22var path = bestPath.runBest(3, 1);23console.log(path);24var path = bestPath.runBest(1, 3);25console.log(path);26var path = bestPath.runBest(4, 3);27console.log(path);28var path = bestPath.runBest(3, 4);29console.log(path);30var path = bestPath.runBest(3, 0);31console.log(path);
Using AI Code Generation
1var BestPath = require('./BestPath.js');2var bestPath = new BestPath();3var start = "A";4var end = "C";5var path = "A-B-C";6console.log(bestPath.runBest(start, end, path));
Using AI Code Generation
1var BestPath = require('./lib/BestPath');2var bestPath = new BestPath();3var start = {lat: 37.782, lng: -122.445};4var end = {lat: 37.781, lng: -122.444};5var waypts = [{location: {lat: 37.782, lng: -122.446}, stopover: true},6 {location: {lat: 37.782, lng: -122.447}, stopover: true},7 {location: {lat: 37.782, lng: -122.448}, stopover: true},8 {location: {lat: 37.782, lng: -122.449}, stopover: true},9 {location: {lat: 37.782, lng: -122.450}, stopover: true},10 {location: {lat: 37.782, lng: -122.451}, stopover: true},11 {location: {lat: 37.782, lng: -122.452}, stopover: true},12 {location: {lat: 37.782, lng: -122.453}, stopover: true},13 {location: {lat: 37.782, lng: -122.454}, stopover: true},14 {location: {lat: 37.782, lng: -122.455}, stopover: true},15 {location: {lat: 37.782, lng: -122.456}, stopover: true},16 {location: {lat: 37.782, lng: -122.457}, stopover: true},17 {location: {lat: 37.782, lng: -122.458}, stopover: true},18 {location: {lat: 37.782, lng: -122.459}, stopover: true},19 {location: {lat: 37.782, lng: -122.460}, stopover: true},20 {location: {lat: 37.782, lng: -122.461}, stopover: true},21 {location: {lat: 37.782, lng: -122.462}, stopover: true},22 {location: {lat: 37.782, lng: -122.463}, stopover: true},23 {location: {lat
Using AI Code Generation
1var BestPath = require('./BestPath.js');2var graph = new BestPath();3graph.addEdge(1, 2, 1);4graph.addEdge(1, 3, 3);5graph.addEdge(2, 3, 1);6graph.addEdge(2, 4, 3);7graph.addEdge(2, 5, 10);8graph.addEdge(3, 4, 5);9graph.addEdge(4, 5, 4);10graph.addEdge(4, 6, 11);11graph.addEdge(5, 6, 2);12var bestPath = graph.runBest(1, 6);13console.log(bestPath);14function BestPath() {15 this.adj = {};16}17BestPath.prototype.addEdge = function(source, dest, weight) {18 if (!this.adj[source]) {19 this.adj[source] = [];20 }21 this.adj[source].push({node: dest, weight: weight});22}23BestPath.prototype.runBest = function(start, end) {24 var nodes = new PriorityQueue(),25 distances = {},26 previous = {},27 smallest, vertex, neighbor, alt;28 for(vertex in this.adj) {29 if(vertex === start) {30 distances[vertex] = 0;31 nodes.enqueue(0, vertex);32 }33 else {34 distances[vertex] = Infinity;35 nodes.enqueue(Infinity, vertex);36 }37 previous[vertex] = null;38 }39 while(nodes.values.length) {40 smallest = nodes.dequeue().val;41 if(smallest === end) {42 while(previous[smallest]) {43 path.push(smallest);44 smallest = previous[smallest];45 }46 break;
Using AI Code Generation
1var BestPathFinder = require('./BestPathFinder.js').BestPathFinder;2var pathFinder = new BestPathFinder();3pathFinder.runBest(1, 1, 4, 4, pathFinder.manhattanDistance);4pathFinder.runBest(1, 1, 4, 4, pathFinder.euclideanDistance);5pathFinder.runBest(1, 1, 4, 4, pathFinder.diagonalDistance);6var BestPathFinder = require('bestpathfinder').BestPathFinder;7var pathFinder = new BestPathFinder();8pathFinder.runBest(1, 1, 4, 4, pathFinder.manhattanDistance);
Using AI Code Generation
1var grid = [];2for (var i = 0; i < 5; i++) {3 grid[i] = [];4 for (var j = 0; j < 5; j++) {5 grid[i][j] = 0;6 }7}8grid[1][1] = 1;9grid[1][2] = 1;10grid[2][3] = 1;11grid[3][1] = 1;12grid[3][2] = 1;13grid[3][3] = 1;14var bp = new BestPath(grid);15var path = bp.runBest(0, 0, 4, 4);16console.log(path);17console.log(bp.getCost());18var grid = [];19for (var i = 0; i < 5; i++) {20 grid[i] = [];21 for (var j = 0; j < 5; j++) {22 grid[i][j] = 0;23 }24}25grid[1][1] = 1;26grid[1][2] = 1;27grid[2][3] = 1;28grid[3][1] = 1;29grid[3][2] = 1;30grid[3][3] = 1;31var bp = new BestPath(grid);32var path = bp.runBest(0, 0, 4, 4);33console.log(path);34console.log(bp.getCost());
Using AI Code Generation
1var BestTime = require('./bestTime.js');2var bestTime = new BestTime();3var bestTime = bestTime.runBest();4console.log(bestTime);5var BestTime = function () {6 this.runBest = function () {7 return "Best Time: 3:00pm";8 };9};10module.exports = BestTime;11var BestTime = require('./bestTime.js');12var bestTime = new BestTime();13var bestTime = bestTime.runBest();14console.log(bestTime);15var BestTime = function () {16 this.runBest = function () {17 return "Best Time: 3:00pm";18 };19};20module.exports = new BestTime();21var BestTime = require('./bestTime.js');22var bestTime = BestTime.runBest();23console.log(bestTime);24var BestTime = function () {25 this.runBest = function () {26 return "Best Time: 3:00pm";27 };28};29module.exports = BestTime;30var BestTime = require('./bestTime.js');31var bestTime = new BestTime();32var bestTime = bestTime.runBest();33console.log(bestTime);34var BestTime = function () {35 this.runBest = function () {36 return "Best Time: 3:00pm";37 };38};39module.exports = BestTime;40var BestTime = require('./bestTime.js');41var bestTime = BestTime();
Using AI Code Generation
1var BestPathFinder = require('./BestPathFinder.js');2var graph = {3 start: { A: 5, B: 2 },4 A: { C: 4, D: 2 },5 B: { A: 8, D: 7 },6 C: { finish: 3, D: 6 },7 D: { finish: 1 },8 finish: {}9};10var constraints = {11};12var finder = new BestPathFinder(graph, constraints);13var bestPath = finder.runBest();14console.log(bestPath);
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!!