Best JavaScript code snippet using jest-extended
pictures.js
Source:pictures.js  
1import {config} from "../Config/config";2let images3let levelName,4    puzzleIndex5export default class Pictures {6    setupPictures(name, index) {7        levelName = name8        puzzleIndex = Math.round(index)9        switch (levelName) {10            case "square":11                this.setupSquarePictures()12                break;13            case "flower":14                this.setupFlowerPictures()15                break;16            case "circle":17                this.setupCirclePictures()18                break;19            case "star":20                this.setupStarPictures()21                break;22            default:23                break;24        }25    }26    getImageDirector() {27        return "img/" + levelName + "/"28    }29    getImages(){30        return images31    }32    setupInputField(levelIndex) {33        document.getElementById("inputImg").src = "img/" + config.levels[levelIndex].levelName + "/exp.png"34        var uagent = navigator.userAgent.toLowerCase()35        if(uagent.search("android") > -1 || uagent.search("apple") > -1 && uagent.search("apple") != 42){36            document.getElementById(("inputImg")).width = config.levels[levelIndex].phone.expWidth37            document.getElementById(("inputImg")).height = config.levels[levelIndex].phone.expHeight38            document.getElementById("button1").coords = config.levels[levelIndex].phone.button139            document.getElementById("button2").coords = config.levels[levelIndex].phone.button240            document.getElementById("button3").coords = config.levels[levelIndex].phone.button341            document.getElementById("button4").coords = config.levels[levelIndex].phone.button442            document.getElementById("button5").coords = config.levels[levelIndex].phone.button543            document.getElementById("button6").coords = config.levels[levelIndex].phone.button644        }45        else {46            document.getElementById(("inputImg")).width = config.levels[levelIndex].computer.expWidth47            document.getElementById(("inputImg")).height = config.levels[levelIndex].computer.expHeight48            document.getElementById("button1").coords = config.levels[levelIndex].computer.button149            document.getElementById("button2").coords = config.levels[levelIndex].computer.button250            document.getElementById("button3").coords = config.levels[levelIndex].computer.button351            document.getElementById("button4").coords = config.levels[levelIndex].computer.button452            document.getElementById("button5").coords = config.levels[levelIndex].computer.button553            document.getElementById("button6").coords = config.levels[levelIndex].computer.button654        }55    }56    setupSquarePictures() {57        images = new Array();58        var arr = new Array();59        arr.push('0-4.gif');60        arr.push('0-4.gif');61        images[0] = arr;62        var arr = new Array();63        arr.push('1-2.gif');64        arr.push('1-2.gif');65        images[1] = arr;66        var arr = new Array();67        arr.push('10-2.gif');68        arr.push('10-2.gif');69        images[10] = arr;70        var arr = new Array();71        arr.push('11-3.gif');72        arr.push('11-5.gif');73        images[11] = arr;74        var arr = new Array();75        arr.push('12-4.gif');76        arr.push('12-4.gif');77        images[12] = arr;78        var arr = new Array();79        arr.push('13-2.gif');80        arr.push('13-2.gif');81        images[13] = arr;82        var arr = new Array();83        arr.push('14-3.gif');84        arr.push('14-3.gif');85        images[14] = arr;86        var arr = new Array();87        arr.push('15-2.gif');88        arr.push('15-2.gif');89        images[15] = arr;90        var arr = new Array();91        arr.push('16-4.gif');92        arr.push('16-4.gif');93        images[16] = arr;94        var arr = new Array();95        arr.push('17-1.gif');96        arr.push('17-1.gif');97        images[17] = arr;98        var arr = new Array();99        arr.push('18-3.gif');100        arr.push('18-3.gif');101        images[18] = arr;102        var arr = new Array();103        arr.push('19-2.gif');104        arr.push('19-2.gif');105        images[19] = arr;106        var arr = new Array();107        arr.push('2-3.gif');108        arr.push('2-3.gif');109        images[2] = arr;110        var arr = new Array();111        arr.push('20-4.gif');112        arr.push('20-4.gif');113        images[20] = arr;114        var arr = new Array();115        arr.push('21-1.gif');116        arr.push('21-1.gif');117        images[21] = arr;118        var arr = new Array();119        arr.push('22-3.gif');120        arr.push('22-3.gif');121        images[22] = arr;122        var arr = new Array();123        arr.push('23-2.gif');124        arr.push('23-2.gif');125        images[23] = arr;126        var arr = new Array();127        arr.push('24-4.gif');128        arr.push('24-4.gif');129        images[24] = arr;130        var arr = new Array();131        arr.push('25-1.gif');132        arr.push('25-1.gif');133        images[25] = arr;134        var arr = new Array();135        arr.push('26-2.gif');136        arr.push('26-2.gif');137        images[26] = arr;138        var arr = new Array();139        arr.push('27-3.gif');140        arr.push('27-3.gif');141        images[27] = arr;142        var arr = new Array();143        arr.push('28-4.gif');144        arr.push('28-4.gif');145        images[28] = arr;146        var arr = new Array();147        arr.push('29-2.gif');148        arr.push('29-2.gif');149        images[29] = arr;150        var arr = new Array();151        arr.push('3-1.gif');152        arr.push('3-1.gif');153        images[3] = arr;154        var arr = new Array();155        arr.push('30-3.gif');156        arr.push('30-3.gif');157        images[30] = arr;158        var arr = new Array();159        arr.push('31-2.gif');160        arr.push('31-2.gif');161        images[31] = arr;162        var arr = new Array();163        arr.push('32-3.gif');164        arr.push('32-3.gif');165        images[32] = arr;166        var arr = new Array();167        arr.push('33-4.gif');168        arr.push('33-4.gif');169        images[33] = arr;170        var arr = new Array();171        arr.push('34-1.gif');172        arr.push('34-1.gif');173        images[34] = arr;174        var arr = new Array();175        arr.push('35-3.gif');176        arr.push('35-3.gif');177        images[35] = arr;178        var arr = new Array();179        arr.push('36-2.gif');180        arr.push('36-2.gif');181        images[36] = arr;182        var arr = new Array();183        arr.push('37-1.gif');184        arr.push('37-1.gif');185        images[37] = arr;186        var arr = new Array();187        arr.push('38-1.gif');188        arr.push('38-5.gif');189        images[38] = arr;190        var arr = new Array();191        arr.push('39-2.gif');192        arr.push('39-6.gif');193        images[39] = arr;194        var arr = new Array();195        arr.push('4-4.gif');196        arr.push('4-4.gif');197        images[4] = arr;198        var arr = new Array();199        arr.push('40-4.gif');200        arr.push('40-4.gif');201        images[40] = arr;202        var arr = new Array();203        arr.push('5-1.gif');204        arr.push('5-1.gif');205        images[5] = arr;206        var arr = new Array();207        arr.push('6-3.gif');208        arr.push('6-3.gif');209        images[6] = arr;210        var arr = new Array();211        arr.push('7-2.gif');212        arr.push('7-2.gif');213        images[7] = arr;214        var arr = new Array();215        arr.push('8-4.gif');216        arr.push('8-4.gif');217        images[8] = arr;218        var arr = new Array();219        arr.push('9-1.gif');220        arr.push('9-1.gif');221        images[9] = arr;222        var arr = new Array();223        arr.push('exp.gif');224        var img = document.getElementById("img3D");225        var rand = parseInt((Math.random() * 2));226        img.src = this.getImageDirector() + images[puzzleIndex][rand];227    }228    setupFlowerPictures() {229        images = new Array();230        var arr = new Array();231        arr.push('0-4.gif');232        arr.push('0-6.gif');233        images[0] = arr;234        var arr = new Array();235        arr.push('1-2.gif');236        arr.push('1-3.gif');237        images[1] = arr;238        var arr = new Array();239        arr.push('10-2.gif');240        arr.push('10-4.gif');241        images[10] = arr;242        var arr = new Array();243        arr.push('11-3.gif');244        arr.push('11-3.gif');245        images[11] = arr;246        var arr = new Array();247        arr.push('12-2.gif');248        arr.push('12-4.gif');249        images[12] = arr;250        var arr = new Array();251        arr.push('13-1.gif');252        arr.push('13-6.gif');253        images[13] = arr;254        var arr = new Array();255        arr.push('14-4.gif');256        arr.push('14-5.gif');257        images[14] = arr;258        var arr = new Array();259        arr.push('15-1.gif');260        arr.push('15-3.gif');261        images[15] = arr;262        var arr = new Array();263        arr.push('16-4.gif');264        arr.push('16-6.gif');265        images[16] = arr;266        var arr = new Array();267        arr.push('17-2.gif');268        arr.push('17-3.gif');269        images[17] = arr;270        var arr = new Array();271        arr.push('18-5.gif');272        arr.push('18-6.gif');273        images[18] = arr;274        var arr = new Array();275        arr.push('19-1.gif');276        arr.push('19-3.gif');277        images[19] = arr;278        var arr = new Array();279        arr.push('2-5.gif');280        arr.push('2-6.gif');281        images[2] = arr;282        var arr = new Array();283        arr.push('20-4.gif');284        arr.push('20-6.gif');285        images[20] = arr;286        var arr = new Array();287        arr.push('21-2.gif');288        arr.push('21-6.gif');289        images[21] = arr;290        var arr = new Array();291        arr.push('22-3.gif');292        arr.push('22-5.gif');293        images[22] = arr;294        var arr = new Array();295        arr.push('23-1.gif');296        arr.push('23-2.gif');297        images[23] = arr;298        var arr = new Array();299        arr.push('24-3.gif');300        arr.push('24-4.gif');301        images[24] = arr;302        var arr = new Array();303        arr.push('25-2.gif');304        arr.push('25-6.gif');305        images[25] = arr;306        var arr = new Array();307        arr.push('26-1.gif');308        arr.push('26-4.gif');309        images[26] = arr;310        var arr = new Array();311        arr.push('27-3.gif');312        arr.push('27-5.gif');313        images[27] = arr;314        var arr = new Array();315        arr.push('28-2.gif');316        arr.push('28-4.gif');317        images[28] = arr;318        var arr = new Array();319        arr.push('29-1.gif');320        arr.push('29-6.gif');321        images[29] = arr;322        var arr = new Array();323        arr.push('3-1.gif');324        arr.push('3-3.gif');325        images[3] = arr;326        var arr = new Array();327        arr.push('30-4.gif');328        arr.push('30-5.gif');329        images[30] = arr;330        var arr = new Array();331        arr.push('31-1.gif');332        arr.push('31-3.gif');333        images[31] = arr;334        var arr = new Array();335        arr.push('32-3.gif');336        arr.push('32-5.gif');337        images[32] = arr;338        var arr = new Array();339        arr.push('33-1.gif');340        arr.push('33-4.gif');341        images[33] = arr;342        var arr = new Array();343        arr.push('34-3.gif');344        arr.push('34-6.gif');345        images[34] = arr;346        var arr = new Array();347        arr.push('35-4.gif');348        arr.push('35-5.gif');349        images[35] = arr;350        var arr = new Array();351        arr.push('36-1.gif');352        arr.push('36-2.gif');353        images[36] = arr;354        var arr = new Array();355        arr.push('37-2.gif');356        arr.push('37-3.gif');357        images[37] = arr;358        var arr = new Array();359        arr.push('4-4.gif');360        arr.push('4-6.gif');361        images[4] = arr;362        var arr = new Array();363        arr.push('5-2.gif');364        arr.push('5-6.gif');365        images[5] = arr;366        var arr = new Array();367        arr.push('6-3.gif');368        arr.push('6-5.gif');369        images[6] = arr;370        var arr = new Array();371        arr.push('7-1.gif');372        arr.push('7-2.gif');373        images[7] = arr;374        var arr = new Array();375        arr.push('8-3.gif');376        arr.push('8-4.gif');377        images[8] = arr;378        var arr = new Array();379        arr.push('9-2.gif');380        arr.push('9-6.gif');381        images[9] = arr;382        var arr = new Array();383        arr.push('exp.gif');384        var img = document.getElementById("img3D");385        var rand = parseInt((Math.random() * 2));386        img.src = this.getImageDirector() + images[puzzleIndex][rand];387    }388    setupCirclePictures() {389        images = new Array();390        var arr = new Array();391        arr.push('0-4.gif');392        arr.push('0-6.gif');393        images[0] = arr;394        var arr = new Array();395        arr.push('1-2.gif');396        arr.push('1-3.gif');397        images[1] = arr;398        var arr = new Array();399        arr.push('10-1.gif');400        arr.push('10-4.gif');401        images[10] = arr;402        var arr = new Array();403        arr.push('11-5.gif');404        arr.push('11-5.gif');405        images[11] = arr;406        var arr = new Array();407        arr.push('12-2.gif');408        arr.push('12-4.gif');409        images[12] = arr;410        var arr = new Array();411        arr.push('13-6.gif');412        arr.push('13-6.gif');413        images[13] = arr;414        var arr = new Array();415        arr.push('14-4.gif');416        arr.push('14-5.gif');417        images[14] = arr;418        var arr = new Array();419        arr.push('15-1.gif');420        arr.push('15-3.gif');421        images[15] = arr;422        var arr = new Array();423        arr.push('16-4.gif');424        arr.push('16-6.gif');425        images[16] = arr;426        var arr = new Array();427        arr.push('17-3.gif');428        arr.push('17-3.gif');429        images[17] = arr;430        var arr = new Array();431        arr.push('18-5.gif');432        arr.push('18-6.gif');433        images[18] = arr;434        var arr = new Array();435        arr.push('19-1.gif');436        arr.push('19-3.gif');437        images[19] = arr;438        var arr = new Array();439        arr.push('2-5.gif');440        arr.push('2-6.gif');441        images[2] = arr;442        var arr = new Array();443        arr.push('20-4.gif');444        arr.push('20-6.gif');445        images[20] = arr;446        var arr = new Array();447        arr.push('21-2.gif');448        arr.push('21-6.gif');449        images[21] = arr;450        var arr = new Array();451        arr.push('22-3.gif');452        arr.push('22-3.gif');453        images[22] = arr;454        var arr = new Array();455        arr.push('23-1.gif');456        arr.push('23-2.gif');457        images[23] = arr;458        var arr = new Array();459        arr.push('24-3.gif');460        arr.push('24-4.gif');461        images[24] = arr;462        var arr = new Array();463        arr.push('25-2.gif');464        arr.push('25-6.gif');465        images[25] = arr;466        var arr = new Array();467        arr.push('26-1.gif');468        arr.push('26-1.gif');469        images[26] = arr;470        var arr = new Array();471        arr.push('27-3.gif');472        arr.push('27-5.gif');473        images[27] = arr;474        var arr = new Array();475        arr.push('28-2.gif');476        arr.push('28-4.gif');477        images[28] = arr;478        var arr = new Array();479        arr.push('29-1.gif');480        arr.push('29-6.gif');481        images[29] = arr;482        var arr = new Array();483        arr.push('3-1.gif');484        arr.push('3-3.gif');485        images[3] = arr;486        var arr = new Array();487        arr.push('30-4.gif');488        arr.push('30-5.gif');489        images[30] = arr;490        var arr = new Array();491        arr.push('31-1.gif');492        arr.push('31-3.gif');493        images[31] = arr;494        var arr = new Array();495        arr.push('32-3.gif');496        arr.push('32-5.gif');497        images[32] = arr;498        var arr = new Array();499        arr.push('33-1.gif');500        arr.push('33-4.gif');501        images[33] = arr;502        var arr = new Array();503        arr.push('34-3.gif');504        arr.push('34-6.gif');505        images[34] = arr;506        var arr = new Array();507        arr.push('35-4.gif');508        arr.push('35-4.gif');509        images[35] = arr;510        var arr = new Array();511        arr.push('36-1.gif');512        arr.push('36-2.gif');513        images[36] = arr;514        var arr = new Array();515        arr.push('37-2.gif');516        arr.push('37-2.gif');517        images[37] = arr;518        var arr = new Array();519        arr.push('38-1.gif');520        arr.push('38-5.gif');521        images[38] = arr;522        var arr = new Array();523        arr.push('4-4.gif');524        arr.push('4-6.gif');525        images[4] = arr;526        var arr = new Array();527        arr.push('5-2.gif');528        arr.push('5-6.gif');529        images[5] = arr;530        var arr = new Array();531        arr.push('6-3.gif');532        arr.push('6-5.gif');533        images[6] = arr;534        var arr = new Array();535        arr.push('7-1.gif');536        arr.push('7-2.gif');537        images[7] = arr;538        var arr = new Array();539        arr.push('8-3.gif');540        arr.push('8-4.gif');541        images[8] = arr;542        var arr = new Array();543        arr.push('9-2.gif');544        arr.push('9-2.gif');545        images[9] = arr;546        var arr = new Array();547        arr.push('exp.gif');548        var img = document.getElementById("img3D");549        var rand = parseInt((Math.random() * 2));550        img.src = this.getImageDirector() + images[puzzleIndex][rand];551    }552    setupStarPictures() {553        images = new Array();554        var arr = new Array();555        arr.push('0-4.gif');556        arr.push('0-6.gif');557        images[0] = arr;558        var arr = new Array();559        arr.push('1-2.gif');560        arr.push('1-3.gif');561        images[1] = arr;562        var arr = new Array();563        arr.push('10-1.gif');564        arr.push('10-4.gif');565        images[10] = arr;566        var arr = new Array();567        arr.push('11-3.gif');568        arr.push('11-5.gif');569        images[11] = arr;570        var arr = new Array();571        arr.push('12-2.gif');572        arr.push('12-4.gif');573        images[12] = arr;574        var arr = new Array();575        arr.push('13-1.gif');576        arr.push('13-6.gif');577        images[13] = arr;578        var arr = new Array();579        arr.push('14-4.gif');580        arr.push('14-5.gif');581        images[14] = arr;582        var arr = new Array();583        arr.push('15-1.gif');584        arr.push('15-3.gif');585        images[15] = arr;586        var arr = new Array();587        arr.push('16-4.gif');588        arr.push('16-6.gif');589        images[16] = arr;590        var arr = new Array();591        arr.push('17-2.gif');592        arr.push('17-3.gif');593        images[17] = arr;594        var arr = new Array();595        arr.push('18-5.gif');596        arr.push('18-6.gif');597        images[18] = arr;598        var arr = new Array();599        arr.push('19-1.gif');600        arr.push('19-3.gif');601        images[19] = arr;602        var arr = new Array();603        arr.push('2-5.gif');604        arr.push('2-6.gif');605        images[2] = arr;606        var arr = new Array();607        arr.push('20-4.gif');608        arr.push('20-6.gif');609        images[20] = arr;610        var arr = new Array();611        arr.push('21-2.gif');612        arr.push('21-6.gif');613        images[21] = arr;614        var arr = new Array();615        arr.push('22-3.gif');616        arr.push('22-5.gif');617        images[22] = arr;618        var arr = new Array();619        arr.push('23-1.gif');620        arr.push('23-2.gif');621        images[23] = arr;622        var arr = new Array();623        arr.push('24-3.gif');624        arr.push('24-4.gif');625        images[24] = arr;626        var arr = new Array();627        arr.push('25-2.gif');628        arr.push('25-6.gif');629        images[25] = arr;630        var arr = new Array();631        arr.push('26-1.gif');632        arr.push('26-4.gif');633        images[26] = arr;634        var arr = new Array();635        arr.push('27-3.gif');636        arr.push('27-5.gif');637        images[27] = arr;638        var arr = new Array();639        arr.push('28-2.gif');640        arr.push('28-4.gif');641        images[28] = arr;642        var arr = new Array();643        arr.push('29-1.gif');644        arr.push('29-6.gif');645        images[29] = arr;646        var arr = new Array();647        arr.push('3-1.gif');648        arr.push('3-3.gif');649        images[3] = arr;650        var arr = new Array();651        arr.push('30-4.gif');652        arr.push('30-5.gif');653        images[30] = arr;654        var arr = new Array();655        arr.push('31-1.gif');656        arr.push('31-3.gif');657        images[31] = arr;658        var arr = new Array();659        arr.push('32-3.gif');660        arr.push('32-5.gif');661        images[32] = arr;662        var arr = new Array();663        arr.push('33-1.gif');664        arr.push('33-4.gif');665        images[33] = arr;666        var arr = new Array();667        arr.push('34-3.gif');668        arr.push('34-6.gif');669        images[34] = arr;670        var arr = new Array();671        arr.push('35-4.gif');672        arr.push('35-5.gif');673        images[35] = arr;674        var arr = new Array();675        arr.push('36-1.gif');676        arr.push('36-2.gif');677        images[36] = arr;678        var arr = new Array();679        arr.push('37-2.gif');680        arr.push('37-3.gif');681        images[37] = arr;682        var arr = new Array();683        arr.push('38-1.gif');684        arr.push('38-5.gif');685        images[38] = arr;686        var arr = new Array();687        arr.push('39-2.gif');688        arr.push('39-6.gif');689        images[39] = arr;690        var arr = new Array();691        arr.push('40-4.gif');692        arr.push('40-6.gif');693        images[40] = arr;694        var arr = new Array();695        arr.push('4-4.gif');696        arr.push('4-6.gif');697        images[4] = arr;698        var arr = new Array();699        arr.push('5-2.gif');700        arr.push('5-6.gif');701        images[5] = arr;702        var arr = new Array();703        arr.push('6-3.gif');704        arr.push('6-5.gif');705        images[6] = arr;706        var arr = new Array();707        arr.push('7-1.gif');708        arr.push('7-2.gif');709        images[7] = arr;710        var arr = new Array();711        arr.push('8-3.gif');712        arr.push('8-4.gif');713        images[8] = arr;714        var arr = new Array();715        arr.push('9-2.gif');716        arr.push('9-6.gif');717        images[9] = arr;718        var arr = new Array();719        arr.push('exp.gif');720        var img = document.getElementById("img3D");721        var rand = parseInt((Math.random() * 2));722        img.src = this.getImageDirector() + images[puzzleIndex][rand];723    }...function-node.js
Source:function-node.js  
1const { FunctionNode } = require('../function-node');2/**3 * @desc [INTERNAL] Represents a single function, inside JS4 *5 * <p>This handles all the raw state, converted state, etc. Of a single function.</p>6 */7class CPUFunctionNode extends FunctionNode {8  /**9   * @desc Parses the abstract syntax tree for to its *named function*10   * @param {Object} ast - the AST object to parse11   * @param {Array} retArr - return array string12   * @returns {Array} the append retArr13   */14  astFunction(ast, retArr) {15    // Setup function return type and name16    if (!this.isRootKernel) {17      retArr.push('function');18      retArr.push(' ');19      retArr.push(this.name);20      retArr.push('(');21      // Arguments handling22      for (let i = 0; i < this.argumentNames.length; ++i) {23        const argumentName = this.argumentNames[i];24        if (i > 0) {25          retArr.push(', ');26        }27        retArr.push('user_');28        retArr.push(argumentName);29      }30      // Function opening31      retArr.push(') {\n');32    }33    // Body statement iteration34    for (let i = 0; i < ast.body.body.length; ++i) {35      this.astGeneric(ast.body.body[i], retArr);36      retArr.push('\n');37    }38    if (!this.isRootKernel) {39      // Function closing40      retArr.push('}\n');41    }42    return retArr;43  }44  /**45   * @desc Parses the abstract syntax tree for to *return* statement46   * @param {Object} ast - the AST object to parse47   * @param {Array} retArr - return array string48   * @returns {Array} the append retArr49   */50  astReturnStatement(ast, retArr) {51    const type = this.returnType || this.getType(ast.argument);52    if (!this.returnType) {53      this.returnType = type;54    }55    if (this.isRootKernel) {56      retArr.push(this.leadingReturnStatement);57      this.astGeneric(ast.argument, retArr);58      retArr.push(';\n');59      retArr.push(this.followingReturnStatement);60      retArr.push('continue;\n');61    } else if (this.isSubKernel) {62      retArr.push(`subKernelResult_${ this.name } = `);63      this.astGeneric(ast.argument, retArr);64      retArr.push(';');65      retArr.push(`return subKernelResult_${ this.name };`);66    } else {67      retArr.push('return ');68      this.astGeneric(ast.argument, retArr);69      retArr.push(';');70    }71    return retArr;72  }73  /**74   * @desc Parses the abstract syntax tree for *literal value*75   * @param {Object} ast - the AST object to parse76   * @param {Array} retArr - return array string77   * @returns {Array} the append retArr78   */79  astLiteral(ast, retArr) {80    // Reject non numeric literals81    if (isNaN(ast.value)) {82      throw this.astErrorOutput(83        'Non-numeric literal not supported : ' + ast.value,84        ast85      );86    }87    retArr.push(ast.value);88    return retArr;89  }90  /**91   * @desc Parses the abstract syntax tree for *binary* expression92   * @param {Object} ast - the AST object to parse93   * @param {Array} retArr - return array string94   * @returns {Array} the append retArr95   */96  astBinaryExpression(ast, retArr) {97    retArr.push('(');98    this.astGeneric(ast.left, retArr);99    retArr.push(ast.operator);100    this.astGeneric(ast.right, retArr);101    retArr.push(')');102    return retArr;103  }104  /**105   * @desc Parses the abstract syntax tree for *identifier* expression106   * @param {Object} idtNode - An ast Node107   * @param {Array} retArr - return array string108   * @returns {Array} the append retArr109   */110  astIdentifierExpression(idtNode, retArr) {111    if (idtNode.type !== 'Identifier') {112      throw this.astErrorOutput(113        'IdentifierExpression - not an Identifier',114        idtNode115      );116    }117    switch (idtNode.name) {118      case 'Infinity':119        retArr.push('Infinity');120        break;121      default:122        if (this.constants && this.constants.hasOwnProperty(idtNode.name)) {123          retArr.push('constants_' + idtNode.name);124        } else {125          retArr.push('user_' + idtNode.name);126        }127    }128    return retArr;129  }130  /**131   * @desc Parses the abstract syntax tree for *for-loop* expression132   * @param {Object} forNode - An ast Node133   * @param {Array} retArr - return array string134   * @returns {Array} the parsed webgl string135   */136  astForStatement(forNode, retArr) {137    if (forNode.type !== 'ForStatement') {138      throw this.astErrorOutput('Invalid for statement', forNode);139    }140    const initArr = [];141    const testArr = [];142    const updateArr = [];143    const bodyArr = [];144    let isSafe = null;145    if (forNode.init) {146      this.pushState('in-for-loop-init');147      this.astGeneric(forNode.init, initArr);148      for (let i = 0; i < initArr.length; i++) {149        if (initArr[i].includes && initArr[i].includes(',')) {150          isSafe = false;151        }152      }153      this.popState('in-for-loop-init');154    } else {155      isSafe = false;156    }157    if (forNode.test) {158      this.astGeneric(forNode.test, testArr);159    } else {160      isSafe = false;161    }162    if (forNode.update) {163      this.astGeneric(forNode.update, updateArr);164    } else {165      isSafe = false;166    }167    if (forNode.body) {168      this.pushState('loop-body');169      this.astGeneric(forNode.body, bodyArr);170      this.popState('loop-body');171    }172    // have all parts, now make them safe173    if (isSafe === null) {174      isSafe = this.isSafe(forNode.init) && this.isSafe(forNode.test);175    }176    if (isSafe) {177      retArr.push(`for (${initArr.join('')};${testArr.join('')};${updateArr.join('')}){\n`);178      retArr.push(bodyArr.join(''));179      retArr.push('}\n');180    } else {181      const iVariableName = this.getInternalVariableName('safeI');182      if (initArr.length > 0) {183        retArr.push(initArr.join(''), ';\n');184      }185      retArr.push(`for (let ${iVariableName}=0;${iVariableName}<LOOP_MAX;${iVariableName}++){\n`);186      if (testArr.length > 0) {187        retArr.push(`if (!${testArr.join('')}) break;\n`);188      }189      retArr.push(bodyArr.join(''));190      retArr.push(`\n${updateArr.join('')};`);191      retArr.push('}\n');192    }193    return retArr;194  }195  /**196   * @desc Parses the abstract syntax tree for *while* loop197   * @param {Object} whileNode - An ast Node198   * @param {Array} retArr - return array string199   * @returns {Array} the parsed javascript string200   */201  astWhileStatement(whileNode, retArr) {202    if (whileNode.type !== 'WhileStatement') {203      throw this.astErrorOutput(204        'Invalid while statement',205        whileNode206      );207    }208    retArr.push('for (let i = 0; i < LOOP_MAX; i++) {');209    retArr.push('if (');210    this.astGeneric(whileNode.test, retArr);211    retArr.push(') {\n');212    this.astGeneric(whileNode.body, retArr);213    retArr.push('} else {\n');214    retArr.push('break;\n');215    retArr.push('}\n');216    retArr.push('}\n');217    return retArr;218  }219  /**220   * @desc Parses the abstract syntax tree for *do while* loop221   * @param {Object} doWhileNode - An ast Node222   * @param {Array} retArr - return array string223   * @returns {Array} the parsed webgl string224   */225  astDoWhileStatement(doWhileNode, retArr) {226    if (doWhileNode.type !== 'DoWhileStatement') {227      throw this.astErrorOutput(228        'Invalid while statement',229        doWhileNode230      );231    }232    retArr.push('for (let i = 0; i < LOOP_MAX; i++) {');233    this.astGeneric(doWhileNode.body, retArr);234    retArr.push('if (!');235    this.astGeneric(doWhileNode.test, retArr);236    retArr.push(') {\n');237    retArr.push('break;\n');238    retArr.push('}\n');239    retArr.push('}\n');240    return retArr;241  }242  /**243   * @desc Parses the abstract syntax tree for *Assignment* Expression244   * @param {Object} assNode - An ast Node245   * @param {Array} retArr - return array string246   * @returns {Array} the append retArr247   */248  astAssignmentExpression(assNode, retArr) {249    const declaration = this.getDeclaration(assNode.left);250    if (declaration && !declaration.assignable) {251      throw this.astErrorOutput(`Variable ${assNode.left.name} is not assignable here`, assNode);252    }253    this.astGeneric(assNode.left, retArr);254    retArr.push(assNode.operator);255    this.astGeneric(assNode.right, retArr);256    return retArr;257  }258  /**259   * @desc Parses the abstract syntax tree for *Block* statement260   * @param {Object} bNode - the AST object to parse261   * @param {Array} retArr - return array string262   * @returns {Array} the append retArr263   */264  astBlockStatement(bNode, retArr) {265    if (this.isState('loop-body')) {266      this.pushState('block-body'); // this prevents recursive removal of braces267      for (let i = 0; i < bNode.body.length; i++) {268        this.astGeneric(bNode.body[i], retArr);269      }270      this.popState('block-body');271    } else {272      retArr.push('{\n');273      for (let i = 0; i < bNode.body.length; i++) {274        this.astGeneric(bNode.body[i], retArr);275      }276      retArr.push('}\n');277    }278    return retArr;279  }280  /**281   * @desc Parses the abstract syntax tree for *Variable Declaration*282   * @param {Object} varDecNode - An ast Node283   * @param {Array} retArr - return array string284   * @returns {Array} the append retArr285   */286  astVariableDeclaration(varDecNode, retArr) {287    retArr.push(`${varDecNode.kind} `);288    const { declarations } = varDecNode;289    for (let i = 0; i < declarations.length; i++) {290      if (i > 0) {291        retArr.push(',');292      }293      const declaration = declarations[i];294      const info = this.getDeclaration(declaration.id);295      if (!info.valueType) {296        info.valueType = this.getType(declaration.init);297      }298      this.astGeneric(declaration, retArr);299    }300    if (!this.isState('in-for-loop-init')) {301      retArr.push(';');302    }303    return retArr;304  }305  /**306   * @desc Parses the abstract syntax tree for *If* Statement307   * @param {Object} ifNode - An ast Node308   * @param {Array} retArr - return array string309   * @returns {Array} the append retArr310   */311  astIfStatement(ifNode, retArr) {312    retArr.push('if (');313    this.astGeneric(ifNode.test, retArr);314    retArr.push(')');315    if (ifNode.consequent.type === 'BlockStatement') {316      this.astGeneric(ifNode.consequent, retArr);317    } else {318      retArr.push(' {\n');319      this.astGeneric(ifNode.consequent, retArr);320      retArr.push('\n}\n');321    }322    if (ifNode.alternate) {323      retArr.push('else ');324      if (ifNode.alternate.type === 'BlockStatement' || ifNode.alternate.type === 'IfStatement') {325        this.astGeneric(ifNode.alternate, retArr);326      } else {327        retArr.push(' {\n');328        this.astGeneric(ifNode.alternate, retArr);329        retArr.push('\n}\n');330      }331    }332    return retArr;333  }334  astSwitchStatement(ast, retArr) {335    const { discriminant, cases } = ast;336    retArr.push('switch (');337    this.astGeneric(discriminant, retArr);338    retArr.push(') {\n');339    for (let i = 0; i < cases.length; i++) {340      if (cases[i].test === null) {341        retArr.push('default:\n');342        this.astGeneric(cases[i].consequent, retArr);343        if (cases[i].consequent && cases[i].consequent.length > 0) {344          retArr.push('break;\n');345        }346        continue;347      }348      retArr.push('case ');349      this.astGeneric(cases[i].test, retArr);350      retArr.push(':\n');351      if (cases[i].consequent && cases[i].consequent.length > 0) {352        this.astGeneric(cases[i].consequent, retArr);353        retArr.push('break;\n');354      }355    }356    retArr.push('\n}');357  }358  /**359   * @desc Parses the abstract syntax tree for *This* expression360   * @param {Object} tNode - An ast Node361   * @param {Array} retArr - return array string362   * @returns {Array} the append retArr363   */364  astThisExpression(tNode, retArr) {365    retArr.push('_this');366    return retArr;367  }368  /**369   * @desc Parses the abstract syntax tree for *Member* Expression370   * @param {Object} mNode - An ast Node371   * @param {Array} retArr - return array string372   * @returns {Array} the append retArr373   */374  astMemberExpression(mNode, retArr) {375    const {376      signature,377      type,378      property,379      xProperty,380      yProperty,381      zProperty,382      name,383      origin384    } = this.getMemberExpressionDetails(mNode);385    switch (signature) {386      case 'this.thread.value':387        retArr.push(`_this.thread.${ name }`);388        return retArr;389      case 'this.output.value':390        switch (name) {391          case 'x':392            retArr.push('outputX');393            break;394          case 'y':395            retArr.push('outputY');396            break;397          case 'z':398            retArr.push('outputZ');399            break;400          default:401            throw this.astErrorOutput('Unexpected expression', mNode);402        }403        return retArr;404      case 'value':405        throw this.astErrorOutput('Unexpected expression', mNode);406      case 'value[]':407      case 'value[][]':408      case 'value[][][]':409      case 'value.value':410        if (origin === 'Math') {411          retArr.push(Math[name]);412          return retArr;413        }414        switch (property) {415          case 'r':416            retArr.push(`user_${ name }[0]`);417            return retArr;418          case 'g':419            retArr.push(`user_${ name }[1]`);420            return retArr;421          case 'b':422            retArr.push(`user_${ name }[2]`);423            return retArr;424          case 'a':425            retArr.push(`user_${ name }[3]`);426            return retArr;427        }428        break;429      case 'this.constants.value':430      case 'this.constants.value[]':431      case 'this.constants.value[][]':432      case 'this.constants.value[][][]':433        break;434      case 'fn()[]':435        this.astGeneric(mNode.object, retArr);436        retArr.push('[');437        this.astGeneric(mNode.property, retArr);438        retArr.push(']');439        return retArr;440      case 'fn()[][]':441        this.astGeneric(mNode.object.object, retArr);442        retArr.push('[');443        this.astGeneric(mNode.object.property, retArr);444        retArr.push(']');445        retArr.push('[');446        this.astGeneric(mNode.property, retArr);447        retArr.push(']');448        return retArr;449      default:450        throw this.astErrorOutput('Unexpected expression', mNode);451    }452    if (!mNode.computed) {453      // handle simple types454      switch (type) {455        case 'Number':456        case 'Integer':457        case 'Float':458        case 'Boolean':459          retArr.push(`${origin}_${name}`);460          return retArr;461      }462    }463    // handle more complex types464    // argument may have come from a parent465    const markupName = `${origin}_${name}`;466    switch (type) {467      case 'Array(2)':468      case 'Array(3)':469      case 'Array(4)':470      case 'Matrix(2)':471      case 'Matrix(3)':472      case 'Matrix(4)':473      case 'HTMLImageArray':474      case 'ArrayTexture(1)':475      case 'ArrayTexture(2)':476      case 'ArrayTexture(3)':477      case 'ArrayTexture(4)':478      case 'HTMLImage':479      default:480        let size;481        let isInput;482        if (origin === 'constants') {483          const constant = this.constants[name];484          isInput = this.constantTypes[name] === 'Input';485          size = isInput ? constant.size : null;486        } else {487          isInput = this.isInput(name);488          size = isInput ? this.argumentSizes[this.argumentNames.indexOf(name)] : null;489        }490        retArr.push(`${ markupName }`);491        if (zProperty && yProperty) {492          if (isInput) {493            retArr.push('[(');494            this.astGeneric(zProperty, retArr);495            retArr.push(`*${ this.dynamicArguments ? '(outputY * outputX)' : size[1] * size[0] })+(`);496            this.astGeneric(yProperty, retArr);497            retArr.push(`*${ this.dynamicArguments ? 'outputX' : size[0] })+`);498            this.astGeneric(xProperty, retArr);499            retArr.push(']');500          } else {501            retArr.push('[');502            this.astGeneric(zProperty, retArr);503            retArr.push(']');504            retArr.push('[');505            this.astGeneric(yProperty, retArr);506            retArr.push(']');507            retArr.push('[');508            this.astGeneric(xProperty, retArr);509            retArr.push(']');510          }511        } else if (yProperty) {512          if (isInput) {513            retArr.push('[(');514            this.astGeneric(yProperty, retArr);515            retArr.push(`*${ this.dynamicArguments ? 'outputX' : size[0] })+`);516            this.astGeneric(xProperty, retArr);517            retArr.push(']');518          } else {519            retArr.push('[');520            this.astGeneric(yProperty, retArr);521            retArr.push(']');522            retArr.push('[');523            this.astGeneric(xProperty, retArr);524            retArr.push(']');525          }526        } else if (typeof xProperty !== 'undefined') {527          retArr.push('[');528          this.astGeneric(xProperty, retArr);529          retArr.push(']');530        }531    }532    return retArr;533  }534  /**535   * @desc Parses the abstract syntax tree for *call* expression536   * @param {Object} ast - the AST object to parse537   * @param {Array} retArr - return array string538   * @returns  {Array} the append retArr539   */540  astCallExpression(ast, retArr) {541    if (ast.type !== 'CallExpression') {542      // Failure, unknown expression543      throw this.astErrorOutput('Unknown CallExpression', ast);544    }545    // Get the full function call, unrolled546    let functionName = this.astMemberExpressionUnroll(ast.callee);547    // Register the function into the called registry548    if (this.calledFunctions.indexOf(functionName) < 0) {549      this.calledFunctions.push(functionName);550    }551    const isMathFunction = this.isAstMathFunction(ast);552    // track the function was called553    if (this.onFunctionCall) {554      this.onFunctionCall(this.name, functionName, ast.arguments);555    }556    // Call the function557    retArr.push(functionName);558    // Open arguments space559    retArr.push('(');560    const targetTypes = this.lookupFunctionArgumentTypes(functionName) || [];561    // Add the arguments562    for (let i = 0; i < ast.arguments.length; ++i) {563      const argument = ast.arguments[i];564      // in order to track return type, even though this is CPU565      let argumentType = this.getType(argument);566      if (!targetTypes[i]) {567        this.triggerImplyArgumentType(functionName, i, argumentType, this);568      }569      if (i > 0) {570        retArr.push(', ');571      }572      this.astGeneric(argument, retArr);573    }574    // Close arguments space575    retArr.push(')');576    return retArr;577  }578  /**579   * @desc Parses the abstract syntax tree for *Array* Expression580   * @param {Object} arrNode - the AST object to parse581   * @param {Array} retArr - return array string582   * @returns {Array} the append retArr583   */584  astArrayExpression(arrNode, retArr) {585    const returnType = this.getType(arrNode);586    const arrLen = arrNode.elements.length;587    const elements = [];588    for (let i = 0; i < arrLen; ++i) {589      const element = [];590      this.astGeneric(arrNode.elements[i], element);591      elements.push(element.join(''));592    }593    switch (returnType) {594      case 'Matrix(2)':595      case 'Matrix(3)':596      case 'Matrix(4)':597        retArr.push(`[${elements.join(', ')}]`);598        break;599      default:600        retArr.push(`new Float32Array([${elements.join(', ')}])`);601    }602    return retArr;603  }604  astDebuggerStatement(arrNode, retArr) {605    retArr.push('debugger;');606    return retArr;607  }608}609module.exports = {610  CPUFunctionNode...array.js
Source:array.js  
1goog.provide('goog.array'); 2goog.provide('goog.array.ArrayLike'); 3goog.require('goog.asserts'); 4goog.NATIVE_ARRAY_PROTOTYPES = true; 5goog.array.ArrayLike; 6goog.array.peek = function(array) { 7  return array[array.length - 1]; 8}; 9goog.array.ARRAY_PROTOTYPE_ = Array.prototype; 10goog.array.indexOf = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.indexOf ? function(arr, obj, opt_fromIndex) { 11  goog.asserts.assert(arr.length != null); 12  return goog.array.ARRAY_PROTOTYPE_.indexOf.call(arr, obj, opt_fromIndex); 13}: function(arr, obj, opt_fromIndex) { 14  var fromIndex = opt_fromIndex == null ? 0:(opt_fromIndex < 0 ? Math.max(0, arr.length + opt_fromIndex): opt_fromIndex); 15  if(goog.isString(arr)) { 16    if(! goog.isString(obj) || obj.length != 1) { 17      return - 1; 18    } 19    return arr.indexOf(obj, fromIndex); 20  } 21  for(var i = fromIndex; i < arr.length; i ++) { 22    if(i in arr && arr[i]=== obj) return i; 23  } 24  return - 1; 25}; 26goog.array.lastIndexOf = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.lastIndexOf ? function(arr, obj, opt_fromIndex) { 27  goog.asserts.assert(arr.length != null); 28  var fromIndex = opt_fromIndex == null ? arr.length - 1: opt_fromIndex; 29  return goog.array.ARRAY_PROTOTYPE_.lastIndexOf.call(arr, obj, fromIndex); 30}: function(arr, obj, opt_fromIndex) { 31  var fromIndex = opt_fromIndex == null ? arr.length - 1: opt_fromIndex; 32  if(fromIndex < 0) { 33    fromIndex = Math.max(0, arr.length + fromIndex); 34  } 35  if(goog.isString(arr)) { 36    if(! goog.isString(obj) || obj.length != 1) { 37      return - 1; 38    } 39    return arr.lastIndexOf(obj, fromIndex); 40  } 41  for(var i = fromIndex; i >= 0; i --) { 42    if(i in arr && arr[i]=== obj) return i; 43  } 44  return - 1; 45}; 46goog.array.forEach = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.forEach ? function(arr, f, opt_obj) { 47  goog.asserts.assert(arr.length != null); 48  goog.array.ARRAY_PROTOTYPE_.forEach.call(arr, f, opt_obj); 49}: function(arr, f, opt_obj) { 50  var l = arr.length; 51  var arr2 = goog.isString(arr) ? arr.split(''): arr; 52  for(var i = 0; i < l; i ++) { 53    if(i in arr2) { 54      f.call(opt_obj, arr2[i], i, arr); 55    } 56  } 57}; 58goog.array.forEachRight = function(arr, f, opt_obj) { 59  var l = arr.length; 60  var arr2 = goog.isString(arr) ? arr.split(''): arr; 61  for(var i = l - 1; i >= 0; -- i) { 62    if(i in arr2) { 63      f.call(opt_obj, arr2[i], i, arr); 64    } 65  } 66}; 67goog.array.filter = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.filter ? function(arr, f, opt_obj) { 68  goog.asserts.assert(arr.length != null); 69  return goog.array.ARRAY_PROTOTYPE_.filter.call(arr, f, opt_obj); 70}: function(arr, f, opt_obj) { 71  var l = arr.length; 72  var res =[]; 73  var resLength = 0; 74  var arr2 = goog.isString(arr) ? arr.split(''): arr; 75  for(var i = 0; i < l; i ++) { 76    if(i in arr2) { 77      var val = arr2[i]; 78      if(f.call(opt_obj, val, i, arr)) { 79        res[resLength ++]= val; 80      } 81    } 82  } 83  return res; 84}; 85goog.array.map = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.map ? function(arr, f, opt_obj) { 86  goog.asserts.assert(arr.length != null); 87  return goog.array.ARRAY_PROTOTYPE_.map.call(arr, f, opt_obj); 88}: function(arr, f, opt_obj) { 89  var l = arr.length; 90  var res = new Array(l); 91  var arr2 = goog.isString(arr) ? arr.split(''): arr; 92  for(var i = 0; i < l; i ++) { 93    if(i in arr2) { 94      res[i]= f.call(opt_obj, arr2[i], i, arr); 95    } 96  } 97  return res; 98}; 99goog.array.reduce = function(arr, f, val, opt_obj) { 100  if(arr.reduce) { 101    if(opt_obj) { 102      return arr.reduce(goog.bind(f, opt_obj), val); 103    } else { 104      return arr.reduce(f, val); 105    } 106  } 107  var rval = val; 108  goog.array.forEach(arr, function(val, index) { 109    rval = f.call(opt_obj, rval, val, index, arr); 110  }); 111  return rval; 112}; 113goog.array.reduceRight = function(arr, f, val, opt_obj) { 114  if(arr.reduceRight) { 115    if(opt_obj) { 116      return arr.reduceRight(goog.bind(f, opt_obj), val); 117    } else { 118      return arr.reduceRight(f, val); 119    } 120  } 121  var rval = val; 122  goog.array.forEachRight(arr, function(val, index) { 123    rval = f.call(opt_obj, rval, val, index, arr); 124  }); 125  return rval; 126}; 127goog.array.some = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.some ? function(arr, f, opt_obj) { 128  goog.asserts.assert(arr.length != null); 129  return goog.array.ARRAY_PROTOTYPE_.some.call(arr, f, opt_obj); 130}: function(arr, f, opt_obj) { 131  var l = arr.length; 132  var arr2 = goog.isString(arr) ? arr.split(''): arr; 133  for(var i = 0; i < l; i ++) { 134    if(i in arr2 && f.call(opt_obj, arr2[i], i, arr)) { 135      return true; 136    } 137  } 138  return false; 139}; 140goog.array.every = goog.NATIVE_ARRAY_PROTOTYPES && goog.array.ARRAY_PROTOTYPE_.every ? function(arr, f, opt_obj) { 141  goog.asserts.assert(arr.length != null); 142  return goog.array.ARRAY_PROTOTYPE_.every.call(arr, f, opt_obj); 143}: function(arr, f, opt_obj) { 144  var l = arr.length; 145  var arr2 = goog.isString(arr) ? arr.split(''): arr; 146  for(var i = 0; i < l; i ++) { 147    if(i in arr2 && ! f.call(opt_obj, arr2[i], i, arr)) { 148      return false; 149    } 150  } 151  return true; 152}; 153goog.array.find = function(arr, f, opt_obj) { 154  var i = goog.array.findIndex(arr, f, opt_obj); 155  return i < 0 ? null: goog.isString(arr) ? arr.charAt(i): arr[i]; 156}; 157goog.array.findIndex = function(arr, f, opt_obj) { 158  var l = arr.length; 159  var arr2 = goog.isString(arr) ? arr.split(''): arr; 160  for(var i = 0; i < l; i ++) { 161    if(i in arr2 && f.call(opt_obj, arr2[i], i, arr)) { 162      return i; 163    } 164  } 165  return - 1; 166}; 167goog.array.findRight = function(arr, f, opt_obj) { 168  var i = goog.array.findIndexRight(arr, f, opt_obj); 169  return i < 0 ? null: goog.isString(arr) ? arr.charAt(i): arr[i]; 170}; 171goog.array.findIndexRight = function(arr, f, opt_obj) { 172  var l = arr.length; 173  var arr2 = goog.isString(arr) ? arr.split(''): arr; 174  for(var i = l - 1; i >= 0; i --) { 175    if(i in arr2 && f.call(opt_obj, arr2[i], i, arr)) { 176      return i; 177    } 178  } 179  return - 1; 180}; 181goog.array.contains = function(arr, obj) { 182  return goog.array.indexOf(arr, obj) >= 0; 183}; 184goog.array.isEmpty = function(arr) { 185  return arr.length == 0; 186}; 187goog.array.clear = function(arr) { 188  if(! goog.isArray(arr)) { 189    for(var i = arr.length - 1; i >= 0; i --) { 190      delete arr[i]; 191    } 192  } 193  arr.length = 0; 194}; 195goog.array.insert = function(arr, obj) { 196  if(! goog.array.contains(arr, obj)) { 197    arr.push(obj); 198  } 199}; 200goog.array.insertAt = function(arr, obj, opt_i) { 201  goog.array.splice(arr, opt_i, 0, obj); 202}; 203goog.array.insertArrayAt = function(arr, elementsToAdd, opt_i) { 204  goog.partial(goog.array.splice, arr, opt_i, 0).apply(null, elementsToAdd); 205}; 206goog.array.insertBefore = function(arr, obj, opt_obj2) { 207  var i; 208  if(arguments.length == 2 ||(i = goog.array.indexOf(arr, opt_obj2)) < 0) { 209    arr.push(obj); 210  } else { 211    goog.array.insertAt(arr, obj, i); 212  } 213}; 214goog.array.remove = function(arr, obj) { 215  var i = goog.array.indexOf(arr, obj); 216  var rv; 217  if((rv = i >= 0)) { 218    goog.array.removeAt(arr, i); 219  } 220  return rv; 221}; 222goog.array.removeAt = function(arr, i) { 223  goog.asserts.assert(arr.length != null); 224  return goog.array.ARRAY_PROTOTYPE_.splice.call(arr, i, 1).length == 1; 225}; 226goog.array.removeIf = function(arr, f, opt_obj) { 227  var i = goog.array.findIndex(arr, f, opt_obj); 228  if(i >= 0) { 229    goog.array.removeAt(arr, i); 230    return true; 231  } 232  return false; 233}; 234goog.array.concat = function(var_args) { 235  return goog.array.ARRAY_PROTOTYPE_.concat.apply(goog.array.ARRAY_PROTOTYPE_, arguments); 236}; 237goog.array.clone = function(arr) { 238  if(goog.isArray(arr)) { 239    return goog.array.concat((arr)); 240  } else { 241    var rv =[]; 242    for(var i = 0, len = arr.length; i < len; i ++) { 243      rv[i]= arr[i]; 244    } 245    return rv; 246  } 247}; 248goog.array.toArray = function(object) { 249  if(goog.isArray(object)) { 250    return goog.array.concat((object)); 251  } 252  return goog.array.clone((object)); 253}; 254goog.array.extend = function(arr1, var_args) { 255  for(var i = 1; i < arguments.length; i ++) { 256    var arr2 = arguments[i]; 257    var isArrayLike; 258    if(goog.isArray(arr2) ||(isArrayLike = goog.isArrayLike(arr2)) && arr2.hasOwnProperty('callee')) { 259      arr1.push.apply(arr1, arr2); 260    } else if(isArrayLike) { 261      var len1 = arr1.length; 262      var len2 = arr2.length; 263      for(var j = 0; j < len2; j ++) { 264        arr1[len1 + j]= arr2[j]; 265      } 266    } else { 267      arr1.push(arr2); 268    } 269  } 270}; 271goog.array.splice = function(arr, index, howMany, var_args) { 272  goog.asserts.assert(arr.length != null); 273  return goog.array.ARRAY_PROTOTYPE_.splice.apply(arr, goog.array.slice(arguments, 1)); 274}; 275goog.array.slice = function(arr, start, opt_end) { 276  goog.asserts.assert(arr.length != null); 277  if(arguments.length <= 2) { 278    return goog.array.ARRAY_PROTOTYPE_.slice.call(arr, start); 279  } else { 280    return goog.array.ARRAY_PROTOTYPE_.slice.call(arr, start, opt_end); 281  } 282}; 283goog.array.removeDuplicates = function(arr, opt_rv) { 284  var returnArray = opt_rv || arr; 285  var seen = { }, cursorInsert = 0, cursorRead = 0; 286  while(cursorRead < arr.length) { 287    var current = arr[cursorRead ++]; 288    var key = goog.isObject(current) ? 'o' + goog.getUid(current):(typeof current).charAt(0) + current; 289    if(! Object.prototype.hasOwnProperty.call(seen, key)) { 290      seen[key]= true; 291      returnArray[cursorInsert ++]= current; 292    } 293  } 294  returnArray.length = cursorInsert; 295}; 296goog.array.binarySearch = function(arr, target, opt_compareFn) { 297  return goog.array.binarySearch_(arr, opt_compareFn || goog.array.defaultCompare, false, target); 298}; 299goog.array.binarySelect = function(arr, evaluator, opt_obj) { 300  return goog.array.binarySearch_(arr, evaluator, true, undefined, opt_obj); 301}; 302goog.array.binarySearch_ = function(arr, compareFn, isEvaluator, opt_target, opt_selfObj) { 303  var left = 0; 304  var right = arr.length; 305  var found; 306  while(left < right) { 307    var middle =(left + right) >> 1; 308    var compareResult; 309    if(isEvaluator) { 310      compareResult = compareFn.call(opt_selfObj, arr[middle], middle, arr); 311    } else { 312      compareResult = compareFn(opt_target, arr[middle]); 313    } 314    if(compareResult > 0) { 315      left = middle + 1; 316    } else { 317      right = middle; 318      found = ! compareResult; 319    } 320  } 321  return found ? left: ~ left; 322}; 323goog.array.sort = function(arr, opt_compareFn) { 324  goog.asserts.assert(arr.length != null); 325  goog.array.ARRAY_PROTOTYPE_.sort.call(arr, opt_compareFn || goog.array.defaultCompare); 326}; 327goog.array.stableSort = function(arr, opt_compareFn) { 328  for(var i = 0; i < arr.length; i ++) { 329    arr[i]= { 330      index: i, 331      value: arr[i]332    }; 333  } 334  var valueCompareFn = opt_compareFn || goog.array.defaultCompare; 335  function stableCompareFn(obj1, obj2) { 336    return valueCompareFn(obj1.value, obj2.value) || obj1.index - obj2.index; 337  } 338  ; 339  goog.array.sort(arr, stableCompareFn); 340  for(var i = 0; i < arr.length; i ++) { 341    arr[i]= arr[i].value; 342  } 343}; 344goog.array.sortObjectsByKey = function(arr, key, opt_compareFn) { 345  var compare = opt_compareFn || goog.array.defaultCompare; 346  goog.array.sort(arr, function(a, b) { 347    return compare(a[key], b[key]); 348  }); 349}; 350goog.array.isSorted = function(arr, opt_compareFn, opt_strict) { 351  var compare = opt_compareFn || goog.array.defaultCompare; 352  for(var i = 1; i < arr.length; i ++) { 353    var compareResult = compare(arr[i - 1], arr[i]); 354    if(compareResult > 0 || compareResult == 0 && opt_strict) { 355      return false; 356    } 357  } 358  return true; 359}; 360goog.array.equals = function(arr1, arr2, opt_equalsFn) { 361  if(! goog.isArrayLike(arr1) || ! goog.isArrayLike(arr2) || arr1.length != arr2.length) { 362    return false; 363  } 364  var l = arr1.length; 365  var equalsFn = opt_equalsFn || goog.array.defaultCompareEquality; 366  for(var i = 0; i < l; i ++) { 367    if(! equalsFn(arr1[i], arr2[i])) { 368      return false; 369    } 370  } 371  return true; 372}; 373goog.array.compare = function(arr1, arr2, opt_equalsFn) { 374  return goog.array.equals(arr1, arr2, opt_equalsFn); 375}; 376goog.array.defaultCompare = function(a, b) { 377  return a > b ? 1: a < b ? - 1: 0; 378}; 379goog.array.defaultCompareEquality = function(a, b) { 380  return a === b; 381}; 382goog.array.binaryInsert = function(array, value, opt_compareFn) { 383  var index = goog.array.binarySearch(array, value, opt_compareFn); 384  if(index < 0) { 385    goog.array.insertAt(array, value, -(index + 1)); 386    return true; 387  } 388  return false; 389}; 390goog.array.binaryRemove = function(array, value, opt_compareFn) { 391  var index = goog.array.binarySearch(array, value, opt_compareFn); 392  return(index >= 0) ? goog.array.removeAt(array, index): false; 393}; 394goog.array.bucket = function(array, sorter) { 395  var buckets = { }; 396  for(var i = 0; i < array.length; i ++) { 397    var value = array[i]; 398    var key = sorter(value, i, array); 399    if(goog.isDef(key)) { 400      var bucket = buckets[key]||(buckets[key]=[]); 401      bucket.push(value); 402    } 403  } 404  return buckets; 405}; 406goog.array.repeat = function(value, n) { 407  var array =[]; 408  for(var i = 0; i < n; i ++) { 409    array[i]= value; 410  } 411  return array; 412}; 413goog.array.flatten = function(var_args) { 414  var result =[]; 415  for(var i = 0; i < arguments.length; i ++) { 416    var element = arguments[i]; 417    if(goog.isArray(element)) { 418      result.push.apply(result, goog.array.flatten.apply(null, element)); 419    } else { 420      result.push(element); 421    } 422  } 423  return result; 424}; 425goog.array.rotate = function(array, n) { 426  goog.asserts.assert(array.length != null); 427  if(array.length) { 428    n %= array.length; 429    if(n > 0) { 430      goog.array.ARRAY_PROTOTYPE_.unshift.apply(array, array.splice(- n, n)); 431    } else if(n < 0) { 432      goog.array.ARRAY_PROTOTYPE_.push.apply(array, array.splice(0, - n)); 433    } 434  } 435  return array; 436}; 437goog.array.zip = function(var_args) { 438  if(! arguments.length) { 439    return[]; 440  } 441  var result =[]; 442  for(var i = 0; true; i ++) { 443    var value =[]; 444    for(var j = 0; j < arguments.length; j ++) { 445      var arr = arguments[j]; 446      if(i >= arr.length) { 447        return result; 448      } 449      value.push(arr[i]); 450    } 451    result.push(value); 452  } 453}; 454goog.array.shuffle = function(arr, opt_randFn) { 455  var randFn = opt_randFn || Math.random; 456  for(var i = arr.length - 1; i > 0; i --) { 457    var j = Math.floor(randFn() *(i + 1)); 458    var tmp = arr[i]; 459    arr[i]= arr[j]; 460    arr[j]= tmp; 461  } ...app.js
Source:app.js  
1const board = document.querySelector('.board-container')2const boardArray = board.querySelectorAll('.tile')3const resetButton = board.querySelector('.reset')4const popup = board.querySelector('.popup')5const message = popup.querySelector('.message')6const values = [7    0,0,0,0,8    0,0,0,0,9    0,0,0,0,10    0,0,0,0 11    //lose condition12//    0,0,0,5,13//     1,6,7,8,14//     9,1,3,2,15//     5,4,6,1 16    // win condition17    // 0,0,0,0,18    // 0,0,0,0,19    // 0,0,0,1024,20    // 0,0,0,102421]22function valuePusher() {23    for (let i = 0; i<values.length; i++) {24        if (values[i] === 0) {25            boardArray[i].innerHTML = ''26        } else {27            boardArray[i].innerHTML=values[i].toString()28        }29    }30}31console.log(boardArray[2].innerHTML)32console.log(boardArray)33let validMove34let validAdd35function checkValue(tile) {36    if (tile === 0) {37        return true38    } else {39        return false40    }41}42let newPopulation = 043function newNum() {44    let randomIndex = Math.random()45    if (randomIndex < .9) {46        newPopulation = 247    } else {48        newPopulation = 449    }50}51function populateRandom() {52    let randomIndex = Math.floor(Math.random()*values.length)53    let square = checkValue(values[randomIndex])54    newNum()55    if (square === true) {56        values[randomIndex] = newPopulation57        valuePusher()58    } else if (square === false) {59        populateRandom()60    } else {61        console.log('game over')62    }63}64const zeroCheck = (element) => element === 0;65function lossCheck() {66    if (values.some(zeroCheck) === true) {67        populateRandom()68    } else if (values.some(zeroCheck) === false) {69        popup.classList.add('show')70        message.innerHTML= 'You Lose! Press reset to play again'71        boardArray.forEach(element => {72            element.classList.toggle('appear')73        })74    } else {75        console.log('something is wrong')76    }77}78lossCheck()79lossCheck()80function checkWin() {81    const win = (element) => element === 204882    if (values.some(win) === true) {83        popup.classList.add('show')84        message.innerHTML = 'Winner! Press reset to play again!'85        boardArray.forEach(element => {86            element.classList.toggle('appear')87        })88    }89}90function adder(arr, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) {91    let validAddOne92    let validAddTwo93    let validAddThree94    let validAddFour95    if ((arr[a] > 0 && arr[a] === arr[b]) || (arr[a] > 0 && arr[a] === arr[c]) || (arr[a] > 0 && arr[a] === arr[d]) || (arr[b] > 0 && arr[b] === arr[c]) || (arr[b] > 0 && arr[b] === arr[d]) || (arr[c] > 0 && arr[c] === arr[d])) {96        validAddOne = true97        if (arr[d] === arr[c]) {98            arr[d] *= 299            arr[c] = 0100        } else if (arr[c] === 0 && arr[d] === arr[b]) {101            arr[d] *=2102            arr[b] = 0103        } else if (arr[c] === 0 && arr[b] === 0 && arr[d] === arr[a]) {104            arr[d] *=2105            arr[a] = 0106        } 107        if (arr[c] === arr[b]) {108            arr[c] *= 2109            arr[b] = 0110        } else if (arr[b] === 0 && arr[c] === arr[a]) {111            arr[c] *=2112            arr[a] = 0113        } 114        if (arr[b] === arr[a]) {115            arr[b] *= 2116            arr[a] = 0117        } 118        console.log(validAddOne)119    } else {120        validAddOne = false121        console.log(validAdd)122    }123    if ((arr[e] > 0 && arr[e] === arr[f]) || (arr[e] > 0 && arr[e] === arr[g]) || (arr[e] > 0 && arr[e] === arr[h]) || (arr[f] > 0 && arr[f] === arr[g]) || (arr[f] > 0 && arr[f] === arr[h]) || (arr[g] > 0 && arr[g] === arr[h])) {124        validAddTwo = true125        if (arr[h] === arr[g]) {126            arr[h] *= 2127            arr[g] = 0128        } else if (arr[g] === 0 && arr[h] === arr[f]) {129            arr[h] *=2130            arr[f] = 0131        } else if (arr[g] === 0 && arr[f] === 0 && arr[h] === arr[e]) {132            arr[d] *=2133            arr[e] = 0134        } 135        if (arr[g] === arr[f]) {136            arr[g] *= 2137            arr[f] = 0138        } else if (arr[f] === 0 && arr[g] === arr[e]) {139            arr[g] *=2140            arr[e] = 0141        } 142        if (arr[f] === arr[e]) {143            arr[f] *= 2144            arr[e] = 0145        } 146        console.log(validAddTwo)147    } else {148        validAddTwo = false149        console.log(validAddTwo)150    }151    if ((arr[i] > 0 && arr[i] === arr[j]) || (arr[i] > 0 && arr[i] === arr[k]) || (arr[i] > 0 && arr[i] === arr[l]) || (arr[j] > 0 && arr[j] === arr[k]) || (arr[j] > 0 && arr[j] === arr[l]) || (arr[k] > 0 && arr[k] === arr[l])) {152        validAddThree = true153        if (arr[l] === arr[k]) {154            arr[l] *= 2155            arr[k] = 0156        } else if (arr[k] === 0 && arr[l] === arr[j]) {157            arr[l] *=2158            arr[j] = 0159        } else if (arr[k] === 0 && arr[j] === 0 && arr[l] === arr[i]) {160            arr[l] *=2161            arr[i] = 0162        } 163        if (arr[k] === arr[j]) {164            arr[k] *= 2165            arr[j] = 0166        } else if (arr[j] === 0 && arr[k] === arr[i]) {167            arr[k] *=2168            arr[i] = 0169        } 170        if (arr[j] === arr[i]) {171            arr[j] *= 2172            arr[i] = 0173        } 174        console.log(validAddThree)175    } else {176        validAddThree = false177        console.log(validAddThree)178    }179    if ((arr[m] > 0 && arr[m] === arr[n]) || (arr[m] > 0 && arr[m] === arr[o]) || (arr[m] > 0 && arr[m] === arr[p]) || (arr[n] > 0 && arr[n] === arr[o]) || (arr[n] > 0 && arr[n] === arr[p]) || (arr[o] > 0 && arr[o] === arr[p])) {180        validAddFour = true181        if (arr[p] === arr[o]) {182            arr[p] *= 2183            arr[o] = 0184        } else if (arr[o] === 0 && arr[p] === arr[n]) {185            arr[p] *=2186            arr[n] = 0187        } else if (arr[o] === 0 && arr[n] === 0 && arr[p] === arr[m]) {188            arr[p] *=2189            arr[m] = 0190        } 191        if (arr[o] === arr[n]) {192            arr[o] *= 2193            arr[n] = 0194        } else if (arr[n] === 0 && arr[o] === arr[m]) {195            arr[o] *=2196            arr[m] = 0197        } 198        if (arr[n] === arr[m]) {199            arr[n] *= 2200            arr[m] = 0201        } 202        console.log(validAddFour)203    } else {204        validAddFour = false205        console.log(validAddFour)206    }207    if (validAddOne === true || validAddTwo === true || validAddThree === true || validAddFour === true) {208        validAdd = true209    } else if (validAddOne === false && validAddTwo === false && validAddThree === false && validAddFour === false) {210        validAdd = false211    }212}213function slideRight(arr, a, b, c, d, e, f, g, h, z, j, k, l, m, n, o, p) {214    let validMoveOne = false215    let validMoveTwo = false216    let validMoveThree = false217    let validMoveFour = false218    if ((arr[a] > 0 && (arr[b] === 0 || arr[c] === 0 || arr[d] === 0)) || (arr[b] > 0 && (arr[c] === 0 || arr[d] === 0)) || (arr[c] > 0 && arr[d] === 0)) {219        for (let i = 0; i<8; i++) {220         if (arr[a] > 0 && arr[b] === 0) {221             arr[b] = arr[a]222             arr[a] = 0223         } 224         if (arr[b] > 0 && arr[c] === 0) {225             arr[c] = arr[b]226             arr[b] = 0227         }228         if (arr[c] > 0 && arr[d] === 0) {229             arr[d] = arr[c]230             arr[c] = 0231         }232        }233        validMoveOne = true234    } else {235        validMoveOne = false236    }237    if ((arr[e] > 0 && (arr[f] === 0 || arr[g] === 0 || arr[h] === 0)) || (arr[f] > 0 && (arr[g] === 0 || arr[h] === 0)) || (arr[g] > 0 && arr[h] === 0)) {238        for (let i = 0; i<8; i++) {239         if (arr[e] > 0 && arr[f] === 0) {240             arr[f] = arr[e]241             arr[e] = 0242         } 243         if (arr[f] > 0 && arr[g] === 0) {244             arr[g] = arr[f]245             arr[f] = 0246         }247         if (arr[g] > 0 && arr[h] === 0) {248             arr[h] = arr[g]249             arr[g] = 0250         }251        }252        validMoveTwo = true253    } else {254        validMoveTwo = false255    }256    if ((arr[z] > 0 && (arr[j] === 0 || arr[k] === 0 || arr[l] === 0)) || (arr[j] > 0 && (arr[k] === 0 || arr[l] === 0)) || (arr[k] > 0 && arr[l] === 0)) {257        for (let i = 0; i<8; i++) {258         if (arr[z] > 0 && arr[j] === 0) {259             arr[j] = arr[z]260             arr[z] = 0261         } 262         if (arr[j] > 0 && arr[k] === 0) {263             arr[k] = arr[j]264             arr[j] = 0265         }266         if (arr[k] > 0 && arr[l] === 0) {267             arr[l] = arr[k]268             arr[k] = 0269         }270        }271        validMoveThree = true272    } else {273        validMoveThree = false274    }275    if ((arr[m] > 0 && (arr[n] === 0 || arr[o] === 0 || arr[p] === 0)) || (arr[n] > 0 && (arr[o] === 0 || arr[p] === 0)) || (arr[o] > 0 && arr[p] === 0)) {276        for (let i = 0; i<8; i++) {277         if (arr[m] > 0 && arr[n] === 0) {278             arr[n] = arr[m]279             arr[m] = 0280         } 281         if (arr[n] > 0 && arr[o] === 0) {282             arr[o] = arr[n]283             arr[n] = 0284         }285         if (arr[o] > 0 && arr[p] === 0) {286             arr[p] = arr[o]287             arr[o] = 0288         }289        }290        validMoveFour = true291    } else {292        validMoveFour = false293    }294    if (validMoveOne === true || validMoveTwo === true || validMoveThree === true || validMoveFour === true) {295        validMove = true296    } else if (validMoveOne === false && validMoveTwo === false && validMoveThree === false && validMoveFour === false) {297        validMove = false 298    }299}300function moveRight(e) {301    if (e.keyCode === 39) {302        console.log('right')303        adder(values, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)304        slideRight(values, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)305        if (validAdd === true || validMove === true) {306            lossCheck()307        } else if(validAdd === false && validMove === false) {308            console.log('no moves')309        }310        valuePusher()311        checkWin()312    }313}314function moveLeft(e) {315    if (e.keyCode === 37) {316        console.log('left')317        adder(values, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12)318        slideRight(values, 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12)319        if (validAdd === true || validMove === true) {320            lossCheck()321        } else if(validAdd === false && validMove === false) {322            console.log('no moves')323        }324        valuePusher()325        checkWin()326    }327}328function moveUp(e) {329    if (e.keyCode === 38) {330        console.log('up')331        adder(values, 15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0)332        slideRight(values, 15, 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0)333        if (validAdd === true || validMove === true) {334            lossCheck()335            336        } else if(validAdd === false && validMove === false) {337            console.log('no moves')338        }339        valuePusher()340        checkWin()341    }342}343function moveDown(e) {344    if (e.keyCode === 40) {345        console.log('down')346        adder(values, 3, 7, 11, 15, 2, 6, 10, 14, 1, 5, 9, 13, 0, 4, 8, 12)347        slideRight(values, 3, 7, 11, 15, 2, 6, 10, 14, 1, 5, 9, 13, 0, 4, 8, 12)348        if (validAdd === true || validMove === true) {349            lossCheck()350        } else if(validAdd === false && validMove === false) {351            console.log('no moves')352        }353        valuePusher()354        checkWin()355    }356}357function spaceAdd(e) {358    if (e.keyCode === 32) {359        console.log('space')360        lossCheck()361    }362}363function resetBoard() {364    for (let i = 0; i< values.length; i++) {365        values[i] = 0366    }367    valuePusher()368    lossCheck()369    lossCheck()370    popup.classList.remove('show')371    boardArray.forEach(element => {372        element.classList.toggle('appear')373    })374}375valuePusher()376resetButton.addEventListener('click', resetBoard)377document.addEventListener('keydown', moveRight)378document.addEventListener('keydown', moveLeft)379document.addEventListener('keydown', moveUp)380document.addEventListener('keydown', moveDown)...logic.js
Source:logic.js  
1const cross = `<svg width=\"170\" height=\"166\" viewBox=\"0 0 170 166\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<rect width=\"170\" height=\"166\" fill=\"black\" fill-opacity=\"0.01\"></rect>\n<g filter=\"url(#filter0_d)\">\n<line x1=\"8.46447\" y1=\"154.464\" x2=\"158.464\" y2=\"4.46447\" stroke=\"black\" stroke-width=\"10\"></line>\n</g>\n<line x1=\"15.5355\" y1=\"4.46447\" x2=\"165.536\" y2=\"154.464\" stroke=\"black\" stroke-width=\"10\"></line>\n<defs>\n<filter id=\"filter0_d\" x=\"0.92894\" y=\"0.928932\" width=\"165.071\" height=\"165.071\" filterUnits=\"userSpaceOnUse\" color-interpolation-filters=\"sRGB\">\n<feFlood flood-opacity=\"0\" result=\"BackgroundImageFix\"></feFlood>\n<feColorMatrix in=\"SourceAlpha\" type=\"matrix\" values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0\"></feColorMatrix>\n<feOffset dy=\"4\"></feOffset>\n<feGaussianBlur stdDeviation=\"2\"></feGaussianBlur>\n<feColorMatrix type=\"matrix\" values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0\"></feColorMatrix>\n<feBlend mode=\"normal\" in2=\"BackgroundImageFix\" result=\"effect1_dropShadow\"></feBlend>\n<feBlend mode=\"normal\" in=\"SourceGraphic\" in2=\"effect1_dropShadow\" result=\"shape\"></feBlend>\n</filter>\n</defs>\n</svg>\n`;2const circle = `<svg width=\"150\" height=\"150\" viewBox=\"0 0 150 150\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<circle cx=\"75\" cy=\"75\" r=\"70\" stroke=\"black\" stroke-width=\"10\"></circle>\n</svg>\n`;3// gets value of all the divs into an array, something like this [x,o,x,o,x,o,x,o,x]4function valuesOfDivs(){5   let arr = [];6   for(let i = 0; i < 9; i++){7   arr.push(document.getElementsByClassName("grid-item")[i].innerHTML);8   }9   return arr;10}11let arrOfElements = document.getElementsByClassName("grid-item");12let counter = 0;13// checks if someone has won14function isGameOver(arrOfDivs){15        if(arrOfDivs[0] === arrOfDivs[1] && arrOfDivs[1] === arrOfDivs[2] && arrOfDivs[0] == cross16        || arrOfDivs[3] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[5] && arrOfDivs[3] == cross17        || arrOfDivs[6] === arrOfDivs[7] && arrOfDivs[7] === arrOfDivs[8] && arrOfDivs[6] == cross18        || arrOfDivs[0] === arrOfDivs[3] && arrOfDivs[3] === arrOfDivs[6] && arrOfDivs[0] == cross19        || arrOfDivs[0] === arrOfDivs[1] && arrOfDivs[1] === arrOfDivs[2] && arrOfDivs[0] == cross20        || arrOfDivs[1] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[7] && arrOfDivs[1] == cross21        || arrOfDivs[2] === arrOfDivs[5] && arrOfDivs[5] === arrOfDivs[8] && arrOfDivs[2] == cross22        || arrOfDivs[0] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[8] && arrOfDivs[0] == cross23        || arrOfDivs[2] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[6] && arrOfDivs[2] == cross24        ){25        document.getElementById("paragraph").innerHTML = "X HAS WON";26        document.getElementById("paragraph").dataset.gameOver = "true";27    }28        if(arrOfDivs[0] === arrOfDivs[1] && arrOfDivs[1] === arrOfDivs[2] && arrOfDivs[0] == circle29        || arrOfDivs[3] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[5] && arrOfDivs[3] == circle30        || arrOfDivs[6] === arrOfDivs[7] && arrOfDivs[7] === arrOfDivs[8] && arrOfDivs[6] == circle31        || arrOfDivs[0] === arrOfDivs[3] && arrOfDivs[3] === arrOfDivs[6] && arrOfDivs[0] == circle32        || arrOfDivs[0] === arrOfDivs[1] && arrOfDivs[1] === arrOfDivs[2] && arrOfDivs[0] == circle33        || arrOfDivs[1] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[7] && arrOfDivs[1] == circle34        || arrOfDivs[2] === arrOfDivs[5] && arrOfDivs[5] === arrOfDivs[8] && arrOfDivs[2] == circle35        || arrOfDivs[0] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[8] && arrOfDivs[0] == circle36        || arrOfDivs[2] === arrOfDivs[4] && arrOfDivs[4] === arrOfDivs[6] && arrOfDivs[2] == circle37        ){38        document.getElementById("paragraph").innerHTML = "O HAS WON";39        document.getElementById("paragraph").dataset.gameOver = "true";40        }41    if(counter === 9 && document.getElementById("paragraph").innerHTML == ""){42        document.getElementById("paragraph").innerHTML = "Draw";43        document.getElementById("paragraph").dataset.gameOver = "true";44    }45}46function finalFunc(i){47    return function(){48       if(document.getElementById("paragraph").dataset.gameOver == "false" && document.getElementsByClassName("grid-item")[i].innerHTML == ""){49        if(counter%2===0){50            document.getElementsByClassName("grid-item")[i].innerHTML = cross;51        }52        else{53            document.getElementsByClassName("grid-item")[i].innerHTML = circle;54        }55        counter++; 56        isGameOver(valuesOfDivs());57    }58  }59}60// adds event listener for the whole htmlCollection61for(let i = 0; i < arrOfElements.length; i++){62    arrOfElements[i].addEventListener("click", finalFunc(i));63};64//65function removeAll(){66    for(let i = 0; i < arrOfElements.length; i++){67        arrOfElements[i].innerHTML = "";68    };69    document.getElementById("paragraph").dataset.gameOver = "false";70    document.getElementById("paragraph").innerHTML = "";71    counter = 0;72}73// new game button74const newGame = document.getElementsByTagName("button")[0];...Using AI Code Generation
1expect([1, 2, 3]).toIncludeSameMembers([2, 1, 3]);2expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);3expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);4expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);5expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);6expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);7expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);8expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);9expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);10expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);11expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);12expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);13expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);14expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);15expect([1, 2, 3]).toIncludeSameMembers([2, 1, 3]);16expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);17expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);18expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);19expect([1, 2, 3]).toIncludeSameMembers([1, 2, 3, 4]);Using AI Code Generation
1const { arr } = require('jest-extended');2const { arr } = require('jest-extended/dist/arr');3import { arr } from 'jest-extended';4import { arr } from 'jest-extended/dist/arr';5import { arr } from 'jest-extended';6import { arr } from 'jest-extended/dist/arr';7import { arr } from 'jest-extended';8import { arr } from 'jest-extended/dist/arr';9import { arr } from 'jest-extended';10import { arr } from 'jest-extended/dist/arr';11import { arr } from 'jest-extended';12import { arr } from 'jest-extended/dist/arr';13import { arr } from 'jest-extended';14import { arr } from 'jest-extended/dist/arr';15import { arr } from 'jest-extended';16import { arr } from 'jest-extended/dist/arr';17import { arr } from 'jest-extended';18import { arr } from 'jest-extended/dist/arr';19import { arr } from 'jest-extended';20import { arr } from 'jest-extended/dist/arr';21import { arr } from 'jest-extended';22import { arr } from 'jest-extended/dist/arr';23import { arr } from 'jest-extended';24import { arr } from 'jest-extended/dist/arr';25import { arr } from 'jest-extended';26import { arr } from 'jestUsing AI Code Generation
1const arr = [1, 2, 3];2expect(arr).toIncludeSameMembers([2, 1, 3]);3const arr = [1, 2, 3];4expect(arr).toEqual(expect.arrayContaining([2, 1, 3]));5const arr = [1, 2, 3];6expect(arr).toIncludeAllMembers([2, 1, 3]);7const arr = [1, 2, 3];8expect(arr).toEqual(expect.arrayContaining([2, 1, 3]));9const arr = [1, 2, 3];10expect(arr).toIncludeAllMembers([2, 1, 3]);11const arr = [1, 2, 3];12expect(arr).toEqual(expect.arrayContaining([2, 1, 3]));13const arr = [1, 2, 3];14expect(arr).toIncludeAllMembers([2, 1, 3]);15const arr = [1, 2, 3];16expect(arr).toEqual(expect.arrayContaining([2, 1, 3]));17const arr = [1, 2, 3];18expect(arr).toIncludeAllMembers([2, 1, 3]);19const arr = [1, 2, 3];20expect(arr).toEqual(expect.arrayContaining([2, 1, 3]));21const arr = [1, 2, 3];22expect(arr).toIncludeAllMembers([2, 1, 3]);Using AI Code Generation
1const arr = require('jest-extended').arr;2describe('test', () => {3  test('test', () => {4    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);5  });6});7const arr = require('jest-extended').arr;8describe('test', () => {9  test('test', () => {10    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);11  });12});13const arr = require('jest-extended').arr;14describe('test', () => {15  test('test', () => {16    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);17  });18});19const arr = require('jest-extended').arr;20describe('test', () => {21  test('test', () => {22    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);23  });24});25const arr = require('jest-extended').arr;26describe('test', () => {27  test('test', () => {28    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);29  });30});31const arr = require('jest-extended').arr;32describe('test', () => {33  test('test', () => {34    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);35  });36});37const arr = require('jest-extended').arr;38describe('test', () => {39  test('test', () => {40    expect(arr([1, 2, 3])).toContainAnyValues([1, 2, 3]);41  });42});Using AI Code Generation
1const arr = require('jest-extended').arr;2describe('test arr', () => {3  test('test arr', () => {4    expect(arr([1, 2, 3])).toContainAllValues([1, 2, 3]);5  });6});7    expect(received).toContainAllValues(expected)8      at Object.<anonymous> (test.js:7:27)9{10  "scripts": {11  },12  "dependencies": {13  }14}15it('should return a promise', () => {16    const data = new Promise((resolve, reject) => {17        resolve('Success!');18    });19    return data.then((result) => {20        expect(result).toBe('Success!');21    });22});23it('should return a promise', async () => {24    const data = new Promise((resolve, reject) => {25        resolve('Success!');26    });27    const result = await data;28    expect(result).toBe('Success!');29});Using AI Code Generation
1const arr = require('jest-extended').arr;2const { arrayContaining } = require('jest-matcher-utils');3expect.extend({4  toBeArrayContaining(received, argument) {5    const pass = arr(received).arrayContaining(argument);6    if (pass) {7      return {8        message: () =>9          `expected ${this.utils.printReceived(10          )} not to be array containing ${this.utils.printExpected(argument)}`,11      };12    } else {13      return {14        message: () =>15          `expected ${this.utils.printReceived(16          )} to be array containing ${this.utils.printExpected(argument)}`,17      };18    }19  },20});21expect.extend({22  toBeArrayNotContaining(received, argument) {23    const pass = !arr(received).arrayContaining(argument);24    if (pass) {25      return {26        message: () =>27          `expected ${this.utils.printReceived(28          )} not to be array containing ${this.utils.printExpected(argument)}`,29      };30    } else {31      return {32        message: () =>33          `expected ${this.utils.printReceived(34          )} to be array containing ${this.utils.printExpected(argument)}`,35      };36    }37  },38});39expect.extend({40  toBeArrayNotContainingAll(received, argument) {41    const pass = !arr(received).arrayContainingAll(argument);42    if (pass) {43      return {44        message: () =>45          `expected ${this.utils.printReceived(46          )} not to be array containing all ${this.utils.printExpected(argument)}`,47      };48    } else {49      return {50        message: () =>51          `expected ${this.utils.printReceived(52          )} to be array containing all ${this.utils.printExpected(argument)}`,53      };54    }55  },56});57expect.extend({58  toBeArrayContainingAll(received, argument) {59    const pass = arr(received).arrayContainingAll(argument);60    if (pass) {61      return {62        message: () =>63          `expected ${this.utilsLearn 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!!
