Best JavaScript code snippet using best
script.js
Source:script.js  
1// Global variables2var timingOptions = {3    easing: 'ease-in-out',4    duration: 700,5};6var modalStart = {7    backgroundColor: 'transparent',8};9var modalEnd = {10    backgroundColor: 'rgba(0,0,0,0.9)',11};12// Add Event listeners13var mealModalOpener = document.querySelector('#mealModalOpener');14mealModalOpener.onclick = function() { handleMealModal('open') };15var mealModalCloser = document.querySelector('#mealModalCloser');16mealModalCloser.onclick = function() { handleMealModal('close') };17var dishModalOpener = document.querySelector('#dishModalOpener');18dishModalOpener.onclick = function() { handleDishModal('open') };19var dishModal = document.querySelector('#dishModal');20dishModal.onclick = function(e) {21    if(e.target !== document.querySelector('#dishModal img')){22        handleDishModal('close');23    }24};25function handleMealModal(action) {26    var btn = document.querySelector('#mealModalOpener');27    var offsetTop = btn.offsetTop;28    var offsetHeight = btn.offsetHeight;29    var offsetWidth = btn.offsetWidth;30    var mealModal = document.querySelector('#mealModal');31    var mealModalContent = document.querySelector('#mealModalContent');32    var mealModalContentStart = {33        height: offsetHeight + 'px',34        width: offsetWidth + 'px',35        marginTop: offsetTop + 'px',36        opacity: 0,37    };38    var mealModalContentEnd = {39        height: '100%',40        width: '100%',41        marginTop: 130 + 'px',42        opacity: 1,43    };44    var mealItems = document.querySelectorAll('.meal-item');45    var mealItemsEnd = {46        marginTop: '0px',47        marginLeft: '0px',48    };49    if (action === 'open') {50        openMealModal();51    }52    if (action === 'close') {53        closeMealModal();54    }55    function openMealModal() {56        mealModal.style.display = 'block';57        mealModal.animate([58            modalStart,59            modalEnd60        ], timingOptions);61        mealModalContent.animate([62            mealModalContentStart,63            mealModalContentEnd64        ], timingOptions);65        mealItems.forEach(mealItem => {66            var firstMealItemOffset = mealItems[0].offsetTop;67            var offsetTop = mealItem.offsetTop;68            var mealItemsStart = {69                marginTop: '-' + Math.abs(offsetTop - firstMealItemOffset) + 'px',70                marginLeft: '-' + 20 + 'px',71            };72            mealItem.animate([73                mealItemsStart,74                mealItemsEnd75            ], timingOptions);76        });77    }78    function closeMealModal() {79        var mealModal = document.querySelector('#mealModal');80        mealModal.animate([81            modalEnd,82            modalStart83        ], timingOptions);84        mealModalContent.animate([85            mealModalContentEnd,86            mealModalContentStart,87        ], timingOptions);88        var offsetDiff = 0;89        mealItems.forEach(function(mealItem) {90            var firstMealItemOffset = mealItems[0].offsetTop;91            var mealItemsStart = {92                marginTop: '-' + Math.abs((mealItem.offsetTop - firstMealItemOffset) - offsetDiff) + 'px',93                marginLeft: '-' + 20 + 'px',94            };95            offsetDiff = mealItem.offsetTop - firstMealItemOffset;96            mealItem.animate([97                mealItemsEnd,98                mealItemsStart99            ], timingOptions);100        });101        setTimeout(function() {102            mealModal.style.display = 'none';103        }, 700);104    }105}106function handleDishModal(action) {107    var dishButton = document.querySelector('#dishModalOpener');108    var offsetTop = dishButton.offsetTop;109    var offsetHeight = dishButton.offsetHeight;110    var offsetWidth = dishButton.offsetWidth;111    var dishModalContent = document.querySelector('#dishModalContent .dish-wrapper');112    var dishModalContentStart = {113        height: offsetHeight + 'px',114        width: offsetWidth + 'px',115        marginTop: offsetTop + 'px',116        marginLeft: '30px',117        opacity: 0.5,118    };119    var dishModalContentEnd = {120        height: '100%',121        width: '100%',122        marginTop: 0 + 'px',123        marginLeft: '0px',124        opacity: 1,125    };126    var dishModalImage = document.querySelector('#dishModalContent img');127    var dishModalImageStart = {128        borderRadius: '15px'129    };130    var dishModalImageEnd = {131        borderRadius: '0px'132    };133    if (action === 'open') {134        openDishModal();135    }136    if (action === 'close') {137        closeDishModal();138    }139    function openDishModal() {140        dishModal.style.display = 'block';141        dishModal.animate([142            modalStart,143            modalEnd144        ], timingOptions);145        dishModalImage.animate([146            dishModalImageStart,147            dishModalImageEnd148        ], timingOptions);149        dishModalContent.animate([150            dishModalContentStart,151            dishModalContentEnd152        ], timingOptions);153    }154    function closeDishModal() {155        dishModal.animate([156            modalEnd,157            modalStart,158        ], timingOptions);159        dishModalImage.animate([160            dishModalImageEnd,161            dishModalImageStart,162        ], timingOptions);163        dishModalContent.animate([164            dishModalContentEnd,165            dishModalContentStart,166        ], timingOptions);167        setTimeout(function() {168            dishModal.style.display = 'none';169        }, 700);170    }...hooks.ts
Source:hooks.ts  
1import { useEffect, useState } from 'react';2import { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native';3import { AnimateStyle, Easing, useAnimatedStyle, useSharedValue, withTiming } from 'react-native-reanimated';4type Type = [ViewStyle, AnimateStyle<StyleProp<ImageStyle>>, TextStyle, TextStyle, (value) => void];5export const useWeatherHourStyle = (isSelected: boolean): Type => {6  const [selected, _setSelected] = useState(isSelected);7  const [wrapper, icon, time, temperature] = isSelected8    ? [useSharedValue(120), useSharedValue(65), useSharedValue(14), useSharedValue(28)]9    : [useSharedValue(100), useSharedValue(50), useSharedValue(12), useSharedValue(22)];10  const timingOptions = {11    duration: 200,12    easing: Easing.bezier(0.25, 0.1, 0.25, 1),13  };14  const animatedWrapper = useAnimatedStyle(() => ({15    width: wrapper.value,16  }));17  const animatedIcon = useAnimatedStyle(() => ({18    width: icon.value,19  }));20  const animatedTime = useAnimatedStyle(() => ({21    fontSize: time.value,22  }));23  const animatedTemperature = useAnimatedStyle(() => ({24    fontSize: temperature.value,25  }));26  const setSelected = (value: boolean) => {27    _setSelected(value);28  };29  useEffect(() => {30    if (selected) {31      wrapper.value = withTiming(120, timingOptions);32      icon.value = withTiming(65, timingOptions);33      time.value = withTiming(14, timingOptions);34      temperature.value = withTiming(28, timingOptions);35    } else {36      wrapper.value = withTiming(100, timingOptions);37      icon.value = withTiming(50, timingOptions);38      time.value = withTiming(12, timingOptions);39      temperature.value = withTiming(22, timingOptions);40    }41  }, [selected]);42  return [animatedWrapper, animatedIcon, animatedTime, animatedTemperature, setSelected];...setPosition.js
Source:setPosition.js  
1import { game, player, stage } from './elements.js'2const timingOptions = {duration: 1, fill: 'both'}3function setPosition({ x, y, perspectiveOrigin }) {4  game.animate({ 5    transform: `translate(${x}px, ${y}px)`6  }, timingOptions)7  player.animate({ 8    transform: `translate(${x}px, ${y}px) rotateX(90deg)`9  }, timingOptions)10  stage.animate({ 11    transform: `translate(${-x}px, ${-y}px)`,12    perspectiveOrigin13  }, timingOptions)14}...Using AI Code Generation
1const BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');2const bestTimeToBuyAndSellStock = new BestTimeToBuyAndSellStock();3console.log(bestTimeToBuyAndSellStock.timingOptions([3, 10, 5, 2, 9, 7, 1, 8, 4, 6]));4class BestTimeToBuyAndSellStock {5    timingOptions(prices) {6        let timingOptions = [];7        let buyPrice = prices[0];8        let sellPrice = prices[0];9        let changeBuyPrice = true;10        for (let i = 0; i < prices.length; i++) {11            if (changeBuyPrice) buyPrice = prices[i];12            sellPrice = prices[i + 1];13            if (sellPrice < buyPrice) {14                changeBuyPrice = true;15            } else {16                let tempProfit = sellPrice - buyPrice;17                if (tempProfit > profit) {18                    profit = tempProfit;19                    timingOptions.push([buyPrice, sellPrice]);20                    changeBuyPrice = false;21                }22            }23        }24        return timingOptions;25    }26}27module.exports = BestTimeToBuyAndSellStock;Using AI Code Generation
1const BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');2const stockPrices = [10, 7, 5, 8, 11, 9];3const stock = new BestTimeToBuyAndSellStock(stockPrices);4console.log(stock.timingOptions());5class BestTimeToBuyAndSellStock {6  constructor(stockPrices) {7    this.stockPrices = stockPrices;8  }9  timingOptions() {10    const timingOptions = [];11    for (let i = 0; i < this.stockPrices.length; i++) {12      for (let j = i + 1; j < this.stockPrices.length; j++) {13        if (this.stockPrices[j] > this.stockPrices[i]) {14          timingOptions.push([i, j]);15        }16      }17    }18    return timingOptions;19  }20}21module.exports = BestTimeToBuyAndSellStock;22O(n^2)23O(n)24O(1)25O(nlogn)26Answer: O(n^2)Using AI Code Generation
1var BestTimeToBuyAndSellStock = require('./BestTimeToBuyAndSellStock');2var bestTime = new BestTimeToBuyAndSellStock();3var prices = [7, 1, 5, 3, 6, 4];4console.log(bestTime.timingOptions(prices));5class BestTimeToBuyAndSellStock {6    timingOptions(prices) {7        var maxProfit = 0;8        for (var i = 0; i < prices.length; i++) {9            for (var j = i + 1; j < prices.length; j++) {10                var profit = prices[j] - prices[i];11                if (profit > maxProfit) {12                    maxProfit = profit;13                }14            }15        }16        return maxProfit;17    }18}19module.exports = BestTimeToBuyAndSellStock;Using AI Code Generation
1var BestTime = require('./besttime');2var bestTime = new BestTime();3bestTime.timingOptions(function(err, data) {4  if (err) {5    console.log(err);6  } else {7    console.log(data);8  }9});10var BestTime = require('./besttime');11var bestTime = new BestTime();12bestTime.timingOptions(function(err, data) {13  if (err) {14    console.log(err);15  } else {16    console.log(data);17  }18});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!!
