Last active
March 26, 2019 08:21
-
-
Save pcmac77/e6d92a1cf12de0801e83c1e086310fa7 to your computer and use it in GitHub Desktop.
_JS TEST created by peterchuang - https://repl.it/@peterchuang/JS-TEST JS Coding questions
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
// Q1 | |
// transform string to currency | |
function toCurrency(input) { | |
console.log('1_toCurrency'); | |
} | |
toCurrency('99000000.5566'); // will return '99,000,000.5566' |
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
// Q2 | |
/* | |
Write a method to decide if two strings are anagrams or not. | |
Note: An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once. | |
Example: | |
anagram("I am a weakish speller", "William Shakespeare") | |
true | |
anagram("listen", "silent") | |
true | |
*/ | |
const anagram = (s1, s2) => { | |
console.log('Q2_anagram'); | |
} | |
console.log( anagram('William Shakespeare', 'I am a weakish speller') ) | |
console.log( anagram('silent', 'listen') ) |
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
// Q3 | |
// Please complete the map function to traverse the tree to get the following result | |
// the returned result will be | |
/* | |
{ | |
'abc.def': true, | |
'abc.ghi': false, | |
'foo.bar': ['foo-bar'], | |
'foo.what.the.hack': 'hack', | |
'a.b.c.d': 123 | |
'a.b.c.e': () => {} // the function reference | |
} | |
*/ | |
const map = (input) => { | |
console.log('Q3_map') | |
return // return the result here | |
} | |
const tree = { | |
abc: { | |
def: true, | |
ghi: false | |
}, | |
foo: { | |
bar: ["foo-bar"], | |
what: { | |
the: { | |
hack: 'hack' | |
} | |
} | |
}, | |
a: { | |
b: { | |
c: { | |
d: 123, | |
e: () => {}, | |
} | |
} | |
} | |
} | |
console.log( map(tree) ) |
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
// Q4 | |
const getUrl = (url, params) => { | |
return url + +params['nickName']; | |
} | |
console.log(getUrl('/api/v1/user/{id}/profile', { | |
id: 0, | |
nickName: 'Lee Chen', | |
description: 'Hello World! @.@' | |
})) | |
// should be '/api/v1/user/0/profile?nickName=Lee+Chen&description=Hello+World!%20%40.%40' |
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
// Q5 | |
// complete the function to simulate a promise that fulfills the below usages | |
const createPromise = (executor) => { | |
} | |
const promiseA = createPromise((resolve, reject) => { | |
setTimeout(() => { | |
resolve('yes') | |
}, 100) | |
}) | |
promiseA.then((response) => { | |
console.log('response should be yes', response) | |
return { | |
data: response | |
} | |
}).then((response) => { | |
console.log('response.data should be yes', response.data) | |
}).finally(() => { | |
console.log('done') | |
}) | |
/* | |
response should be yes: yes | |
response.data should be yes: yes | |
done | |
*/ | |
const promiseB = createPromise((resolve, reject) => { | |
setTimeout(() => { | |
reject('no') | |
}, 100) | |
}) | |
promiseB.catch((error) => { | |
console.log('response should be no', error) | |
return { | |
data: response | |
} | |
}).catch((error) => { | |
console.log('error.data should be no', error.data) | |
}).finally(() => { | |
console.log('done') | |
}) | |
/* | |
error should be no: no | |
error.data should be no: no | |
done | |
*/ |
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
// Q6 | |
// Please complete this function to satisfy the 4 usages below | |
function add() { | |
} | |
console.log( 'add(1)(2)(3)', add(1)(2)(3) ); // should be 6 | |
console.log( 'add(1, 2)(3)', add(1, 2)(3) ); // should be 6 | |
console.log( 'add(1)(2, 3)', add(1)(2, 3) ); // should be 6 | |
console.log( 'add(1, 2, 3)', add(1, 2, 3) ); // should be 6 |
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>repl.it</title> | |
</head> | |
<body> | |
<script src="1_toCurrency.js"></script> | |
<script src="2_anagram.js"></script> | |
<script src="3_map.js"></script> | |
<script src="4_geturl.js"></script> | |
<script src="5_promise.js"></script> | |
<script src="6_add.js"></script> | |
<!--<script src="setTimeout.js"></script>--> | |
</body> | |
</html> |
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
// Please complete this function to satisfy the 4 usages below | |
function add() { | |
} | |
console.log( 'add(1)(2)(3)', add(1)(2)(3) ); // should be 6 | |
console.log( 'add(1, 2)(3)', add(1, 2)(3) ); // should be 6 | |
console.log( 'add(1)(2, 3)', add(1)(2, 3) ); // should be 6 | |
console.log( 'add(1, 2, 3)', add(1, 2, 3) ); // should be 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment