Loading...
More JavaScript Posts
function formatDate(date) {return date.toLocaleDateString();}function Comment(props) {return (<div className="Comment"><div className="UserInfo"><imgclassName="Avatar"src={props.author.avatarUrl}alt={props.author.name}/><div className="UserInfo-name">{props.author.name}</div></div><div className="Comment-text">{props.text}</div><div className="Comment-date">{formatDate(props.date)}</div></div>);}const comment = {date: new Date(),text: 'I hope you enjoy learning React!',author: {name: 'Hello Kitty',avatarUrl: 'https://placekitten.com/g/64/64',},};ReactDOM.render(<Commentdate={comment.date}text={comment.text}author={comment.author}/>,document.getElementById('root'));
import { createSlice } from "@reduxjs/toolkit";const alert = createSlice({name: "alert",initialState: {msg: "",status: "",},reducers: {set_alert: (state, action) => {return {...state,msg: action.payload.msg,status: action.payload.status,};},clear_alert: (state, action) => {return {...state,msg: "",status: "",};},},});export default alert.reducer;const { set_alert, clear_alert } = alert.actions;export const setAlert = (msg, status) => dispatch => {dispatch(set_alert({ msg, status }));setTimeout(() => dispatch(clear_alert()), 4000);};export const clearAlert = () => dispatch => {dispatch(clear_alert());};
//QM Helper by Leif Messinger//Groups numbers by number of bits and shows their binary representations.//To be used on x.comconst minterms = prompt("Enter your minterms separated by commas").split(",").map(x => parseInt(x.trim()));const maxNumBits = minterms.reduce(function(a, b) {return Math.max(a, Math.log2(b));});const bitGroups = [];for(let i = 0; i < maxNumBits; ++i){bitGroups.push([]);}for(const minterm of minterms){let outputString = (minterm+" ");//Count the bitslet count = 0;for (var i = maxNumBits; i >= 0; --i) {if((minterm >> i) & 1){++count;outputString += "1";}else{outputString += "0";}}bitGroups[count].push(outputString);}document.body.textContent = "";document.body.style.setProperty("white-space", "pre");for(const group of bitGroups){for(const outputString of group){document.body.textContent += outputString + "\r\n";}}
const union = (...arr) => [...new Set(arr.flat())];// Exampleunion([1, 2], [2, 3], [3]); // [1, 2, 3]
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;// ExamplescelsiusToFahrenheit(15); // 59celsiusToFahrenheit(0); // 32celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15fahrenheitToCelsius(32); // 0
function copyString(text){async function navigatorClipboardCopy(text){if(document.location.protocol != "https:"){return false;}return new Promise((resolve, reject)=>{navigator.clipboard.writeText(text).then(()=>{resolve(true);}, ()=>{resolve(false);});});}function domCopy(text){if(!(document.execCommand)){console.warn("They finally deprecated document.execCommand!");}const input = document.createElement('textarea');input.value = text;document.body.appendChild(input);input.select();const success = document.execCommand('copy');document.body.removeChild(input);return success;}function promptCopy(){prompt("Copy failed. Might have ... somewhere. Check console.", text);console.log(text);}function done(){alert("Copied to clipboard");}navigatorClipboardCopy(text).catch(()=>{return false;}).then((success)=>{if(success){done();}else{if(domCopy(text)){done();}else{promptCopy();}}});}