Skip to content

Instantly share code, notes, and snippets.

@shawnco
Created May 30, 2024 00:13
Show Gist options
  • Save shawnco/d56cfede269177d955b4c515aa8007cf to your computer and use it in GitHub Desktop.
Save shawnco/d56cfede269177d955b4c515aa8007cf to your computer and use it in GitHub Desktop.
Letter Boxed Words-to-Binary Tweet Generator Tutorial
import React, { useState } from 'react';
const TweetGenerator = () => {
const [words, setWords] = useState('');
const [output, setOutput] = useState('');
const wordToBinary = (word) => word
.split('')
.map((w) => w.charCodeAt(0).toString(2))
.join(' ');
const generateTweet = () => {
const wordsArr = words.split('\n').map((w) => w.toUpperCase());
const binary = wordsArr.map(wordToBinary).join('<br />');
const d = new Date();
const month = d.getMonth() + 1;
const day = d.getDate();
const year = d.getFullYear() % 100;
const date = `${month}/${day}/${year}`;
setOutput(`#LetterBoxed ${date}:<br />${binary}`);
};
return (
<div>
<textarea
rows={10}
cols={20}
value={words}
onChange={(e) => setWords(e.target.value)}
/>
<br />
<button onClick={generateTweet}>Generate tweet</button>
<div dangerouslySetInnerHTML={{ __html: output }} />
</div>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment