Best JavaScript code snippet using wpt
A2.js
Source:A2.js
...118/**119 * 该å½æ°å°å®ç°å¾çæ¹åå空æ¹åç交æ¢ï¼å
æ¬å¾ç交æ¢åimagePositionæ°æ®äº¤æ¢ä»¥åå¨è¾¹âæ¿æ´»âå120 * @param item å°è¦å空åè¿è¡äº¤æ¢çå°æ¹å121 */122function doExchange(item){123 let imagePosition=toObject(item.dataset.imagePosition);//å¾å°itemçå¾çä½ç½®124 let emptyImagePosition=emptyBlock.dataset.imagePosition;//å¾å°ç©ºåçå¾çä½ç½®125 emptyBlock.dataset.imagePosition=item.dataset.imagePosition;//ä¿®æ¹ç©ºåçå¾çä½ç½®126 item.dataset.imagePosition=emptyImagePosition;//ä¿®æ¹itemçå¾çä½ç½®127 emptyBlock.style.background="url("+image.src+") "+singleWidth*imagePosition.x+"px "+singleHeight*imagePosition.y+"px" ;128 item.style.background=null;//å¡«å
å¾å129 let disableBlocks=getAroundBlocks(emptyBlock);130 for(let i=0;i<disableBlocks.length;i++){131 disableBlocks[i].className="rowItemDisable";132 }133 emptyBlock=item;//æ¢å¤å¤äºâæ¿æ´»âç¶æçå°å134 let enableBlocks=getAroundBlocks(emptyBlock);135 for(let i=0;i<enableBlocks.length;i++){136 enableBlocks[i].className="rowItemEnable";137 }//å±ç¤ºæ°çæ¿æ´»ç¶æ138 let scoreElement=document.getElementById("score");139 score+=1;140 scoreElement.innerHTML="å½åæä½æ¬¡æ°ï¼"+score;141 setTimeout(function(){ 142 if(gameOver()){//æ£æ¥æ¸¸ææ¯å¦ç»æ143 alert("æåä½ ï¼å®æä»»å¡ï¼æ¥ä¸æ¥æææ´é«é¾åº¦å§:-)");144 difficulty=(difficulty+1)%3+3;145 singleHeight=-1*image.height/difficulty;146 singleWidth=-1*image.width/difficulty;147 opening();148 }149 },1000); 150 // //第äºç151 // let imagePosition=toObject(item.dataset.imagePosition);152 // let emptyImagePosition=emptyBlock.dataset.imagePosition;153 // emptyBlock.style.background="url("+image.src+") "+singleWidth*imagePosition.x+"px "+singleHeight*imagePosition.y+"px" ;154 // emptyBlock.dataset.imagePosition=toString(imagePosition);155 // // emptyBlock.style.opacity=0;156 // let emptyOpacity = 0;157 // let emptyInterval = setInterval(function () {158 // emptyOpacity++;159 // emptyBlock.style.opacity = emptyOpacity*0.1;160 // if(emptyOpacity === 10){161 // clearInterval(emptyInterval);162 // }163 // },25);164 // let itemOpacity = 10;165 // item.dataset.imagePosition=emptyImagePosition;166 // let itemInterval = setInterval(function () {167 // itemOpacity--;168 // item.style.opacity =itemOpacity*0.1;169 // if(itemOpacity === 0){170 // item.style.background=null;171 // emptyBlock=item;172 // if(gameOver()){173 // alert("æåä½ ï¼å®æä»»å¡ï¼æ¥ä¸æ¥æææ´é«é¾åº¦å§:-)");174 // }175 // clearInterval(itemInterval);176 // }177 // },25);178 179 // //第ä¸ç180 // fillImage(imagePositionTemp,emptyBlock);181 // let disableBlocks=getAroundBlocks(emptyBlock);182 // for(let i=0;i<disableBlocks.length;i++){183 // disableBlocks[i].className="rowItem";184 // }185 // cleanImage(item);186 // emptyBlock=item;187 // let enableBlocks=getAroundBlocks(emptyBlock);188 // for(let i=0;i<enableBlocks.length;i++){189 // enableBlocks[i].className="rowItemActive";190 // }191}192 193/**194 *195 * @param centerBlock å¤äºä¸å¿çå°æ¹å196 * @returns {Array} ä¸å¿å°æ¹åå¨å´çå°æ¹å197 */198function getAroundBlocks(centerBlock){199 let centerPosition=toObject(centerBlock.dataset.physicalPosition);200 let targetBlock;201 let blocks=[];202 //æ£æ¥ä¸ä¸å·¦å³æ¯å¦å¨çå
ï¼203 if(centerPosition.x-1>=0){//左边204 targetBlock=document.getElementById("id"+(centerPosition.x-1+centerPosition.y*difficulty));205 blocks.push(targetBlock);206 }207 if(centerPosition.x+1<difficulty){//å³è¾¹208 targetBlock=document.getElementById("id"+(centerPosition.x+1+centerPosition.y*difficulty));209 blocks.push(targetBlock);210 }211 if(centerPosition.y+1<difficulty){//ä¸è¾¹212 targetBlock=document.getElementById("id"+(centerPosition.x+(centerPosition.y+1)*difficulty));213 blocks.push(targetBlock);214 }215 if(centerPosition.y-1>=0){//ä¸è¾¹216 targetBlock=document.getElementById("id"+(centerPosition.x+(centerPosition.y-1)*difficulty));217 blocks.push(targetBlock);218 }219 return blocks;220}221 222/**223 * éæ©æ¸¸æé¾åº¦ï¼3*3ã4*4ã5*5224 * @param difficultyNum225 */226function onChooseDifficulty(difficultyNum){227 difficulty=difficultyNum;228 image=new Image();229 image.src=imagePath;230 image.onload=function() {231 singleWidth=-1*image.width/difficulty;232 singleHeight=-1*image.height/difficulty;233 opening();234 }235}236 237/**238 * éæ©æ¸¸æå¾ç239 * @param imageNum 游æå¾ç代ç 240 */241function onChooseImage(imageNum){242 image=new Image();243 switch(imageNum){244 case 0:245 imagePath="./img/duck.jpg";246 break;247 case 1:248 imagePath="./img/guowang.jpg";249 break;250 case 2:251 imagePath="./img/long.jpg";252 break;253 }254 let tipImg=document.getElementById("tipImg");255 image.src=imagePath;256 tipImg.src=image.src;257 image.onload=function() {258 singleWidth=-1*image.width/difficulty;259 singleHeight=-1*image.height/difficulty;260 opening();261 }262}263 264/**265 * å°æ¹åçç¹å»å½æ°266 */267function onItemClick(){268 let item=this;269 let itemPosition=toObject(item.dataset.physicalPosition);270 //å¤ææ¯å¦å¯ä»¥ç§»å¨ï¼å¦æå¯ä»¥ç§»å¨ï¼æåæä½åå
¥æ 271 if(isOnEmptyLeft(itemPosition)){272 operateStack.push(emptyMoveRight);273 }else if(isOnEmptyUp(itemPosition)){274 operateStack.push(emptyMoveDown);275 }else if(isOnEmptyRight(itemPosition)){276 operateStack.push(emptyMoveLeft);277 }else if(isOnEmptyDown(itemPosition)){278 operateStack.push(emptyMoveUp);279 }else{280 return;281 }282 doExchange(item);//移å¨å°±å¥½283}284 285/**286 * æ示çç¹å»å½æ°ï¼ä»æä½æ éå¼¹åºä¸ä¸ªå½æ°ï¼ç¶åè°ç¨å³å¯å¤å287 */288function onTips(){289 let doFunction=operateStack.pop();290 if(doFunction){291 doFunction();292 }//å®é
ä¸ï¼æä½æ 为空çæ¶åï¼æ¸¸æä¹å°±ç»æäº293}294 295/**296 * 该å½æ°èµ·å°æ´çæä½ï¼ä½æ¯ä¸å±ç¤ºâç¹æâï¼ä»
ä»
å¨æ°æ®ä¸å®ç°æ´çï¼297 * 该æ´çç®æ³ä¿è¯äºæ¸¸æä¸å®æ解ï¼ä½æ¯æ¯è¾æè ¢ï¼æå¯è½å·¦ç§»æå°±å³ç§»ï¼å®é
ä¸ä¹åºè¯¥å¯ä»¥å¤ç298 * ä½æ¯ç±äºå°æªå®ç°299 * @returns {Array}å¾çä½ç½®ä¿¡æ¯æ°ç»300 */301function getOpeningPositions(){302 let positions=[];303 operateStack=[];304 for(let y=0;y<difficulty;y++){305 for(let x=0;x<difficulty;x++){306 positions[y*difficulty+x]=new Position(x,y);307 }308 }//å®æ顺åºå¡«å
309 let currentEmptyX=difficulty-1;310 let currentEmptyY=difficulty-1;//è®°å½ç©ºåä½ç½®ä¿¡æ¯311 let emptyPositionId=currentEmptyX+currentEmptyY*difficulty;312 let moveNum=5*difficulty;//çæ移å¨æ¬¡æ°313 let tempPosition;314 let targetPositionId;315 let directionNum;316 let doExchange=false;//æ¯å¦éè¦æ§è¡äº¤æ¢317 for(let i=0;i<moveNum;i++){318 directionNum=Math.floor(Math.random()*4+1);//产çéæºæ¹åæ°ï¼ä¸ä¸å·¦å³å个319 //æ£æ¥æ¯å¦å¯ä»¥ç§»å¨320 switch(directionNum){321 case 1://ä¸322 if(currentEmptyY-1>=0){323 currentEmptyY--;324 operateStack.push(emptyMoveDown);325 doExchange=true;326 }else{327 doExchange=false;328 }329 break;330 case 2://ä¸331 if(currentEmptyY+1<difficulty){332 currentEmptyY++;333 operateStack.push(emptyMoveUp);334 doExchange=true;335 }else{336 doExchange=false;337 }338 break;339 case 3://å·¦340 if(currentEmptyX-1>=0){341 currentEmptyX--;342 operateStack.push(emptyMoveRight);343 doExchange=true;344 }else{345 doExchange=false;346 }347 break;348 case 4://å³349 if(currentEmptyX+1<difficulty){350 currentEmptyX++;351 operateStack.push(emptyMoveLeft);352 doExchange=true;353 }else{354 doExchange=false;355 }356 break;357 }358 if(doExchange){//æ§è¡äº¤æ¢359 targetPositionId=currentEmptyX+currentEmptyY*difficulty;360 tempPosition=positions[targetPositionId];361 positions[targetPositionId]=positions[emptyPositionId];362 positions[emptyPositionId]=tempPosition;363 emptyPositionId=targetPositionId;364 }365 }366 emptyBlockId=emptyPositionId;//è®°å½ç©ºåid367 return positions;368}369 370/**371 * 以ä¸å½æ°ä¸ºé®çæä½æä¾æ¯æ372 */373function emptyMoveLeft(){374 let emptyPositionObj=toObject(emptyBlock.dataset.physicalPosition);375 let operateBlock=document.getElementById("id"+((emptyPositionObj.x-1)+(emptyPositionObj.y*difficulty)));376 doExchange(operateBlock);377}378function emptyMoveRight(){379 let emptyPositionObj=toObject(emptyBlock.dataset.physicalPosition);380 let operateBlock=document.getElementById("id"+((emptyPositionObj.x+1)+(emptyPositionObj.y*difficulty)));381 doExchange(operateBlock);382}383function emptyMoveUp(){384 let emptyPositionObj=toObject(emptyBlock.dataset.physicalPosition);385 let operateBlock=document.getElementById("id"+(emptyPositionObj.x+(emptyPositionObj.y-1)*difficulty));386 doExchange(operateBlock);387}388function emptyMoveDown(){389 let emptyPositionObj=toObject(emptyBlock.dataset.physicalPosition);390 let operateBlock=document.getElementById("id"+(emptyPositionObj.x+(emptyPositionObj.y+1)*difficulty));391 doExchange(operateBlock);392}393 394function onKeyDown(event){395 let emptyPosition=toObject(emptyBlock.dataset.physicalPosition);396 if(event && event.keyCode===37){ // æ 左移397 if(emptyPosition.x+1<difficulty){398 emptyMoveRight();399 operateStack.push(emptyMoveLeft);400 }else{401 alert("ä¸è½å·¦ç§»ï¼");402 }403 }404 if(event && event.keyCode===38){ // æ ä¸ç§»405 if(emptyPosition.y+1<difficulty){
...
数组排列组合.js
Source:数组排列组合.js
2 [1, 2],3 [1, 2],4 [2, 4]5];6console.log(doExchange(arr));7// let results = [];8// doExchange(arr, 0);9// console.log(results);10// function doExchange(arr, index) {11// let result = new Array();12// for (var i = 0; i < arr[index].length; i++) {13// result[index] = arr[index][i];14// if (index != arr.length - 1) {15// doExchange(arr, index + 1);16// } else {17// console.log('result---', result, '--11', arr[index][i])18// results.push(result);19// console.log('results---', results)20// }21// }22// }23//æ¯æ¬¡è¿ç®é½æ¯èåå两个æ°ç»ï¼è¿æ ·éæ¥éå½è°ç¨24// function gerArray(dArray) {25// var len = dArray.length;26// if (len > 1) {27// var len1 = dArray[0].length, len2 = dArray[1].length, newArray = [], tempArray = [];28// for (var i = 0; i < len1; i++) {29// for (var j = 0; j < len2; j++) {30// newArray.push(dArray[0][i] + "," + dArray[1][j]);31// console.log('newArray===', newArray);32// };33// };34// tempArray.push(newArray);35// if (len > 2) {36// for (var i = 2; i < len; i++) {37// //å·²ç»å½¢æçæ°æ°ç»åå©ä¸çæ°ç»38// tempArray.push(dArray[i]);39// }40// };41// return gerArray(tempArray);//éå½éå¤è°ç¨42// } else {43// return dArray[0];//len<=1æ¯éå½çåºå£44// }45// }46/*è¿åç»åçæ°ç»*/47function doExchange(arr) {48 var len = arr.length;49 // å½æ°ç»å¤§äºçäº2个çæ¶å50 if (len >= 2) {51 // 第ä¸ä¸ªæ°ç»çé¿åº¦52 var len1 = arr[0].length;53 // 第äºä¸ªæ°ç»çé¿åº¦54 var len2 = arr[1].length;55 // 2个æ°ç»äº§ççç»åæ°56 var lenBoth = len1 * len2;57 // ç³æä¸ä¸ªæ°æ°ç»58 var items = new Array(lenBoth);59 // ç³ææ°æ°ç»çç´¢å¼60 var index = 0;61 for (var i = 0; i < len1; i++) {62 for (var j = 0; j < len2; j++) {63 if (arr[0][i] instanceof Array) {64 items[index] = arr[0][i].concat(arr[1][j]);65 } else {66 items[index] = [arr[0][i]].concat(arr[1][j]);67 }68 index++;69 }70 }71 var newArr = new Array(len - 1);72 for (var i = 2; i < arr.length; i++) {73 newArr[i - 1] = arr[i];74 }75 newArr[0] = items;76 return doExchange(newArr);77 } else {78 return arr[0];79 }...
doExchange.js
Source:doExchange.js
1// æ§è¡ç»åæåçå½æ°2function doExchange(arr){3 var len = arr.length;4 // å½æ°ç»å¤§äºçäº2个çæ¶å5 if(len >= 2){6 // 第ä¸ä¸ªæ°ç»çé¿åº¦7 var len1 = arr[0].length;8 // 第äºä¸ªæ°ç»çé¿åº¦9 var len2 = arr[1].length;10 // 2个æ°ç»äº§ççç»åæ°11 var lenBoth = len1 * len2;12 // ç³æä¸ä¸ªæ°æ°ç»,åæ°æ®æå13 var items = new Array(lenBoth);14 // ç³ææ°æ°ç»çç´¢å¼15 var index = 0;16 // 2å±åµå¥å¾ªç¯,å°ç»åæ¾å°æ°æ°ç»ä¸17 for(var i=0; i<len1; i++){18 for(var j=0; j<len2; j++){19 items[index] = arr[0][i] +"|"+ arr[1][j];20 index++;21 }22 }23 // å°æ°ç»åçæ°ç»å¹¶å°åæ°ç»ä¸24 var newArr = new Array(len -1);25 for(var i=2;i<arr.length;i++){26 newArr[i-1] = arr[i];27 }28 newArr[0] = items;29 // æ§è¡åè°30 return doExchange(newArr);31 }else{32 return arr[0];33 }34}35//æ§è¡36var array = [['a', 'b', 'c'], [1, 2, 3], ['x', 'y', 'z']];37var array1 = [['a','b'], [1], ['x', 'y', 'z']];38var array2 = [['a','b'], [1, 2, 3], ['x', 'y', 'z']];39var arr1 = [['a','b']];40var arr2 = [['a','b'],['x', 'y'], [1, 2]];41console.log(doExchange(array));42console.log(doExchange(array1));...
Using AI Code Generation
1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdef1234567890abcdef');3 if (err) return console.error(err);4 console.log('Test status: ' + data.statusText);5 wpt.getTestResults(data.data.testId, function(err, data) {6 if (err) return console.error(err);7 console.log('First View: ' + data.data.average.firstView.loadTime);8 });9});
Using AI Code Generation
1var wpt = require('wpt');2wpt.doExchange('www.google.com', function (err, data) {3 console.log(data);4});5var wpt = require('wpt');6wpt.doExchange('www.google.com', function (err, data) {7 console.log(data);8});9{ status: 'ok',10 { exchange:11 { 'www.google.com':12 { ip: '
Using AI Code Generation
1var wpt = require('wpt');2 if(err) {3 console.log('Error: '+err);4 } else {5 console.log('Response: '+res);6 }7});8var wpt = require('wpt');9 if(err) {10 console.log('Error: '+err);11 } else {12 console.log('Response: '+res);13 }14});15var wpt = require('wpt');16 var response = JSON.parse(res);17 console.log('Response: '+response);18});19var wpt = require('wpt');20 var response = JSON.parse(res);21 console.log('Response: '+response);22});23var wpt = require('wpt');24 var response = JSON.parse(res);25 console.log('Response: '+response);26});27var wpt = require('wpt');28 var response = JSON.parse(res);29 console.log('Response: '+response);30});31var wpt = require('wpt');32 var response = JSON.parse(res);33 console.log('Response: '+response);34});
Using AI Code Generation
1var wptools = require('wptools');2var wp = new wptools('Barack Obama');3wp.doExchange(function(err, data) {4 console.log(data);5});6{ title: 'Barack Obama',
Using AI Code Generation
1var wptools = require('wptools');2var wiki = new wptools('Albert Einstein');3wiki.doExchange(function(err, data) {4 console.log(data);5});6from wptools import wptools7wiki = wptools('Albert Einstein')8wiki.doExchange()9{10 "extract": "Albert Einstein (14 March 1879 – 18 April 1955) was a German-born theoretical physicist who developed the theory of relativity, one of the two pillars of modern physics (alongside quantum mechanics). Einstein's work is also known for its influence on the philosophy of science. He is best known to the general public for his mass–energy equivalence formula E = mc2 (which has been dubbed \"the world's most famous equation\"). He received the 1921 Nobel Prize in Physics \"for his services to theoretical physics, and especially for his discovery of the law of the photoelectric effect\", a pivotal step in the evolution of quantum theory.",11 "thumbnail": {12 },13 "originalimage": {14 },15 "descriptions": {
Using AI Code Generation
1var wptoolkit = require('wptoolkit');2var result = wptoolkit.doExchange(1, "USD", "EUR");3console.log(result);4var wptoolkit = require('wptoolkit');5var express = require('express');6var app = express();7app.get('/exchange', function(req, res) {8 var result = wptoolkit.doExchange(1, "USD", "EUR");9 res.send(result);10});11app.listen(3000);
Using AI Code Generation
1const wptools = require('wptools');2const fs = require('fs');3fs.readFile('test.txt', 'utf8', function(err, data) {4 if (err) throw err;5 console.log('OK: ' + 'test.txt');6 wptools.getExchange(data, function(err, response) {7 if (err) throw err;8 console.log(response);9 });10});11fs.readFile('test.txt', 'utf8', function(err, data) {12 if (err) throw err;13 console.log('OK: ' + 'test.txt');14 wptools.getExchange(data, function(err, response) {15 if (err) throw err;16 console.log(response);17 });18});19fs.readFile('test.txt', 'utf8', function(err, data) {20 if (err) throw err;21 console.log('OK: ' + 'test.txt');22 wptools.getExchange(data, function(err, response) {23 if (err) throw err;24 console.log(response);25 });26});27fs.readFile('test.txt', 'utf8', function(err, data) {28 if (err) throw err;29 console.log('OK: ' + 'test.txt');30 wptools.getExchange(data, function(err, response) {31 if (err) throw err;32 console.log(response);33 });34});35fs.readFile('test.txt', 'utf8', function(err, data) {36 if (err) throw err;37 console.log('OK: ' + 'test.txt');38 wptools.getExchange(data, function(err, response) {39 if (err) throw err;40 console.log(response);41 });42});43fs.readFile('test.txt', 'utf8', function(err, data) {44 if (err) throw err;45 console.log('OK: ' + 'test.txt');46 wptools.getExchange(data, function(err, response) {47 if (err) throw err;48 console.log(response);49 });50});
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!!