Skip to content

Instantly share code, notes, and snippets.

View gitdagray's full-sized avatar
👋
☕ 💻 Coffee and Code

Dave Gray gitdagray

👋
☕ 💻 Coffee and Code
View GitHub Profile
@gitdagray
gitdagray / navigator_online_at_page_load.js
Last active July 29, 2020 20:03
navigator.onLine at page load
window.addEventListener("load", (event) => {
const statusDisplay = document.getElementById("status");
statusDisplay.textContent = navigator.onLine ? "Online" : "OFFline";
});
@gitdagray
gitdagray / navigator_online.js
Created July 29, 2020 20:05
navigator.onLine with offline and online event listeners
window.addEventListener("load", (event) => {
const statusDisplay = document.getElementById("status");
statusDisplay.textContent = navigator.onLine ? "Online" : "Offline";
});
window.addEventListener("offline", (event) => {
const statusDisplay = document.getElementById("status");
statusDisplay.textContent = "OFFline";
});
@gitdagray
gitdagray / checkOnlineStatus.js
Created July 29, 2020 20:16
checkOnlineStatus function
const checkOnlineStatus = async () => {
try {
const online = await fetch("/1pixel.png");
return online.status >= 200 && online.status < 300; // either true or false
} catch (err) {
return false; // definintely offline
}
};
@gitdagray
gitdagray / rps_v1.js
Last active July 10, 2025 09:37
Rock_Paper_Scissors_v1
// Your First Interactive Game
let playGame = confirm("Shall we play rock, paper, or scissors?");
if (playGame) {
//play
let playerChoice = prompt("Please enter rock, paper, or scissors.");
if (playerChoice) {
let playerOne = playerChoice.trim().toLowerCase();
if (
playerOne === "rock" ||
playerOne === "paper" ||
@gitdagray
gitdagray / rps_v2.js
Created October 8, 2020 18:29
Rock_Paper_Scissors_v2
// Rock, Paper, Scissors: Refactored with While Loop and an Array
let playGame = confirm("Shall we play rock, paper, or scissors?");
if (playGame) {
//play
while (playGame) {
const playerChoice = prompt("Please enter rock, paper, or scissors.");
if (playerChoice || playerChoice === "") {
const playerOne = playerChoice.trim().toLowerCase();
if (
playerOne === "rock" ||
@gitdagray
gitdagray / rps_v3.js
Created October 8, 2020 18:31
Rock_Paper_Scissors_v3
// Rock, Paper, Scissors: Refactored with Functions
const initGame = () => {
const startGame = confirm("Shall we play rock, paper, or scissors?");
startGame ? playGame() : alert("Ok, maybe next time.");
};
// Game flow function
const playGame = () => {
while (true) {
let playerChoice = getPlayerChoice();
@gitdagray
gitdagray / index.html
Created January 4, 2021 07:25
Basic HTML
<!DOCTYPE html>
<html lang="en">
<head>
<title>My First Page</title>
<style>
body { min-height: 300vh;}
</style>
</head>
<body>
<header>
@gitdagray
gitdagray / util.js
Last active August 21, 2025 13:23
Javascript Utility Functions
// Youtube tutorial here: https://youtu.be/LDgPTw6tePk
// These functions are designed to be exported, but you could create a class instead. See tutorial video.
// #1 proper case
export const properCase = (string) => {
return `${string[0].toUpperCase()}${string.slice(1).toLowerCase()}`;
};
@gitdagray
gitdagray / functional_composition_example
Created June 4, 2021 14:46
Functional composition for Vehicle and Motorcycle
// Define methods first. They can be used when building any object.
const readyToGo = () => {
return {
readyToGo() { console.log('Ready to go!') }
}
}
const wheelie = () => {
return {
@gitdagray
gitdagray / decorators.js
Created August 11, 2021 19:30
Using multiple decorators
// Our basic function
let rectangleArea = (length, width) => {
return length * width;
}
// A decorator that counts the parameters
const countParams = (fn) => {
return (...params) => {
console.log('countParams')
if (params.length !== fn.length) {