Open Side Menu Go to the Top
Register
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** ** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD **

04-30-2015 , 07:17 PM
Quote:
Originally Posted by gaming_mouse
Quote:
Originally Posted by Somnius
getting the impression the timing of my post was less than optimal
The code is pretty verbose. Quick rewite:

Code:
console.log( makeBoard(8,8) );

function makeBoard(w,h) {
  var row = repeatStr(' #', w) + "\n";
  return repeatStr(row, h);
}

function repeatStr(str, n) {
  return new Array(n+1).join(str);
}
Not a JS dev, but does that do the same?

If I understand the initial block correctly, the hash symbols are supposed to alternate by row, much like a chess board pattern if I'm not wrong.

@Somnius: either way, try to reduce as much repetition of code as necessary. That's a good step to begin with.

In your example, and without giving away solutions, in every if/else block the code is identical except for one or two lines.
Start by extracting all the identical lines to the outside of the blocks (start with one innermost block to get a feel for it), then when you end up with very few lines, see if you cannot reduce that part, too.

As an example (assuming all variables have been defined & initialised prior):
Code:
if (onething == 0) {
  this += 1;
  that += 2;
  other += fluffy;
} else {
  this += 1;
  that += 2;
  other += fluffy - 1;
}
turns into:
Code:
this += 1;
that += 2;
if (onething == 0) {
  other += fluffy;
} else {
  other += fluffy - 1;
}
turns into:
Code:
this += 1;
that += 2;
other += (onething == 0) ? fluffy : fluffy - 1;
etc... (hint: fluffy is still being repeated, could be extracted, too)
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 08:47 PM
yeah, you're right. it's easily fixed tho.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 09:12 PM
Yeah, thought so. I can see a few ways to do that without really knowing JS all that well.

Your admittedly more elegant approach just makes it less intuitive when compared to the spelled-out variation (concatenating by single chars) to make certain adjustments such as having an odd number of columns.
For beginners, I'd personally lean more towards refining the current approach, tbh.

But that is just the opinion of one guy here.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 09:46 PM
Quote:
Originally Posted by kazana
Yeah, thought so. I can see a few ways to do that without really knowing JS all that well.

Your admittedly more elegant approach just makes it less intuitive when compared to the spelled-out variation (concatenating by single chars) to make certain adjustments such as having an odd number of columns.
For beginners, I'd personally lean more towards refining the current approach, tbh.

But that is just the opinion of one guy here.
nah, i disagree with pretty strongly. i would even argue that unlearning the procedural thinking you are taught as beginner because it's "easy" is one of the hardest parts of learning to program well. just start out the right way instead.

familiarity with syntax is not the same as simplicity.

when you look at a chessboard, you don't see nested if statements and indexes. you see rows and columns and alternating colors. your programs should reflect the way you naturally see and think about things.

all of programming is a battle against the fog of implementation detail.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 09:51 PM
Silly one-liner:

Array.apply(null, Array(8)).map(function (k, j) { return Array.apply(null, Array(8)).map(function(k,i) { return ((i + j) % 2 == 0 ? ' ' : '#'); }).join(''); }).join("\n");
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 09:53 PM
Quote:
Originally Posted by gaming_mouse
nah, i disagree with pretty strongly. i would even argue that unlearning the procedural thinking you are taught as beginner because it's "easy" is one of the hardest parts of learning to program well. just start out the right way instead.
I mean, yeah, that's an easy statement to make when you've been developing for... How many years have you been developing for?

It's kind of like when movie stars say they'd prefer not being wealthy
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 10:02 PM
Quote:
Originally Posted by jjshabado
Candybar, I think its pretty silly to call their feedback nitpicking style (except for the things that they admitted were nitpicks). It seems pretty clear from your feedback that you also felt that Suzzer's solution wasn't really to the depth they would expect and that he probably misunderstood what they wanted.
I was role-playing, though, having already been told what happened.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 10:19 PM
Quote:
Originally Posted by candybar
Silly one-liner:

Array.apply(null, Array(8)).map(function (k, j) { return Array.apply(null, Array(8)).map(function(k,i) { return ((i + j) % 2 == 0 ? ' ' : '#'); }).join(''); }).join("\n");
nice

Quote:
Originally Posted by Anais
I mean, yeah, that's an easy statement to make when you've been developing for... How many years have you been developing for?

It's kind of like when movie stars say they'd prefer not being wealthy
it's true i've been developing for a long time, but it wasn't just a flippant let the newbs eat cake remark. it was more of "if i'd only known then what i know now." and i meant it pretty literally. spend somewhere between 30 minutes and a few hours playing around with using array methods like map to solve similar problems, however long it takes for the syntax to fade away and for you to see only the underlying concept, and i think you'll agree with my statement that map is a more intuitive way to think about this problem than for loops.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 10:27 PM
candy, something sweet for you:

