Created
March 3, 2017 00:44
-
-
Save fitfab/86c96cbd99c9d3690593624a628ee111 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/lesoce
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// String exercises | |
// Palindrome | |
function isPalind(str) { | |
return str.split('').reverse().join('').toLowerCase() === str.toLowerCase(); | |
} | |
console.log('Palindrome? level ' + isPalind('level')); | |
console.log('Palindrome? Rotator ', isPalind('Rotator')); | |
// count how chars in a gibing string | |
function countChar(char, str) { | |
var count = 0; | |
for(var i=0; i<str.length; i++) { | |
if(str[i] == char) { | |
count++; | |
} | |
} | |
return count; | |
} | |
console.log('how many occurence of a in banana? ' + countChar('a','banana')); | |
// duplicate item in array | |
function findDuplicate(arr) { | |
var dictionary = {}; | |
var dup = []; | |
for(var i=0; i<arr.length;i++) { | |
if(!dictionary[arr[i]]) { | |
dictionary[arr[i]] = 1; | |
} else { | |
dup.push(arr[i]); | |
} | |
} | |
return dup; | |
} | |
console.log(findDuplicate(['c', 2, 3, 4, 5, 3, 'c',3,'c'])); | |
// Recursion | |
function countDown(start){ | |
if (start === 0) { | |
return; | |
} | |
console.log(start); | |
countDown(start-1); | |
} | |
countDown(7); | |
var categories = [ | |
{id:'animal', parent: null}, | |
{id:'mamal', parent: 'animal'}, | |
{id:'cat', parent: 'mamal'}, | |
{id:'dog', parent: 'mamal'}, | |
{id:'chihuaha', parent: 'dog'}, | |
{id:'German Sheper', parent: 'dog'}, | |
{id:'Siamese', parent: 'cat'}, | |
{id:'Persian', parent: 'cat'} | |
]; | |
function makeTree(cat, parent) { | |
var node = {}; | |
cat.filter(c => c.parent === parent) | |
.forEach(s => node[s.id] = makeTree(cat, s.id)); | |
return node; | |
} | |
console.log( | |
JSON.stringify( | |
makeTree(categories, null), | |
null, | |
2 | |
) | |
); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// String exercises | |
// Palindrome | |
function isPalind(str) { | |
return str.split('').reverse().join('').toLowerCase() === str.toLowerCase(); | |
} | |
console.log('Palindrome? level ' + isPalind('level')); | |
console.log('Palindrome? Rotator ', isPalind('Rotator')); | |
// count how chars in a gibing string | |
function countChar(char, str) { | |
var count = 0; | |
for(var i=0; i<str.length; i++) { | |
if(str[i] == char) { | |
count++; | |
} | |
} | |
return count; | |
} | |
console.log('how many occurence of a in banana? ' + countChar('a','banana')); | |
// duplicate item in array | |
function findDuplicate(arr) { | |
var dictionary = {}; | |
var dup = []; | |
for(var i=0; i<arr.length;i++) { | |
if(!dictionary[arr[i]]) { | |
dictionary[arr[i]] = 1; | |
} else { | |
dup.push(arr[i]); | |
} | |
} | |
return dup; | |
} | |
console.log(findDuplicate(['c', 2, 3, 4, 5, 3, 'c',3,'c'])); | |
// Recursion | |
function countDown(start){ | |
if (start === 0) { | |
return; | |
} | |
console.log(start); | |
countDown(start-1); | |
} | |
countDown(7); | |
var categories = [ | |
{id:'animal', parent: null}, | |
{id:'mamal', parent: 'animal'}, | |
{id:'cat', parent: 'mamal'}, | |
{id:'dog', parent: 'mamal'}, | |
{id:'chihuaha', parent: 'dog'}, | |
{id:'German Sheper', parent: 'dog'}, | |
{id:'Siamese', parent: 'cat'}, | |
{id:'Persian', parent: 'cat'} | |
]; | |
function makeTree(cat, parent) { | |
var node = {}; | |
cat.filter(c => c.parent === parent) | |
.forEach(s => node[s.id] = makeTree(cat, s.id)); | |
return node; | |
} | |
console.log( | |
JSON.stringify( | |
makeTree(categories, null), | |
null, | |
2 | |
) | |
);</script></body> | |
</html> |
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
// String exercises | |
// Palindrome | |
function isPalind(str) { | |
return str.split('').reverse().join('').toLowerCase() === str.toLowerCase(); | |
} | |
console.log('Palindrome? level ' + isPalind('level')); | |
console.log('Palindrome? Rotator ', isPalind('Rotator')); | |
// count how chars in a gibing string | |
function countChar(char, str) { | |
var count = 0; | |
for(var i=0; i<str.length; i++) { | |
if(str[i] == char) { | |
count++; | |
} | |
} | |
return count; | |
} | |
console.log('how many occurence of a in banana? ' + countChar('a','banana')); | |
// duplicate item in array | |
function findDuplicate(arr) { | |
var dictionary = {}; | |
var dup = []; | |
for(var i=0; i<arr.length;i++) { | |
if(!dictionary[arr[i]]) { | |
dictionary[arr[i]] = 1; | |
} else { | |
dup.push(arr[i]); | |
} | |
} | |
return dup; | |
} | |
console.log(findDuplicate(['c', 2, 3, 4, 5, 3, 'c',3,'c'])); | |
// Recursion | |
function countDown(start){ | |
if (start === 0) { | |
return; | |
} | |
console.log(start); | |
countDown(start-1); | |
} | |
countDown(7); | |
var categories = [ | |
{id:'animal', parent: null}, | |
{id:'mamal', parent: 'animal'}, | |
{id:'cat', parent: 'mamal'}, | |
{id:'dog', parent: 'mamal'}, | |
{id:'chihuaha', parent: 'dog'}, | |
{id:'German Sheper', parent: 'dog'}, | |
{id:'Siamese', parent: 'cat'}, | |
{id:'Persian', parent: 'cat'} | |
]; | |
function makeTree(cat, parent) { | |
var node = {}; | |
cat.filter(c => c.parent === parent) | |
.forEach(s => node[s.id] = makeTree(cat, s.id)); | |
return node; | |
} | |
console.log( | |
JSON.stringify( | |
makeTree(categories, null), | |
null, | |
2 | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment