Best JavaScript code snippet using best
online_cal_js.js
Source:online_cal_js.js
1function buildCal(m, y, cM, cH, cDW, cD, brdr,events_list){2 var mn = ['January','February','March','April','May','June','July','August','September','October','November','December'];3 var dim = [31,0,31,30,31,30,31,31,30,31,30,31];4 // calculate the offset from Sunday at the beginning of the month5 var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st6 oD.od = oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st7 var todaydate = new Date(); //DD added8 var scanfortoday = (y == todaydate.getFullYear() && m == (todaydate.getMonth() + 1))? todaydate.getDate() : 0; //DD added9 // check for leap year, set February number of days (28 or 29)10 dim[1] = (((oD.getFullYear() % 100 !== 0) && (oD.getFullYear() % 4 === 0)) || (oD.getFullYear() % 400 === 0)) ? 29 : 28;11 var t = '<div class="' + cM +'" >';12 t += '<div class="' + cH + '">' + mn[m - 1] + ' - ' + y + '</div>'; // month and year header13 for(w = 0; w < 5; w++) { //creates 35 repeating day names, 21 / 3 = 7, 7 x 5 = 35 total14 for(s = 0; s < 21; s += 1) t += '<div class="' + cDW + '">' + "SMTWTFS".substr(s,1) + '</div>';15 }16 t += '<div class="' + cDW + '">S</div><div class="' + cDW + '">M</div>'; //add two more day names to make 37 total17 t += '<div style="clear:both"></div>'; //clear the float:left18 //collect events for this month19 var currentEvents = [], start, end, span;20 for(e = 0; e < events_list.length; e++) {21 var dStr = events_list[e].start.split(' ');22 var da = dStr[0].split('-');23 var ti = dStr[1].split(':');24 start = new Date(parseInt(da[0],10),parseInt((da[1])-1,10),parseInt(da[2],10),parseInt(ti[0],10),parseInt(ti[1],10),parseInt(ti[2],10));25 dStr = events_list[e].end.split(' ');26 da = dStr[0].split('-');27 ti = dStr[1].split(':');28 end = new Date(parseInt(da[0],10),parseInt((da[1])-1,10),parseInt(da[2],10),parseInt(ti[0],10),parseInt(ti[1],10),parseInt(ti[2],10));29 if((start.getMonth() + 1) == m && start.getFullYear() == y) {30 span = Math.round((end.valueOf() - start.valueOf()) / (24 * 60 * 60 * 1000));31 span = (events_list[e].allDay === "1" || events[e].allDay === "true") ? span : span + 1;32 // add additional information to the array33 events_list[e].span = span; //span of the event in days34 events_list[e].day = start.getDate(); //date of the event35 events_list[e].offset = oD.od - 1; //month start offset36 events_list[e].startDate = start; //start date object37 events_list[e].endDate = end; //end date object38 events_list[e].used = false; //needed later for sorting39 events_list[e].top = 0; //needed later for positioning40 currentEvents.push(events[e]);41 }42 }43 var sortedDates = [];44 for (var key in currentEvents) {45 sortedDates.push(currentEvents[key].startDate); //copy the startDates into a simple array46 }47 sortedDates.sort(date_sort_asc); //sort the start dates48 var sortedEvents = currentEvents;49 for (se = 0; se < sortedDates.length; se++) {50 for (ce = 0; ce < sortedDates.length; ce++) { //match up the unsorted with the sorted startDates.51 if(currentEvents[ce].startDate === sortedDates[se] && (!currentEvents[ce].used)) {52 sortedEvents[ce].sortKey = se; // add a sort key to the sorted array of events53 currentEvents[ce].used = true; //2 events may have exactly the same time stamp, "true" will prevent the same event from being added twice54 }55 }56 }57 //look for overlapping events58 var monthDays = daysInMonth(m, y);//number of days in the current month59 var overlaps = [];60 for (de = 0; de < sortedEvents.length; de++) {61 var id = sortedEvents[de].id;62 var sDate = sortedEvents[de].startDate.getDate();63 var sYear = sortedEvents[de].startDate.getFullYear();64 var sd = sDate + '-' + sYear; //combine date-year65 var sd1, span1 = sortedEvents[de].span;66 for (re = 0; re < span1; re++) { //events can span days. Event days other than the start day can overlap another event's days67 for (ee = 0; ee < sortedEvents.length; ee++) {68 sd1 = sortedEvents[ee].startDate.getDate() + '-' + sortedEvents[de].startDate.getFullYear();//combine date-year69 if (sd === sd1 && (sortedEvents[ee].id !== id)) overlaps.push(id + ',' + sortedEvents[ee].id);//compare70 }71 sDate = ((sDate + 1) > monthDays) ? 1 : sDate + 1;//advance date for multi-day events72 sd = sDate + '-' + sYear;73 }74 }75 var unique = overlaps.filter(onlyUnique); //save only unique overlaps76 var unique1 = [];77 //unique array may still include duplicate reverse order overlaps like "2,7" and "7,2"78 //this loop eliminates reverse order duplicates79 for (ge = 0; ge < unique.length; ge++) {80 if (ge === 0) unique1.push(unique[ge]); //add the first item to unique181 for (he = 0; he < unique1.length; he++) {82 var a = unique[ge].split(',');//split so we can reverse the order and compare83 if ((a[1] + ',' + a[0]) === unique1[he] ||unique[ge] === unique1[he]) {84 break; //break if both the forward or reverse of the pair exists in unique1...85 } else { //...otherwise add unique[ge] to unique186 unique1.push(unique[ge]);87 break;88 }89 }90 }91 //only unique pairs of overlaps remain in unique1 array92 //now adjust the "top" property so later we can use it to stack events one above the other on calendar days93 for (te = 0; te < unique1.length; te++) {94 var ids = unique1[te].split(','), sortkeyA, sortKeyB;95 for (be = 0; be < sortedEvents.length; be++) {96 if (sortedEvents[be].id === ids[0]) sortkeyA = ids[0] + ',' + sortedEvents[be].sortKey;97 if (sortedEvents[be].id === ids[1]) sortkeyB = ids[1] + ',' + sortedEvents[be].sortKey;98 }99 var sortA = sortkeyA.split(',');100 var sortB = sortkeyB.split(',');101 var first = (sortA[1] > sortB[1]) ? sortA[0] : sortB[0];102 for (we = 0; we < sortedEvents.length; we++) {103 if (sortedEvents[we].id === first) {104 sortedEvents[we].top = sortedEvents[we].top + 1;105 }106 }107 }108 var daysPrevMonth = ((m - 1) === 0) ? dim[dim.length-1] : dim[m-2];//previous month number of days109 for(i = 1; i <= 37; i++){ // create 37 boxes for days of the month110 var x = '';111 if ((i - oD.od) < 0) { //print out previous month's days112 // x = '<span style="color:#ccc">' + (daysPrevMonth + (i - (oD.od-1))) + '</span>';113 } else if (((i - oD.od) >= 0) && ((i - oD.od) < dim[m-1])) { //print out this month's days114 x = (i - oD.od) + 1;115 } else { //print out next month's days116 //x = '<span style="color:#ccc">' + (i - (dim[m-1] + (oD.od-1))) + '</span>';117 }118 if (x == scanfortoday) {// find today119 x = '<span id="today">' + x + '</span>'; // highlight today120 }121 for(me = 0; me < sortedEvents.length; me++) {122 if ((sortedEvents[me].day + sortedEvents[me].offset) == i) { // add in the current events123 x = '<span class="'+ sortedEvents[me].class + ' eventBar" >' + x + '</span>'; // highlight today124 //x += '<div class="'+ sortedEvents[me].class + ' eventBar" style="margin-top:' + (sortedEvents[me].top * 20) + 'px;width:' + (((sortedEvents[me].span * 49) - 4) + sortedEvents[me].span) + 'px">' + sortedEvents[me].title + ', ' + sortedEvents[me].span + ' day event</div>';125 }126 }127 t += '<div class="' + cD + '">' + x + '</div>';128 }129 return t += '</div>';130}131//date sorting function132var date_sort_asc = function (date1, date2) {133 if (date1 > date2) return 1;134 if (date1 < date2) return -1;135 return 0;136};137//returns days in a given month and year138function daysInMonth(month,year) {139 return new Date(year, month, 0).getDate();140}141//eliminates duplicates from an array142function onlyUnique(value, index, self) {143 return self.indexOf(value) === index;...
info.js
Source:info.js
1import AbstractView from './absract.js';2import {getEmptyDataClassName} from '../utils/utils-common.js';3import {humanizeDate, sortData, SortType} from '../utils/utils-event.js';4import {draft} from '../utils/utils-render.js';5import {MAX_INFO_TITLES} from '../const.js';6const getTripInfoTitle = (sortedEvents) => {7 if (sortedEvents.length === 0) {8 return ``;9 }10 if (sortedEvents.length > MAX_INFO_TITLES) {11 return `${sortedEvents[0].destination.NAME} — ... — ${sortedEvents[sortedEvents.length - 1].destination.NAME}`;12 }13 return sortedEvents.reduce((finalTemplate, currentEvent, currentEventIndex) => {14 let currentTemplate = `${sortedEvents[currentEventIndex].destination.NAME} — `;15 if (currentEventIndex === sortedEvents.length - 1) {16 currentTemplate = `${sortedEvents[currentEventIndex].destination.NAME}`;17 }18 return `${finalTemplate}${currentTemplate}`;19 }, draft);20};21const getTripInfoDates = (sortedEvents) => {22 if (sortedEvents.length === 0) {23 return ``;24 }25 return `26 ${humanizeDate(`D MMM`, sortedEvents[0].date.START)} — ${humanizeDate(`D MMM`, sortedEvents[sortedEvents.length - 1].date.START)}27 `;28};29const getTotalPrice = (events) => {30 const totalPrice = 0;31 const offerTotalPrice = 0;32 return events.reduce((totalSum, currentEvent) => {33 const currentEventOffers = currentEvent.offers;34 totalSum += currentEvent.price + currentEventOffers.reduce((sum, currentOffer) => {35 sum += currentOffer.price;36 return sum;37 }, offerTotalPrice);38 return totalSum;39 }, totalPrice);40};41const createInfoTemplate = (events) => {42 const sortedEvents = sortData(events, SortType.DAY);43 return `<section class="trip-main__trip-info trip-info ${getEmptyDataClassName(events)}">44 <div class="trip-info__main">45 <h1 class="trip-info__title">${getTripInfoTitle(sortedEvents)}</h1>46 <p class="trip-info__dates">${getTripInfoDates(sortedEvents)}</p>47 </div>48 <p class="trip-info__cost">49 Total: € <span class="trip-info__cost-value">${getTotalPrice(events)}</span>50 </p>51 </section>`;52};53export default class Info extends AbstractView {54 constructor(events) {55 super();56 this._events = events;57 }58 getTemplate() {59 return createInfoTemplate(this._events);60 }...
trip-info-view.js
Source:trip-info-view.js
1import AbstractView from '../framework/view/abstract-view.js';2import {sortPointsDay} from '../utils/sorting.js';3import {getOffersCost} from '../utils/point.js';4import {formatDate} from '../utils/event.js';5import {TripInfoLength} from '../constants.js';6const createTripInfoTemplate = (sortedEvents, tripCost, tripDestinations) => {7 const startDate = formatDate(sortedEvents[0].dateFrom, 'MMM DD');8 const endDate = formatDate(sortedEvents.at(-1).dateTo, 'MMM DD');9 return (10 `<section class="trip-main__trip-info trip-info">11 <div class="trip-info__main">12 <h1 class="trip-info__title">${tripDestinations}</h1>13 <p class="trip-info__dates">${startDate} — ${endDate}</p>14 </div>15 <p class="trip-info__cost">16 Total: € <span class="trip-info__cost-value">${tripCost}</span>17 </p>18 </section>`19 );20};21export default class TripInfoView extends AbstractView {22 #sortedEvents = null;23 #allOffers = null;24 constructor (events, offers) {25 super();26 this.#sortedEvents = events.sort(sortPointsDay);27 this.#allOffers = offers;28 }29 get template() {30 return createTripInfoTemplate(this.#sortedEvents, this.#getTripCost(), this.#getTripDestinations(), );31 }32 #getTripDestinations = () => {33 const firstPoint = this.#sortedEvents[0].destination.name;34 const endPoint = this.#sortedEvents.at(-1).destination.name;35 if (this.#sortedEvents.length === TripInfoLength.MIN_LENGTH) {36 return `${firstPoint}`;37 }38 if (this.#sortedEvents.length <= TripInfoLength.MAX_LENGTH) {39 return this.#sortedEvents.map(({destination}) => `${destination.name}`).join(' — ');40 }41 return `${firstPoint} — . . . — ${endPoint}`;42 };43 #getTripCost = () => this.#sortedEvents.reduce(44 (sum, {offers, type, basePrice}) => {45 const offersCost = getOffersCost(this.#allOffers, offers, type);46 sum += offersCost + basePrice;47 return sum;48 }, 0);...
Using AI Code Generation
1var BestTimeToParty = require('./BestTimeToParty');2 {startTime: 30, endTime: 75},3 {startTime: 0, endTime: 50},4 {startTime: 60, endTime: 150}5];6console.log("Best time to attend the party is at " +7 BestTimeToParty.findBestTime(events) + " o'clock " +8 BestTimeToParty.sortedEvents(events));9var BestTimeToParty = require('./BestTimeToParty');10 {startTime: 30, endTime: 75},11 {startTime: 0, endTime: 50},12 {startTime: 60, endTime: 150}13];14console.log("Best time to attend the party is at " +15 BestTimeToParty.findBestTime(events) + " o'clock " +16 BestTimeToParty.sortedEvents(events));17var BestTimeToParty = require('./BestTimeToParty');18 {startTime: 30, endTime: 75},19 {startTime: 0, endTime: 50},20 {startTime: 60, endTime: 150}21];22console.log("Best time to attend the party is at " +23 BestTimeToParty.findBestTime(events) + " o'clock " +24 BestTimeToParty.sortedEvents(events));25var BestTimeToParty = require('./BestTimeToParty');26 {startTime: 30, endTime: 75},27 {startTime: 0, endTime: 50},28 {startTime: 60, endTime: 150}29];30console.log("Best time to attend the party is at " +31 BestTimeToParty.findBestTime(events) + " o'clock " +32 BestTimeToParty.sortedEvents(events));33var BestTimeToParty = require('./BestTimeToParty');34 {startTime: 30, endTime: 75},35 {startTime: 0, endTime: 50},36 {startTime: 60
Using AI Code Generation
1var BestTimeToParty = require('./bestTimeToParty.js');2var eventList = new BestTimeToParty();3eventList.addEvent('9:00', '12:00');4eventList.addEvent('10:00', '11:00');5eventList.addEvent('12:00', '13:00');6eventList.addEvent('13:00', '14:00');7eventList.addEvent('13:00', '15:00');8eventList.addEvent('14:00', '15:00');9eventList.addEvent('14:00', '16:00');10eventList.addEvent('15:00', '16:00');11eventList.addEvent('16:00', '17:00');12eventList.addEvent('17:00', '18:00');13eventList.addEvent('17:00', '19:00');14eventList.addEvent('18:00', '20:00');15eventList.addEvent('19:00', '21:00');16eventList.addEvent('20:00', '21:00');17eventList.addEvent('20:00', '22:00');18eventList.addEvent('21:00', '22:00');19eventList.addEvent('21:00', '23:00');20eventList.addEvent('22:00', '23:00');21eventList.addEvent('22:00', '24:00');22eventList.addEvent('23:00', '24:00');23eventList.addEvent('23:00', '1:00');24eventList.addEvent('0:00', '1:00');25eventList.addEvent('0:00', '2:00');26eventList.addEvent('1:00', '2:00');27eventList.addEvent('1:00', '3:00');28eventList.addEvent('2:00', '3:00');29eventList.addEvent('2:00', '4:00');30eventList.addEvent('3:00', '4:00');31eventList.addEvent('3:00', '5:00');32eventList.addEvent('4:00', '5:00');33eventList.addEvent('4:00', '6:00');34eventList.addEvent('5:00', '6:00');35eventList.addEvent('5:00', '7:00');36eventList.addEvent('6:00
Using AI Code Generation
1var BestTimeToParty = require('./BestTimeToParty');2var events = new BestTimeToParty();3events.addEvent("2013-03-30 17:00", "2013-03-30 20:00", "Birthday Party");4events.addEvent("2013-03-31 14:00", "2013-03-31 23:00", "Haircut");5events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");6events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");7events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");8events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");9events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");10events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");11events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");12events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");13events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");14events.addEvent("2013-03-31 10:00", "2013-03-31 18:00", "Dentist");15events.addEvent("2013-03-31 09:00", "2013-03-31 17:00", "School");16events.addEvent("2013-03-31 08:00", "2013-03-31 16:00", "Work");17events.addEvent("2013-03-31 12:00", "201
Using AI Code Generation
1var BestTimeToParty = require('./BestTimeToParty');2var events = new BestTimeToParty();3events.addEvent('Birthday Party', 2, 13, 100);4events.addEvent('Dinner Party', 7, 10, 70);5events.addEvent('Pool Party', 12, 15, 90);6events.addEvent('Super Bowl Party', 5, 9, 88);7events.addEvent('Wine Tasting', 9, 17, 200);8events.addEvent('Cookout', 14, 17, 180);9events.addEvent('Graduation Party', 16, 20, 300);10events.addEvent('Wedding', 19, 22, 400);11events.addEvent('Funeral', 23, 24, 10);12events.addEvent('Cocktail Party', 23, 24, 100);13events.addEvent('New Years Eve Party', 23, 24, 1000);14events.addEvent('Concert', 18, 22, 75);15events.addEvent('Festival', 10, 22, 50);16events.addEvent('Halloween', 18, 22, 75);17events.addEvent('Christmas', 10, 22, 50);18var bestTimeToParty = events.bestTimeToParty();19console.log("The best time to attend the party is between " +20 ": enjoy " + bestTimeToParty.event + "!");
Using AI Code Generation
1var sortedEvents = require('./BestTimeToBuySellStocks').sortedEvents;2 {type: 'buy', price: 100, time: 1},3 {type: 'sell', price: 200, time: 2},4 {type: 'buy', price: 150, time: 3},5 {type: 'buy', price: 50, time: 4},6 {type: 'sell', price: 180, time: 5},7 {type: 'sell', price: 250, time: 6},8 {type: 'buy', price: 120, time: 7},9 {type: 'sell', price: 300, time: 8},10 {type: 'sell', price: 50, time: 9},11 {type: 'buy', price: 250, time: 10},12 {type: 'sell', price: 350, time: 11}13];14console.log(sortedEvents(events));
Using AI Code Generation
1var BestTimeToParty = require("./BestTimeToParty").BestTimeToParty;2var events = new BestTimeToParty();3events.addEvent("2017-03-01 19:00", "2017-03-01 23:00", "Birthday Party");4events.addEvent("2017-03-03 19:00", "2017-03-03 23:00", "Birthday Party");5events.addEvent("2017-03-04 19:00", "2017-03-04 23:00", "Birthday Party");6events.addEvent("2017-03-02 19:00", "2017-03-02 23:00", "Birthday Party");7events.addEvent("2017-03-05 19:00", "2017-03-05 23:00", "Birthday Party");8events.addEvent("2017-03-06 19:00", "2017-03-06 23:00", "Birthday Party");9events.addEvent("2017-03-07 19:00", "2017-03-07 23:00", "Birthday Party");10events.addEvent("2017-03-08 19:00", "2017-03-08 23:00", "Birthday Party");11events.addEvent("2017-03-09 19:00", "2017-03-09 23:00", "Birthday Party");12events.addEvent("2017-03-10 19:00", "2017-03-10 23:00", "Birthday Party");13events.addEvent("2017-03-11 19:00", "2017-03-11 23:00", "Birthday Party");14events.addEvent("2017-03-12 19:00", "2017-03-12 23:00", "Birthday Party");15events.addEvent("2017-03-13 19:00", "2017-03-13 23:00", "Birthday Party");16events.addEvent("2017-03-14 19:00", "2017-03-14 23:00", "Birthday Party");17events.addEvent("2017-03-15 19:00", "2017-03-15
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!!