Code:
([' #','# ']*4).map{|x| x*8}.join("\n")
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 10:38 PM
Quote:
Originally Posted by gaming_mouse
candy, something sweet for you:

Code:
([' #','# ']*4).map{|x| x*8}.join("\n")
4 and 4 instead of 4 and 8 though.

This is very nice and was originally what I was thinking but Javascript takes some of the elegance away and it also doesn't work for odd #s.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 10:43 PM
Quote:
Originally Posted by candybar
4 and 4 instead of 4 and 8 though.

This is very nice and was originally what I was thinking but Javascript takes some of the elegance away and it also doesn't work for odd #s.
yeah, def unfair advantage. es6 is more elegant, but it's hard to beat ruby for this kind of thing.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 11:04 PM
Quote:
Originally Posted by gaming_mouse
nah, i disagree with pretty strongly. i would even argue that unlearning the procedural thinking you are taught as beginner because it's "easy" is one of the hardest parts of learning to program well. just start out the right way instead.

familiarity with syntax is not the same as simplicity.

when you look at a chessboard, you don't see nested if statements and indexes. you see rows and columns and alternating colors. your programs should reflect the way you naturally see and think about things.

all of programming is a battle against the fog of implementation detail.
appreciate your guys discussing it - seeing coders think out loud is pretty helpful.

fwiw btw - there wasn't instruction as to how to approach the chessboard problem - that is naturally where my mind went when I sat down to write pseudocode - then I created that monstrosity - couldve gone one more step at least to refine the code for sure but mind didn't go to creating functions even though have worked with them on codeschool (hand-holding is so much diff then actually formulating from scratch)

Marijn's (book author) solution is interesting -

Code:
var size = 8;

var board = "";

for (var y = 0; y < size; y++) {
  for (var x = 0; x < size; x++) {
    if ((x + y) % 2 == 0)
      board += " ";
    else
      board += "#";
  }
  board += "\n";
}

console.log(board);
seems like a very fine-tuned but at the same time convoluted solution.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 11:25 PM
In a year you probably won't think that is convoluted.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 11:26 PM
Quote:
Originally Posted by suzzer99
Absolutely if he's under you and sucks. I would tell my boss in a heartbeat.
This definitely.

I would have gotten him fired a long time ago as well, probably.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
04-30-2015 , 11:31 PM
Quote:
Originally Posted by iosys
In a year you probably won't think that is convoluted.
I have heard that it is good practice to code in a way that allows a coworker to look at it and understand your objective as quickly as possible.

if you look at that and see 'oh yes - a chessboard' - then I certainly hope you're right and my brain does the same in a year.

Quote:
Originally Posted by kazana
Not a JS dev, but does that do the same?
think everyone here has seen enough of this so won't post any more about it - but know your input helped a lot and taking your approach made it easier to work through simplifying - thank you.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 12:17 AM
Quote:
Originally Posted by kazana
Not a JS dev, but does that do the same?

If I understand the initial block correctly, the hash symbols are supposed to alternate by row, much like a chess board pattern if I'm not wrong.
Okay I lied this is the last post about it! Sorry for the spam wouldn't let me edit the post.

kazana worked from the inside out given my previous solution and ended up here given current tiredness - not perfect but much better especially with the ternary operators

Code:
var gridHeight = 8;
var gridWidth = 8;
var rows = 1;
var grid = '';

for (var rows = 1; rows <= gridHeight; rows++) {
  if (rows % 2 !== 0) {
    for (var rowLength = 1; rowLength <= gridWidth; rowLength++) {
      grid += (rowLength % 2 !== 0) ? '#' : ' ';
    }
    grid += '\n';
  } else {
    for (var rowLength = 1; rowLength <= gridWidth; rowLength++) {
      grid += (rowLength % 2 !== 0) ? ' ' : '#';
    }
    grid += '\n';
  }
}

console.log(grid);
If the above specifically can be simplified further without using functions or altering the central logic or whatever that'd be cool to see.

Ok will keep this stuff in the other thread from here thanks.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 12:22 AM
yes, ignoring that this is not the best way to approach the problem, it is still problematic even for what it is, because you have the entire inner for loop duplicated merely to account for a variation in order between the space and hash. isolate the thing that varies and don't duplicate the thing that stays the same.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 02:46 AM
Quote:
Originally Posted by gaming_mouse
it's true i've been developing for a long time, but it wasn't just a flippant let the newbs eat cake remark. it was more of "if i'd only known then what i know now." and i meant it pretty literally. spend somewhere between 30 minutes and a few hours playing around with using array methods like map to solve similar problems, however long it takes for the syntax to fade away and for you to see only the underlying concept, and i think you'll agree with my statement that map is a more intuitive way to think about this problem than for loops.
Someone just got dinged for creating O(n^3) because they nested a bunch of functional crap together. I really don't think it is good to start someone off with functional, or at the least, introduce a bunch of FP magic to someone who doesn't understand what is happening once that compiles.

