Skip to content

Instantly share code, notes, and snippets.

@sanemat
Created November 3, 2011 14:20
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 sanemat/1336596 to your computer and use it in GitHub Desktop.
Save sanemat/1336596 to your computer and use it in GitHub Desktop.
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]);
});
});
@sanemat
Copy link
Author

sanemat commented Nov 3, 2011

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?

@sanemat
Copy link
Author

sanemat commented Nov 3, 2011

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 };

@sanemat
Copy link
Author

sanemat commented Nov 3, 2011

Comma? I've understood! This is object literal.

@sanemat
Copy link
Author

sanemat commented Nov 3, 2011

I want to access parent variable from child anonymous function. How can I get this?
a84e2523eb5bd560a9789869a8a2e29ac2b5b8db
My idea is passing sort as argument.

@sanemat
Copy link
Author

sanemat commented Nov 26, 2011

@sanemat
Copy link
Author

sanemat commented Nov 26, 2011

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