This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const hostname = '127.0.0.1'; | |
const port = 3000; | |
const server = http.createServer((req, res) => { | |
res.statusCode = 200; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Hello World\n'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
============ | |
# install web3py first | |
!pip install web3 | |
from eth_account import Account | |
import secrets | |
acct = None | |
expect = "000" | |
while acct is None or acct.address[2:2+len(expect)] != expect: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
read_files = glob.glob("*.md") | |
with open("new-file.md", "wb") as outfile: | |
for f in read_files: | |
with open(f, "rb") as infile: | |
outfile.write(infile.read()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Asynchronous functions | |
// Code goes here | |
const shortenUrl = async () => { | |
const urlToShorten = inputField.value; | |
const data = JSON.stringify({destination: urlToShorten}); | |
try { | |
const response = await fetch('https://api-to-call.com/endpoint', { | |
method: 'POST', | |
body: data, | |
headers: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript | |
str = str.replace(/\s+/g, ''); | |
// \s is the regex for "whitespace", and g is the "global" flag, meaning match ALL \s (whitespaces). | |
// A great explanation for + can be found https://stackoverflow.com/questions/5964373/is-there-a-difference-between-s-g-and-s-g | |
// Alternative | |
var a = b = " /var/www/site/Brand new document.docx "; | |
console.log( a.split(' ').join('') ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// https://stackoverflow.com/questions/21076732/how-to-get-the-last-item-in-a-javascript-value-object-with-a-for-loop | |
// blind iteration | |
Object.keys(obj).forEach(function(key, i) { | |
var value = obj[key]; | |
// do what you need to here, with index i as position information. | |
// Note that you cannot break out of this iteration, although you | |
// can of course use ".some()" rather than ".forEach()" for that. | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// async await GET | |
const getData = async () => { | |
try { | |
const response = await fetch('https://api-to-call.com/endpoint'); | |
if (response.ok) { | |
const jsonResponse = await response.json(); | |
// Code to execute with jsonResponse | |
} | |
throw new Error('Request failed!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fetch POST | |
fetch('http://api-to-call.com/endpoint', { | |
method: 'POST', | |
headers: { | |
'Content-type': 'application/json', | |
'apikey': apiKey | |
} | |
body: JSON.stringify({id: '200'}) | |
}).then(response => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fetch GET | |
fetch('http://api-to-call.com/endpoint').then(response => { // Sends request | |
if (response.ok) { | |
return response.json(); // converts response object to JSON | |
} | |
throw new Error('Request failed!'); | |
}, networkError => console.log(networkError.message) | |
).then(jsonResponse => { | |
// Code to execute with jsonResponse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/questions/27678052/usage-of-the-backtick-character-in-javascript | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals | |
let person = {name: 'RajiniKanth', age: 68, greeting: 'Thalaivaaaa!' }; | |
// Usual HTML String | |
let usualHtmlStr = "<p>My name is " + person.name + ",</p>\n" + | |
"<p>I am " + person.age + " old</p>\n" + | |
"<strong>\"" + person.greeting + "\" is what I usually say</strong>"; | |
// New HTML String |
NewerOlder