Created
November 3, 2011 14:20
-
-
Save sanemat/1336596 to your computer and use it in GitHub Desktop.
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
var bubbleSort = {} | |
bubbleSort.flag = false; | |
bubbleSort.values = [1, 3, 2, 5, 4]; | |
bubbleSort.bubbleSort = function(){ | |
do { | |
this.flag = false; | |
var count = this.values.length; | |
var k = 0; | |
for(var i = 0; i < count - 1 - k; ++i){ | |
if (this.values[i] > this.values[i + 1]) { | |
this.flag = true; | |
var j = this.values[i]; | |
this.values[i] = this.values[i + 1]; | |
this.values[i + 1] = j; | |
} | |
} | |
++k; | |
} while (this.flag); | |
}; | |
describe('bubbleSort', function(){ | |
it('should return sorted value', function(){ | |
expect(bubbleSort.values).toEqual([1, 3, 2, 5, 4]); | |
bubbleSort.bubbleSort(); | |
expect(bubbleSort.values).toEqual([1, 2, 3, 4, 5]); | |
}); | |
}); |
I get my expected result at d471e23a515bbb2ece61f4597f3d9d9d0c508263, but I don't understand yet. Why?
Correct:
return { bubbleSort: bubbleSort, sort: sort };
Wrong:
return { bubbleSort: bubbleSort sort: sort };
Comma? I've understood! This is object literal.
I want to access parent variable from child anonymous function. How can I get this?
a84e2523eb5bd560a9789869a8a2e29ac2b5b8db
My idea is passing sort as argument.
Finally, I soloved my problem.
https://gist.github.com/1336596/12da47b354b7f7c5975fd67b4c72e4c48e958b52
Thanks.
I want to use "destructuring assignment", but I got fail.
https://gist.github.com/1336596/4570cd315b88b41ed360962928c3d4aaafab8dbf
Why?
$ ~/node_modules/jasmine-node/bin/jasmine-node BubbleSort.js The "sys" module is now called "util". It should have a similar interface. Started F bubbleSort it should return sorted value ReferenceError: Invalid left-hand side in assignment at Object.bubbleSort (/Users/sane/work/js-study/algrism-and-data/BubbleSort.js:12:9) at [object Object]. (/Users/sane/work/js-study/algrism-and-data/BubbleSort.js:21:16) Finished in 0.012 seconds 1 test, 2 assertions, 1 failure $ node --version v0.6.3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I want to make bubbleSort, and before do this I want to get "sort" variable. But I can not get "sort" and node repl said:
"bubbleSort.sort is undefined"
Why? And How can I get this param? OR I missunderstand and there is another javascript-ish way?