Published
Edited
Apr 26, 2019
2 stars
Insert cell
Insert cell
Insert cell
{
const x = 32243;
return reverseValue(x);
function reverseValue(value) {
return typeof value === 'number' ? parseInt(reverseString(String(value))) : reverseString(value);
}
function reverseString(string) {
return string.split('').reduceRight((res, v) => (res += v), '');
}
}
Insert cell
Insert cell
{
const a = 'madam';
const b = 'nurses run';
const c = 'fox';
return [a, b, c].map(value => ([value, isPalindrome(value)]));
function isPalindrome(text) {
text = text.replace(/ /g, '');
// return text === Array.from(text).reverse().join('');
return text === Array.from(text).reduceRight((res, s) => (res += s), '');
}
}
Insert cell
Insert cell
Insert cell
{
const a = 'webmaster';
return sortAlpha(a);
function sortAlpha(value) {
return Array.from(value).sort().join('');
}
}
Insert cell
Insert cell
{
const a = 'the quick brown fox';
return capitalize(a);
function capitalize(text) {
return text
.split(' ')
.map((value) => (value[0].toUpperCase() + value.slice(1)))
.join(' ');
}
}
Insert cell
Insert cell
{
const a = 'Web Development Tutorial';
return longestWord(a);
function longestWord(text) {
return text
.split(' ')
.sort((a, b) => (a.length > b.length ? -1 : 1))[0];
}
}
Insert cell
Insert cell
{
const a = 'The quick brown fox';
return [countVowelsV1(a), countVowelsV2(a)];
function countVowelsV1(text) {
const vowels = ['a', 'e', 'i', 'o', 'u'];
return text
.replace('/ /g', '')
.split('')
.filter((char) => vowels.includes(char.toLowerCase()))
.length;
}
function countVowelsV2(text) {
return text.match(/[aeiou]/g).length;
}
}
Insert cell
Insert cell
{
const a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 23, 24, 25, 26, 27, 29, 31];
return a.map(n => ([n, isPrime(n)]));

function isPrime(num) {
if (num <= 2) {
return num === 2;
}
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}

}
Insert cell
Insert cell
{
const a = [1, '1', [1], false, RegExp(), {}, undefined, null];
return a.map(v => getType(v));
function getType(value) {
if (value === null) {
return 'null';
}
if (value && value.constructor) {
return value.constructor.name;
}
return typeof value;
}
}
Insert cell
Insert cell
{
return createMatrix(5);
function createMatrix(n) {
let m = [];
for (let x = 0; x < n; x++) {
if (m[x] === undefined) {
m[x] = [];
}
for (let y = 0; y < n; y++) {
m[x][y] = x === y ? x : 0;
}
}
return m;
}
}
Insert cell
Insert cell
{
const a = [1, -3, 2, 4, 5, 5];
return find2ndLowestAndGreatest(a);
function find2ndLowestAndGreatest(arr){
const uniq = arr
.reduce((res, n) => {
if (!res.includes(n)) {
res.push(n);
}
return res;
}, [])
.sort((a, b) => a > b);
return [uniq[1], uniq[uniq.length - 2]];
}
}
Insert cell
Insert cell
{
const a = [6, 28, 8, 9];
return a.map(n => [n, isPerfectNum(n)]);
function isPerfectNum(num) {
let a = [];
for (let i = 1; i <= num / 2; i++) {
if (num % i === 0) {
a.push(i);
}
}
return sum(a) === num;
}
function sum(arr) {
return arr.reduce((res, a) => res += a, 0);
}
}
Insert cell
Insert cell
Insert cell
{
const coins = [25, 10, 5, 2, 1];
const a = 46;
return amountToCoins(a, coins);
function amountToCoins(num, coins) {
return coins.reduce((res, coin) => {
while(num - coin >= 0) {
res.push(coin);
num -= coin;
}
return res;
}, []);
}
}
Insert cell
Insert cell
{
return computePow(2, 3);
function computePow(base, exp) {
let res = base;
while(exp > 1) {
res *= base;
exp--;
}
return res;
}
function pow(base, exp) {
return Math.pow(base, exp);
}
}
Insert cell
Insert cell
{
const a = 'the quick brown fox jumps over the lazy dog 1';
return uniqChars(a);
function uniqChars(text) {
return text.toLowerCase()
.match(/[a-z]/g)
.reduce((res, char) => {
if (!res.includes(char)) {
res.push(char);
}
return res;
}, [])
.join('');
}
}
Insert cell
Insert cell
{
const a = 'The quick brown fox jumps over the lazy dog';
return countChars(a);
function countChars(text) {
return text.match(/[a-zA-Z0-9]/g)
.reduce((res, char) => {
res[char] = res[char] || 0;
res[char]++;
return res;
}, {});
}
}
Insert cell
Insert cell
{
const a = [1, 2, 3, 5, 6, 7, 10, 11, 14, 15, 17, 19, 20, 22, 23];
return find(a, 5);
function find(arr, value) {
let low = 0;
let high = arr.length - 1;
let mid;
while (low < high){
mid = Math.floor((low + high) / 2);
if (arr[mid] === value) {
return mid;
} else if (arr[mid] < value) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
}
Insert cell
Insert cell
{
const a = [1, 2, 3, 5, 10, 11, 4];
return getBiggers(a, 5);
function getBiggers(arr, value) {
return arr.filter((n) => n > value);
}
}
Insert cell
Insert cell
Insert cell
{
const a = [1, 2, 3, 4];
return getSubsets(a, 2);
function getSubsets(arr, len) {
let sets = [];
for (let i = 0; i < Math.pow(arr.length, len); i++) {
let k = arr.length - 1;
let a = [];
do {
if ((i & (1 << k)) !== 0) {
a.push(arr[k]);
}
} while (k--);
if (a.length >= len) {
sets.push(a);
}
}
return sets;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
{
const a = [12, 345, 4, 546, 122, 84, 98, 64, 9, 1, 3223, 455, 23, 234, 213];
return bubbleSort(a);
function bubbleSort(arr) {
return arr.reduce((res, n, i) => {
for (let j = 0; j < res.length; j++) {
if (res[i] < res[j]) {
let v = res[j];
res[j] = res[i];
res[i] = v;
}
}
return res;
}, arr.slice());
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more