Skip to content

Conversation

Ariana1729
Copy link
Contributor

No description provided.

@Gathros Gathros added Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) Hacktoberfest The label for all Hacktoberfest related things! labels Oct 6, 2018
@Butt4cak3 Butt4cak3 self-assigned this Oct 6, 2018
Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for contributing, as always!

There are quite a few things I need you to change, but nothing major. Mostly style related.

  • We use 2 spaces as indentation for JavaScript, not 4.
  • We use camelCase for function and method names in JavaScript.

As a side note: Did you take the C implementation as a reference? Your code looks suspiciously similar to it. It's not wrong or bad. I just noticed. That would also explain the \n in all your console.logs.

@@ -0,0 +1,112 @@
function gaussian_elimination(a){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a space between ) and { here and in all other function declarations where you forgot it.

@@ -0,0 +1,112 @@
function gaussian_elimination(a){
var rows = a.length
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var is a pretty outdated keyword in JavaScript. These days, you should really use either let or const to declare a variable. let is an objectively better version of var. const works the same as let except that you can't overwrite a variable declared with const.

The rule of thumb is: Always use const unless you have to overwrite a variable. That would mean that both rows and cols should be declared as const.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok thanks!

}
}

if (a[pivot][col] == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use === or !==.

continue;
}

if (col != pivot) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use === or !==.

}

if (col != pivot) {
let t=a[col];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be const as explained further up.

Also, please add spaces between operators and operands like t = a[col]. That goes for the next 2 lines as well.

var row = 0;

for (let col = 0; col < cols - 1; ++col) {
if (a[row][col] != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use === or !==.

}
}

var a = [[3, 2 , -4, 3 ],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a should be const.


gaussian_elimination(a);
console.log("Gaussian elimination:\n");
for(let i=0;i<a.length;++i){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned earlier, please make sure to space things out to make them more readable. for (let i = 0; i < a.length; +) {

That goes for pretty much every line from here on out.

gaussian_elimination(a);
console.log("Gaussian elimination:\n");
for(let i=0;i<a.length;++i){
let txt=""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This snippet of code appears 3 times. I suggest creating a function to handle the matrix output. Something like this:

function printMatrixRow(row) { const text = row .map(v => (v < 0 ? " " : " ") + v.toPrecision(8)) .join(""); console.log(text); } function printMatrix(a) { for (const row of a) { printMatrixRow(row); } }

Then you could just printMatrix(a) and printMatrixRow(sol) and you're done.

}

if (a[pivot][col] == 0) {
console.log("The matrix is singular.\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trailing \n is not needed. console.log does that for you.

@Ariana1729
Copy link
Contributor Author

Yeah used the c code as reference+keeping variables mostly the same so when switching between languages it's easier

@Butt4cak3
Copy link
Contributor

Don't forget to update the line numbers in the .md file according to the changes you made.

@Ariana1729
Copy link
Contributor Author

Hmm the line numbers were the same unless I accidentally newlined somewhere
Works on my local aaa

@Butt4cak3
Copy link
Contributor

Then everything seems to be fine. It was just a reminder, because people often forget that.

I'll take a look at the updated code later and if not, tomorrow afternoon my time (CEST). So far it looks good, though.

@Ariana1729
Copy link
Contributor Author

Ah ok thanks!

Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a couple more very minor things. If you fix these, the PR will be ready to merge. Sorry for being so nitpicky, but we want to make sure that the code examples are as readable as possible.

let row = 0;

for (let col = 0; col < cols - 1; ++col) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This empty line here looks weird. It seems quite random because it's the only for loop that has it. Could you remove it for consistency?


const a = [[3, 2, -4, 3],
[ 2, 3, 3, 15],
[ 5, -3, 1, 14]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest you format the matrix like this:

const a = [ [3, 2, -4, 3], [2, 3, 3, 15], [5, -3, 1, 14] ];
function backSubstitution(a) {
const rows = a.length;
const cols = a[0].length;
const sol=[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put spaces around the = here.

}

function backSubstitution(a) {
const rows = a.length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rows variable is unused. You can remove this declaration.

@Ariana1729
Copy link
Contributor Author

Okie I've made the changes

@Butt4cak3
Copy link
Contributor

Great, thanks!


const a = [
[3, 2, -4, 3],
[2, 3, 3, 15],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has a tab instead of 2 spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh oops tab looked like 2 spaces to me thanks

@Butt4cak3 Butt4cak3 dismissed their stale review October 8, 2018 09:11

I made a mistake.

@Butt4cak3
Copy link
Contributor

I just went through the code one last time before I could merge and then I realized that I messed up during my review yesterday. You remember how I told you that the rows variable was unused? Well, I put that comment on the wrong line. The rows variable in gaussJordan() is unused, not the one in backSubstitution(). By removing the variable that I told you to remove yesterday, we broke the code. I'm sorry. You'll have to undo that and remove the rows variable from gaussJordan() instead.

We'll get this PR merged today. I promise. :D

@Ariana1729
Copy link
Contributor Author

whoops died on my bed too early yesterday haha, fixed the code

Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright. Finally. :D

@Butt4cak3 Butt4cak3 merged commit db5b251 into algorithm-archivists:master Oct 9, 2018
@Ariana1729 Ariana1729 deleted the gausselim_js branch October 11, 2018 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hacktoberfest The label for all Hacktoberfest related things! Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)

3 participants