Skip to content

Instantly share code, notes, and snippets.

@pcmac77
Last active March 26, 2019 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcmac77/e6d92a1cf12de0801e83c1e086310fa7 to your computer and use it in GitHub Desktop.
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
// Q1
// transform string to currency
function toCurrency(input) {
console.log('1_toCurrency');
}
toCurrency('99000000.5566'); // will return '99,000,000.5566'
// 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') )
// 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) )
// 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'
// 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
*/
// 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
<!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>
// 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