kolodny / exercises Goto Github PK
View Code? Open in Web Editor NEWSome basic javascript coding challenges and interview questions
Some basic javascript coding challenges and interview questions
First of all, thanks for this wonderful project.
When I started to do the binary-search challenge, I found there maybe some issues about the last test(the lookup count one). I have written 2 implementations, both implementations has passed all the tests except last one.
First one: the lookup count is 5000 in this implementation. I think maybe it is because the native implementation of array.slice
has touched the get
property. So I implemented the second one.
function search(arr, ele) {
var len = arr.length;
if (len === 0) {
return -1;
}
var middleIdx = Math.floor(len / 2);
if (arr[middleIdx] > ele) {
return search(arr.slice(0, middleIdx), ele);
} else if (arr[middleIdx] < ele) {
var res = search(arr.slice(middleIdx + 1), ele);
if (res === -1) {
return -1;
}
return middleIdx + 1 + res;
} else {
return middleIdx;
}
}
Second one: the lookup count is 11, not 13.
function search(arr, ele) {
var len = arr.length;
if (len === 0) {
return -1;
}
function _search(startIdx, endIdx) {
if (startIdx === endIdx && arr[startIdx] !== ele) {
return -1;
}
var middleIdx = startIdx + Math.floor((endIdx - startIdx) / 2);
if (arr[middleIdx] > ele) {
return _search(startIdx, middleIdx);
} else if (arr[middleIdx] < ele) {
return _search(middleIdx + 1, endIdx);
} else {
return middleIdx;
}
}
return _search(0, len);
}
So I guess maybe 13 is not correct. Could you help me to check this out? Thanks!
Someone not familiar with time complexity might see that this spec is passing and assume that their algorithm is less than O(n^2), when in fact the algorithm might be O(n^2) or worse.
I would think it would be better to just call the spec "sort... can sort a large array" or something.
Or, on the other hand, maybe you actually could do something to verify the time complexity, along the lines of what is in the binary-search "uses a divide and conquer algorithm" example, maybe?
Are there any instructions on how to run files through flipjs?
The only documentation on this is for a jquery library that does flipping animations.
The expectedHistory is
0,1,2,3,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,5,4,3,2,1,0
but it should be
0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0,1,2,3,4,5,4,3,2,1,0
Comparing with this https://github.com/kolodny/exercises/blob/master/throttle-promises/test.js#L36 variable also makes the process stop responsing, don't know the reason
var debounced = debounce(sayHi, 100);
^
TypeError: debounce is not a function
I am new to JavaScript, I was wondering if there is any order to be followed to approach these exercises? Moreover, what do you suggest for a beginner javascripter, should he/she attempt these exercises?
Things like debounce should be level 1, throttle-promises should be something higher
https://github.com/kolodny/exercises/blob/master/throttle/test.js#L8
makes method call Date.now()
and (new Date()).getTime()
always return 0
It works quite weird, IMO
Ideally, the 4th test suite for throttle, will execute every threshold ms
, the throttled function will executed 6
times, as 1st time right away and every time when time counts to 10ms from 10 to 50.
But the test case is unstable, sometimes it passes, sometimes it doesn't. I test it on both my solution and yours. My method to verify it is to run this test case in a loop for multiple times, says, 10. This test case won't all pass.
I think the reason for this problem is the timeout 55ms
of assert.equal is too strict.
Maybe you should consider reset it as 59
, which I think it is reasonable.
And I run the same test with timeout 59
, I got my solution all passes, also the other test case. But yours still won't all pass, I have not figure out what's your solution's problem yet.
Also I create a pull request about this.
The throttle test suite has the following tests:
it('gets called with context', function(done) {
var ctx;
var throttled = throttle(function() {
ctx = this;
}, 10);
throttled.call(11);
throttled.call(22);
setTimeout(function() {
assert.equal(ctx, 22);
done();
}, 15)
});
it('gets called with arguments', function(done) {
var args;
var throttled = throttle(function() {
args = [].slice.call(arguments);
}, 10);
throttled(11, 22, 33);
throttled(22, 33, 44);
setTimeout(function() {
assert.deepEqual(args, [22, 33, 44]);
done();
}, 15);
});
In both cases, it appears that the test suite is expecting the throttled
function to execute twice within the wait period. Is this test suite correct?
Shouldn't that last test's assertion be assert.deepEqual(args, [11, 22, 33]);
?
Theoretically wouldn't you hit a stack overflow if you used a recursive approach to the value exercise?
To be easier to access it is needed a gh-page. Is it possible to do?
Not all developers may understand the context of how these exercises provide value and what their use would be in real world scenarios.
Perhaps including a description of what each of these exercises aims to help the developer learn, where that knowledge might be used, and the context of that use case could be helpful to include?
It provides a testing and programming framework
Added a test for the return value of memoized function
In the test "it will execute every threshold ms", the result expected is [0, 11, 22, 33, 44, 55]
But If we strictly respect the definition of throttle (and the name of the test) :
throttle is used to make sure the function doesn't execute more than once _per_ x milliseconds
It should be [0, 10, 20, 30, 40, 50] right ?
Also, when I run the solution with
var interval = setInterval(throttled, 5);
I get [0,16,28,40,52] ; but it should be [0,10,20,..] ?
setTimeout should not take ms but the difference between the last call, in order to call the func as soon as possible.
How I see throttle according to the definition and the test "it will execute at least once more to make up for swallowed calls" :
With an interval of 6ms and 10ms for throttle (EDIT: Updated with 6ms)
Call 1 (0 ms) direct call ==> [0]
+6ms
Call 2 (6 ms) --> timed for 4s (not 10ms!) ==> [0]
+4ms @10ms Call 2 end ==> [0, 10]
+2ms
Call 3 (12 ms) --> timed for 8s (not 10ms!) ==> [0, 10]
+6ms
Call 4 (18 ms) --> clear Call 3 ; timed for 2s ==> [0, 10]
+2ms @20ms Call 4 end ==> [0, 10, 20]
+4ms
....
I know that doing this is not very relevant (at least with such low interval).
Maybe I'm just over-thinking the problem, but the definition (or the swallowed calls test) should be more explicit about this behavior.
EDIT 2 : Without this swallowed calls test weirdness, it would be even more simple. And we would get [0,12, 24, 36, 38] as 2 * 6ms > 10ms ; 1 call skipped each time. And it strictly respect the simple need of "limit to one call per x ms".
Thanks for this btw !
Hi, what's the difference between those two exercices?
now. I've done this
async
curry
debounce
flatten
flatten-thunk
invert-tree
map
memoize
middleware
once
sort
this is my repo , thank you for your star or clone
I'm going to finish all
This problem is fixed in #6 and #12, but curry/README.md links to an article on Curry, the language, rather than "Currying" as is described in the prompt. While Curry is a language that utilizes FP concepts, the technique of currying is much more widely applicable and does not derive it's name from this language.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.