How to use doExchange method in wpt

Best JavaScript code snippet using wpt

A2.js

Source:A2.js Github

copy

Full Screen

...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){ ...

Full Screen

Full Screen

数组排列组合.js

Source:数组排列组合.js Github

copy

Full Screen

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 }...

Full Screen

Full Screen

doExchange.js

Source:doExchange.js Github

copy

Full Screen

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));...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

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});

Full Screen

Using AI Code Generation

copy

Full Screen

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: '

Full Screen

Using AI Code Generation

copy

Full Screen

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});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wptools = require('wptools');2var wp = new wptools('Barack Obama');3wp.doExchange(function(err, data) {4 console.log(data);5});6{ title: 'Barack Obama',

Full Screen

Using AI Code Generation

copy

Full Screen

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": {

Full Screen

Using AI Code Generation

copy

Full Screen

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);

Full Screen

Using AI Code Generation

copy

Full Screen

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});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2wpt.doExchange(function(err, data) {3 console.log(data);4});5var wpt = require('wpt');6wpt.doExchange('USD', function(err, data) {7 console.log(data);8});

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful