This file contains hidden or 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
// Falsy values in Javascript | |
// The falsy values in JavaScript are 0, 0n, null, undefined, false, NaN, and the empty string "". | |
// A false value is a value that is false when encountered in Boolean context. | |
Boolean(0) | |
> false | |
Boolean(0n) | |
> false | |
Boolean(null) | |
> false | |
Boolean(undefined) |
This file contains hidden or 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 axios from 'axios' | |
import * as actions from './actions' | |
export function suggestedSearch() { | |
return async dispatch => { | |
try { | |
dispatch(actions.suggestedSearchLoading()) | |
const url = 'https://0b6c4e73-c7fd-4eab-b3ad-89e11db59c58.mock.pstmn.io/search' | |
const json = await axios.get(url) |
This file contains hidden or 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
// Splitting an array into two. | |
// Use case: | |
// Given array: [1,2,3,4,5] | |
// Expected result: [1, 2] and [3, 4, 5] | |
var arr = [1, 2, 3, 4, 5] | |
var arrLength = arr.length | |
var arrMidSection = Math.floor(arrLength/2) | |
var leftPart = arr.slice(0, arrMidSection) | |
var rightPart = arr.slice(arrMidSection) | |
// leftPart |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="col left-col"> |
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>Simple Form Validation</title> | |
</head> | |
<body> | |
<form> |
This file contains hidden or 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 React, { useState, useEffect } from 'react'; | |
const useFetch = (url) => { | |
const [data, setData] = useState(null) | |
const [loading, setLoading] = useState(true) | |
useEffect( async () => { | |
const response = await fetch(url) | |
const data = await response.json() | |
const [ item ] = data.results |
This file contains hidden or 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 React, { useState, useEffect } from 'react'; | |
function App() { | |
const [count, setCount] = useState(0) | |
useEffect(() => { | |
document.title = `You clicked ${count} times` | |
console.log('component did mount') | |
}, []); // componentDidMount, only when the component has been rendered at start |
This file contains hidden or 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 React, { useState } from "react"; | |
import ReactDOM from "react-dom"; | |
function App() { | |
const [count, setCount] = useState(0); | |
const increaseCount = () => { | |
setCount(count + 1) | |
} | |
const decreaseCount = () => { |
This file contains hidden or 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
function formatNumber(number, format = 'xxx-xxx-xxx') { | |
let result = '', | |
curIndex = 0; | |
format.split('').forEach(char => { | |
if(char.toLowerCase() === 'x') { | |
result += number.charAt(curIndex) | |
curIndex++ | |
} else { | |
result += char |
This file contains hidden or 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
function maskCC(number) { | |
// If given parameter doesn't fall under below category, then return | |
// 1. Should be numbers(Every element should be a number) | |
// 2. Should be of length 16 | |
if(!(typeof number === 'string' && number.length === 16 && number.split('').map(Number).every(n => !isNaN(n)) )) { | |
return; | |
} | |
return [...new Array(3).fill('****'), number.slice(-4)].join('-') | |
} |
NewerOlder