Best JavaScript code snippet using best
print.js
Source:print.js  
1//Print out a string recursively2function print(s) {3  console.log(s[0]);4  print(s.slice(1))5  return;6}7console.log("ABCD");8/*9 * This is a top-down, structural approach10 * We pass in a smaller subset of the original structure -> first to end of string11 */12function factorial(n) {13  if (n === 0)14    return 1;15  return n * factorial(n-1);16}17function fib(n) {18  if (n <=1 ) return n;19  return fib(n-1) + fib(n-2);20}21/*22 * This is structural where the number is the structure 23 * The depth is bound by the end of the substructure.24 */25function gcd(a, b) {26  if (b === 0) return a;27  return gcd(b, a % b )28}29/*30 * This is generative since the result is computed from the arguments rather than 31 * a successive number of calls.32 * There is no guarantee that generative algorithms terminate.33 */34function printFlatObj(obj) {35  if (Object.keys(obj).length === 0) {36    return;37  }38  //Here we identify a k,v pair as the substructure39  //For simplicity we use object.keys40  const firstKey = Object.keys(obj)[0];41  console.log(firstKey, obj[firstKey])42  delete obj[firstKey];43  printFlatObj(obj);44}45const obj = {a:1, b:2, c:3, d:4}46console.log(printFlatObj(obj));47const deepObj = {a:1, b: {c : 3, d:4}, e: 5}48function printDeepObj(obj) {49  if (Object.keys(obj).length === 0) {50    return;51  }52  const firstKey = Object.keys(obj)[0];53  if (typeof(obj[firstKey]) === 'object') {54    console.log(firstKey)55    printDeepObj(obj[firstKey]);56    delete obj[firstKey];57    printDeepObj(obj);58    return;59  }60  console.log(firstKey, obj[firstKey])61  delete obj[firstKey];62  printDeepObj(obj);63}64console.log("attempting deep obj")65console.log(printDeepObj(deepObj));66function printDeepObjAdjustKeys(obj){67  helper(obj, '');68}69function helper(obj, key) {70  if (Object.keys(obj).length === 0) {71    return;72  }73  const firstKey = Object.keys(obj)[0];74  if (key !== '') {75    keyToPrint = `${key}.${firstKey}`76  } else {77    keyToPrint = firstKey; 78  }79  if (typeof(obj[firstKey]) === 'object') {80    console.log(firstKey);81    //1. pass in (f) with f82    //2. pass in (g) with g83    helper(obj[firstKey], keyToPrint)84    delete obj[firstKey];85    helper(obj, '');86    return;87  }88  console.log(keyToPrint, obj[firstKey]);89  delete obj[firstKey];90  helper(obj, key)91}92console.log("attempting")93const deepObj2 = {a:1, b: {c : 3, d:4}, e: 5}94const deepObj3 = {a:1, b: {c : 3, d:4}, e: 5, f: { g : { h : 6, i: 7}, j: {"" : 8}}}95// f.j: 896console.log(printDeepObjAdjustKeys(deepObj3));97function flatObjPrintHandleEmpty(obj) {98  helper3(obj,'');99}100function helper3 (obj, key) {101  if (Object.keys(obj).length === 0) {102    return;103  }104  const firstKey = Object.keys(obj)[0];105  if (firstKey === "") {106    console.log('found it');107    keyToPrint = `${key}`;108  } else if (key !== "") {109    keyToPrint = `${key}.${firstKey}`;110  } else {111    keyToPrint = firstKey; 112  }113  if (typeof(obj[firstKey]) === 'object') {114    console.log(firstKey);115    //1. pass in (f) with f116    //2. pass in (g) with g117    helper3(obj[firstKey], keyToPrint)118    delete obj[firstKey];119    helper3(obj, '');120    return;121  }122  console.log(keyToPrint, obj[firstKey]);123  delete obj[firstKey];124  helper3(obj, key)125}126console.log("#4")127const deepObj4 = {a:1, b: {c : 3, d:4}, e: 5, f: { g : { h : 6, i: 7}, j: {k : {"": 8}}}}128console.log(flatObjPrintHandleEmpty(deepObj4));129function recurseObj(obj, newObj, set) {130  const set = new Set();131  for (let key of obj) {132    console.log(key, obj[key]);133    break; 134  }135}136/*137 * function flattenDictionary(dict):138    flatDictionary = {}139    flattenDictionaryHelper("", dict, flatDictionary)140    return flatDictionary141function flattenDictionaryHelper(initialKey, dict, flatDictionary):142    for (key : dict.keyset()):143        value = dict.get(key)144        if (!isDictionary(value)): # the value is of a primitive type145            if (initialKey == null || initialKey == ""):146                flatDictionary.put(key, value)147            else:148                flatDictionary.put(initialKey + "." + key, value)149        else:150            if (initialKey == null || initialKey == "")151                flattenDictionaryHelper(key, value, flatDictionary)152            else:153                flattenDictionaryHelper(initialKey + "." + key, value, flatDictionary154 *155 */156function main(dict) {157  let flatDict = {};158  flattenDictionaryHelper("", dict, flatDict)159  return flatDict;160}161function flattenDictionaryHelper(initialKey, dict, flatDict) {162  for (let key in dict) {163    let val = dict[key];164    //primitive165    if (typeof(val) !== 'object') {166      if (initialKey == null || initialKey == "")  {167        flatDict[key] = val;168      } else {169        flatDict[initialKey+"."+key] = val;170      }171    //object172    } else {173      console.log("!!",initialKey)174      if (initialKey === null || initialKey === undefined || initialKey === "")  {175        flattenDictionaryHelper(key, val, flatDict);176      } else {177        flattenDictionaryHelper(initialKey + "." + key, val,flatDict) 178      }179    }180  }181}182const deepObj5 = {a:1, b: {c : 3, d:4}, e: 5, f: { g : { h : 6, i: 7}, j: {k : {"": 8}}}}183console.log(main(deepObj5));184/*185 * In this way this is bottom-up (accumulator)186 * The substructures are actually the objects by level187 * Outer level, inner level, inner-inner level...HashBasedTable.js
Source:HashBasedTable.js  
1const Utils = require(`./Utils`);2/**3 * HashBasedTable class4 */5class HashBasedTable {6    /**7     * Creates new HashBasedTable object8     */9    constructor() {10        const me = this;11        me.hashMap = new Map();12    }13    /**14     * Checks if keys are exist15     * @param firstKey16     * @param secondKey17     * @returns {boolean}18     */19    has(firstKey, secondKey) {20        const me = this;21        let result = false;22        if (secondKey) {23            const secondLevelMap = me.hashMap.get(firstKey);24            if (secondLevelMap) {25                result = secondLevelMap.has(secondKey);26            }27        } else {28            result = me.hashMap.has(firstKey);29        }30        return result;31    }32    /**33     * Returns value by keys34     * @param firstKey35     * @param secondKey36     * @returns {*}37     */38    get(firstKey, secondKey) {39        const me = this;40        let result;41        if (secondKey) {42            const secondLevelMap = me.hashMap.get(firstKey);43            if (secondLevelMap) {44                result = secondLevelMap.get(secondKey);45            }46        } else {47            result = me.hashMap.get(firstKey);48        }49        return result;50    }51    /**52     * Adds value by keys53     * @param firstKey54     * @param secondKey55     * @param items56     */57    add(firstKey, secondKey, items) {58        const me = this;59        let secondLevelMap = me.hashMap.get(firstKey);60        if (!secondLevelMap) {61            secondLevelMap = new Map();62            me.hashMap.set(firstKey, secondLevelMap);63        }64        let set = secondLevelMap.get(secondKey);65        if (!set) {66            set = new Set();67            secondLevelMap.set(secondKey, set);68        }69        Utils.forEach(items, (item) => set.add(item));70    }71    /**72     * Deletes value by keys73     * @param firstKey74     * @param secondKey75     * @returns {boolean}76     */77    delete(firstKey, secondKey) {78        const me = this;79        let result = false;80        if (secondKey) {81            const secondLevelMap = me.hashMap.get(firstKey);82            if (secondLevelMap) {83                result = secondLevelMap.delete(secondKey);84            }85        } else {86            result = me.hashMap.delete(firstKey);87        }88        return result;89    }90    /**91     * Returns size of map92     * @param firstKey93     * @param secondKey94     * @param needSetSize95     * @returns {*}96     */97    size(firstKey, secondKey, needSetSize=false) {98        const me = this;99        let result;100        if (secondKey) {101            const secondLevelMap = me.hashMap.get(firstKey);102            if (needSetSize) {103                const set = secondLevelMap.get(secondKey);104                if (set) {105                    result = set.size;106                }107            } else {108                result = secondLevelMap.size;109            }110        } else {111            result = me.hashMap.size;112        }113        return result;114    }115}...sorting.js
Source:sorting.js  
1export const sortUp = (firstKey, secondKey) => {2  if (secondKey === '' || secondKey === undefined) {3    return (a, b) => {4      const x =5        typeof a[firstKey] === 'string'6          ? a[firstKey].toString().toLowerCase()7          : parseFloat(a[firstKey])8      const y =9        typeof b[firstKey] === 'string'10          ? b[firstKey].toString().toLowerCase()11          : parseFloat(b[firstKey])12      if (x < y) {13        return -114      }15      if (x > y) {16        return 117      }18      return 019    }20  }21  return (a, b) => {22    const x =23      typeof a[firstKey][secondKey] === 'string'24        ? a[firstKey][secondKey].toString().toLowerCase()25        : parseFloat(a[firstKey][secondKey])26    const y =27      typeof b[firstKey][secondKey] === 'string'28        ? b[firstKey][secondKey].toString().toLowerCase()29        : parseFloat(b[firstKey][secondKey])30    if (x < y) {31      return -132    }33    if (x > y) {34      return 135    }36    return 037  }38}39export const sortDown = (firstKey, secondKey) => {40  if (secondKey === '' || secondKey === undefined) {41    return (a, b) => {42      const x =43        typeof a[firstKey] === 'string'44          ? a[firstKey].toString().toLowerCase()45          : parseFloat(a[firstKey])46      const y =47        typeof b[firstKey] === 'string'48          ? b[firstKey].toString().toLowerCase()49          : parseFloat(b[firstKey])50      if (x < y) {51        return 152      }53      if (x > y) {54        return -155      }56      return 057    }58  }59  return (a, b) => {60    const x =61      typeof a[firstKey][secondKey] === 'string'62        ? a[firstKey][secondKey].toString().toLowerCase()63        : parseFloat(a[firstKey][secondKey])64    const y =65      typeof b[firstKey][secondKey] === 'string'66        ? b[firstKey][secondKey].toString().toLowerCase()67        : parseFloat(b[firstKey][secondKey])68    if (x < y) {69      return 170    }71    if (x > y) {72      return -173    }74    return 075  }...Using AI Code Generation
1var bestBook = new BestBook();2console.log(bestBook.firstKey());3console.log(bestBook.secondKey());4console.log(bestBook.thirdKey());5console.log(bestBook.fourthKey());6console.log(bestBook.fifthKey());7var BestBook = function() {8  var book = {9  };10  this.firstKey = function() {11    return Object.keys(book)[0];12  };13  this.secondKey = function() {14    return Object.keys(book)[1];15  };16  this.thirdKey = function() {17    return Object.keys(book)[2];18  };19  this.fourthKey = function() {20    return Object.keys(book)[3];21  };22  this.fifthKey = function() {23    return Object.keys(book)[4];24  };25};26var BestBook = function() {27  var book = {28  };29  this.firstKey = function() {30    return Object.keys(book)[0];31  };Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bestbuy = new BestBuy();3console.log(bestbuy.firstKey());4console.log(bestbuy.lastKey());5function BestBuy() {6    this.products = {7    };8    this.firstKey = function() {9        var firstKey = Object.keys(this.products)[0];10        return firstKey;11    };12    this.lastKey = function() {13        var lastKey = Object.keys(this.products)[Object.keys(this.products).length - 1];14        return lastKey;15    };16}17module.exports = BestBuy;18JavaScript Object.keys() Method19JavaScript Object.values() Method20JavaScript Object.entries() Method21JavaScript Object.getOwnPropertyNames() Method22JavaScript Object.getOwnPropertySymbols() Method23JavaScript Object.is() Method24JavaScript Object.isExtensible() Method25JavaScript Object.isFrozen() Method26JavaScript Object.isSealed() Method27JavaScript Object.preventExtensions() Method28JavaScript Object.seal() Method29JavaScript Object.freeze() MethodUsing AI Code Generation
1var BestRestaurant = require('./bestRestaurant');2var bestRestaurant = new BestRestaurant();3var firstKey = bestRestaurant.firstKey();4console.log(firstKey);5var BestRestaurant = function() {6  this.bestRestaurant = {7  };8};9BestRestaurant.prototype.firstKey = function() {10  for (var key in this.bestRestaurant) {11    return key;12  }13};14module.exports = BestRestaurant;15module.exports = BestRestaurant;16module.exports.firstKey = BestRestaurant.prototype.firstKey;Using AI Code Generation
1var firstKey = require('best-library').firstKey;2var obj = {a: 1, b: 2};3var secondKey = require('best-library').secondKey;4var obj = {a: 1, b: 2};5var thirdKey = require('best-library').thirdKey;6var obj = {a: 1, b: 2, c: 3};7var fourthKey = require('best-library').fourthKey;8var obj = {a: 1, b: 2, c: 3, d: 4};9var fifthKey = require('best-library').fifthKey;10var obj = {a: 1, b: 2, c: 3, d: 4, e: 5};11var sixthKey = require('best-library').sixthKey;12var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6};13var seventhKey = require('best-library').seventhKey;14var obj = {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7};Using AI Code Generation
1var BestBuyAPI = require('bestbuyapi');2var bestbuy = new BestBuyAPI('yourapikey');3bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {4  console.log(data);5});6var BestBuyAPI = require('bestbuyapi');7var bestbuy = new BestBuyAPI('yourapikey');8bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {9  console.log(data);10});11var BestBuyAPI = require('bestbuyapi');12var bestbuy = new BestBuyAPI('yourapikey');13bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {14  console.log(data);15});16var BestBuyAPI = require('bestbuyapi');17var bestbuy = new BestBuyAPI('yourapikey');18bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {19  console.log(data);20});21var BestBuyAPI = require('bestbuyapi');22var bestbuy = new BestBuyAPI('yourapikey');23bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {24  console.log(data);25});26var BestBuyAPI = require('bestbuyapi');27var bestbuy = new BestBuyAPI('yourapikey');28bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {29  console.log(data);30});31var BestBuyAPI = require('bestbuyapi');32var bestbuy = new BestBuyAPI('yourapikey');33bestbuy.firstKey('products', 'categoryPath.id=abcat0502000', function(err, data) {34  console.log(data);35});Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
