Skip to content

Instantly share code, notes, and snippets.

View pjcodesjs's full-sized avatar

PJ Codes pjcodesjs

View GitHub Profile
@pjcodesjs
pjcodesjs / lc-goal-parser.js
Created May 4, 2021 18:28
PJ Codes Leet code - 1678. Goal Parser Interpretation
/*
LEETCODE PROBLEM: 1678. Goal Parser Interpretation
You own a goal-parser that can interpret a string command. The command consists of an alphabet of "G", "()" and/or "(al)" in some order. The goal-parser will interpret "G" as the string "G", "()" as the string "o" and "(al)" as "al". The interpreted strings are then concatenated in the original order.
Given the string command, return the goal-parser's interpretation of command.
----------
EXAMPLE 1:
----------
@pjcodesjs
pjcodesjs / server.js
Last active May 6, 2021 05:06
Socket.io Setup with Express JS
// IMPORT AND INITIALIZE EXPRESS
var express = require ('express');
const fetch = require('node-fetch');
const socketIo = require('socket.io');
var app = express();
var port = 9000;
// CONNECT TO SOCKET IO
const server = app.listen(port);
const io = require('socket.io')(server);
@pjcodesjs
pjcodesjs / feed.js
Created May 6, 2021 05:18
Feed table
import React, { useState, useEffect } from 'react';
import io from 'socket.io-client';
const FeedSection = () => {
const socket = io("ws://localhost:9000");
socket.on('latest_quakes', data => {
if (data) {
console.log(data);
}
// REGULAR FUNCTION DECLARATION
function my_cats(a,b,c) {
return `My cats names are: ${a}, ${b} & ${c}`;
}
my_cats('Tom', 'Marty', 'Jimmy');
// ARROW FUNCTION
let my_cats = (a,b,c) => {
return `My cats names are: ${a}, ${b} & ${c}`;
// SINGLE LINE ARROW FUNCTIONS WITHOUT RETURN KEYWORD OR {}
let oldest_cat = c => `${c} is the oldest one`;
oldest_cat('Jimmy'); // Jimmy is the oldest one
//------------------------------------------------
// COPY ELEMENTS FROM ONE ARRAY INTO ANOTHER ARRAY
//------------------------------------------------
const fruits = ['apple', 'mangoe', 'papaya'];
const updated_fruits = ['banana', ...fruits];
console.log(updated_fruits); // [ 'banana', 'apple', 'mangoe', 'papaya' ]
//-------------------
// MERGING TWO ARRAYS
//-------------------
// OPTIONAL CHAINING EXAMPLE
let character = {
name: 'Dean Winchester',
age: 34,
car: 'Chevy Imala (Baby)',
enemy_demons: [ 'Abadon', 'Yellow Eyes', 'Crowley' ],
enemy_angels: [ 'Zacharia', 'Michael', 'Lucifer' ],
family: {
mother: 'Mary',
father: 'John',
let Dean = {
name: 'Dean Winchester',
age: 34,
car: 'Chevy Imala (Baby)',
enemy_demons: [ 'Abadon', 'Yellow Eyes', 'Crowley' ],
enemy_angels: [ 'Zacharia', 'Michael', 'Lucifer' ],
family: {
mother: 'Mary',
father: 'John',
}
// REGULAR IF/ELSE
const cat_love_checker = (status) => {
if (status === false) {
return 'Sigh... my cat does not love me :(';
} else {
return 'Aha! My cat does love me!';
}
}
cat_love_checker(false); // Sigh... my cat does not love me :(
// REGULAR WAY
let fav_num;
let fav_fruit = 'Apple';
console.log(fav_num); // undefined
console.log(fav_fruit); // 'Apple'
// SHORTER WAY
let fav_num, fav_fruit = 'Banana';