Our computers are sick fast, we all have crazy fast internet, yet, the web is getting slower and slower by the day, unless you use plug-ins to stop this. I feel inclined at some point to begin a thread on why FP is outright dangerous, especially for JS-slingers with no foundations.

I know I have a reputation for being FP-first, but that's not the truth. The dangers of using FP was well-defined and the limitations weren't sugar-coated.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 05:41 AM
Quote:
Originally Posted by Somnius
kazana worked from the inside out given my previous solution and ended up here given current tiredness - not perfect but much better especially with the ternary operators

If the above specifically can be simplified further without using functions or altering the central logic or whatever that'd be cool to see.

Ok will keep this stuff in the other thread from here thanks.
You're much closer to the solution in that book than you realize.

The next hint until then is actually repeating/clarifying what I said earlier:
You can typically pull everything that is repeated verbatim on both sides of an if/else block out of the block and have it one time outside (may have to be before or after the block, that's up to you to figure it out).

Transfer that post into another thread and people can help with further hints.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 05:46 AM
Quote:
Originally Posted by Somnius
I have heard that it is good practice to code in a way that allows a coworker to look at it and understand your objective as quickly as possible.

if you look at that and see 'oh yes - a chessboard' - then I certainly hope you're right and my brain does the same in a year.
i look at the authors code and see a grid instantly. your monstrosity i have no idea what it does and to be honest i just skipped your post entirely because i couldn't be bothered to waste the time to try to understand whatever that code was

Quote:
Originally Posted by Somnius
If the above specifically can be simplified further without using functions or altering the central logic or whatever that'd be cool to see.
you already posted the simplified solution from the author

Last edited by greg nice; 05-01-2015 at 05:54 AM.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 05:51 AM
Quote:
Originally Posted by gaming_mouse
spend somewhere between 30 minutes and a few hours playing around with using array methods like map to solve similar problems, however long it takes for the syntax to fade away and for you to see only the underlying concept, and i think you'll agree with my statement that map is a more intuitive way to think about this problem than for loops.
do you have some example problems that someone could use to play around with to learn these concepts? or perhaps a link to some?
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 09:49 AM
Quote:
Originally Posted by greg nice
i look at the authors code and see a grid instantly. your monstrosity i have no idea what it does and to be honest i just skipped your post entirely because i couldn't be bothered to waste the time to try to understand whatever that code was



you already posted the simplified solution from the author
I already understand the psyche and that not everyone who can't be bothered to read through a submission and waste the time also can't be bothered to acknowledge any of it and waste that time. Some people are just ass#$%@s. Not trying to solve that it's tried tested and true already thanks.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 11:53 AM
Quote:
Originally Posted by Somnius
I already understand the psyche and that not everyone who can't be bothered to read through a submission and waste the time also can't be bothered to acknowledge any of it and waste that time. Some people are just ass#$%@s. Not trying to solve that it's tried tested and true already thanks.
I agree with you. Overwhelming majority of posters on this forum want to offer help and realize that they were noobs once. Keep posting here, the advice is good.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 12:01 PM
Quote:
Originally Posted by Somnius
I have heard that it is good practice to code in a way that allows a coworker to look at it and understand your objective as quickly as possible.
This is a cart before the horse kind of thing - like trying to learn how to harmonize before you can hit the notes in your range consistently. Don't worry about other people, just worry about being able to solve problems on your own. This is not a question designed to find out if you're a good programmer, but a simple problem designed to quickly filter out non-programmers.

I wouldn't waste much time trying to figure out a better way to solve this problem - just move onto harder problems.
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote
05-01-2015 , 12:41 PM
Quote:
Originally Posted by Somnius
I already understand the psyche and that not everyone who can't be bothered to read through a submission and waste the time also can't be bothered to acknowledge any of it and waste that time. Some people are just ass#$%@s. Not trying to solve that it's tried tested and true already thanks.
lol.

it was not meant as an attack. but if the shoe fits, wear it i guess. but that was not intended. sorry if it came off harshly

you implied that your code was easier to read and understand for another developer and said that the author's solution was "convoluted". i just gave you an example to the contrary. the fact that you took it differently suggests that you may want to reconsider your attitude and approach
** UnhandledExceptionEventHandler :: OFFICIAL LC / CHATTER THREAD ** Quote

      
m