Skip to content

Instantly share code, notes, and snippets.

@lejonmanen
lejonmanen / script.js
Last active December 20, 2021 15:46
Minimal service worker for use when developing a PWA
// Copy and paste this code into your main script file
// The purpose is to register the service worker
// The browser will load sw.js for us if you do this
if ('serviceWorker' in navigator) {
// Assumes your service worker has file name "sw.js"
navigator.serviceWorker.register('sw.js')
.then(reg => {
console.log('Registration succeeded. Scope is ' + reg.scope);
});

Alt text

A Study in Programming

En stor del av att lära sig programmering handlar om ens tankesätt och att bygga upp mentala modeller. I denna guide ska vi titta på hur vi kan använda Sherlock Holmes "Science of deduction" som ett arbetssätt för att lösa olika programmeringsproblem. Du kanske ställer dig frågan varför Sherlock Holmes har något med programmering att göra? Se detta som ett sätt att tänka och som en hjälp när du ska analysera och lösa olika programmeringsproblem.

Sherlock Holmes "Science of deduction" består av tre delar: Observe, Theorize, Test, som beskrivs mer i detalj nedanför. Bra att ha i åtanke är att även om dessa görs i ordning är de även individuellt bra att ha i åtanke konstant när man programmerar.

Observe

Lösenordsgenerator

I denna övning ska du bygga en lösenordsgenerator i flera steg.

Level 1

Skapa en enklare lösenordsgenerator i form av en function passwordGenerator(length) där enda argumentet är längd på lösenordet. Funktionen ska returnera ett slumpat lösenord som visas på skärmen. Använd Math.random() för att skapa slumptal.

Du får inte använda dig av regex.

@lejonmanen
lejonmanen / useCancellableAjax.js
Last active March 20, 2024 21:47
React hook for sending fetch requests and cancelling them safely.
import { useEffect, useRef } from 'react'
/*
Safely send and cancel one AJAX request at the time. If you need to send multiple requests at the same time, call this hook several times.
Usage:
const [cancelRef, doAjax] = useCancellableAjax()
Fetch data. Tip: use a setState function as callback.
doAjax(url, fetchOptions, dataCallback)
@lejonmanen
lejonmanen / MediaDevice.jsx
Created November 11, 2021 20:40
Use MediaDevices to display video stream from webcam in a React component.
import { useRef } from 'react'
const MediaDevice = () => {
const videoRef = useRef(null)
const handleVideoOn = () => assignStream(videoRef.current)
const handleVideoOff = () => videoRef.current.srcObject = null
return (
<div>
@lejonmanen
lejonmanen / Notis.jsx
Created November 11, 2021 21:11
Use Notification API to display notifications from a React component.
import { useState, useEffect } from 'react'
// Be aware that notifications are fickle creatures. You may have to jump through some hoops to get them working on your desired browsers and platforms. The following code is tested on Firefox for Windows. But you should be able to modify it to work in other environments.
const Notis = () => {
const [canUse, setCanUse] = useState(false)
const [clicks, setClicks] = useState(0)
const [fromNotification, setFromNotification] = useState(null)
useEffect(() => {
@lejonmanen
lejonmanen / LiftingState.tsx
Last active January 18, 2022 13:19
React: Lifting state up example, with TypeScript
import { useState } from 'react'
/*
"Lifting state up" is useful when several components need to share state.
In this example, both Parent and Child needs to display the value of
"clicks". Child needs to update the value as well. We are lifting the
state up, out of the child component, into its parent.
1. Move the shared state to the common component - the component that
contains all of the components that needs to share state. Here, Parent is
@lejonmanen
lejonmanen / input.js
Created March 9, 2022 10:53
Node script: how to get input from user in the console
const prompt = require('prompt-sync')()
/*
Before running this code, make sure to create an npm project:
npm init -y
npm install prompt-sync
node input.js
*/
// How to get input from user
@lejonmanen
lejonmanen / datatyper.js
Last active March 16, 2022 11:02
JavaScript demo datatyper och funktioner
/*
console.log('Hello world') // apostrof (enkelfnutt)
console.log("Hello world") // citattecken (dubbelfnutt)
console.log(`Hello world`) // grav accent, backtick
*/
const exempel = 15 // global, gäller i hela filen
console.log(exempel)
// definiera funktionen sayName
function sayName(name) {
@lejonmanen
lejonmanen / ContextDemo.jsx
Created April 11, 2022 09:43
React Context demo using hooks
import { createContext, useState, useContext } from 'react'
// Create the context in a separate file. Import it to every component that needs it.
const CounterContext = createContext(0)
const ContextDemo = () => {
const [count, setCount] = useState(2)
const contextValue = { count, setCount }