r/ProgrammerHumor • • 19h ago

Meme technicallyCorrect

Post image
1.0k Upvotes

42 comments sorted by

View all comments

Show parent comment

-1

u/AlwaysHopelesslyLost 10h ago edited 9h ago

Two special case lines, end of line padding, head and tail characters, two offsets per line.

What are you on about? Zero special case. Zero padding. Zero head and tail characters. Zero offsets. 

const width = 10;
const height = 9;
const maxX = width - 1;
const maxY = height - 1;

for (let y = 0; y < height; y += 1) {
  const diagonal = y + (y > maxY / 2);
  for (let x = 0; x < width; x += 1) {
    let isEdge = x % maxX == 0 || y % maxY == 0;
    let isDiagonal = x == diagonal || x == maxX - diagonal;
    let doDraw = isEdge || isDiagonal;
    output.innerText += (doDraw ? "x" : " ");
  }
  output.innerText += "\n";
}

Edit: Alright, one offset was needed to perfectly match the meme. Added that and match the memes dimensions.

2

u/radobot 9h ago

That's wrong. You are printing an x always when x == y, but in the given pattern the position (5, 5) should be empty.

-1

u/AlwaysHopelesslyLost 9h ago

Who would have thought the meme is poorly thought out. Regardless, it doesn't add all of the complexity you implied. The fix is one more Boolean and updating the diagonal check 

let isLowerHalf = y > maxY / 2;
let isDiagonal = x == y + isLowerHalf || x == maxX - y - isLowerHalf;

4

u/da_Aresinger 9h ago
  • Downplays difficulty
  • does it wrong
  • blames the task

XD

Anyway, it's a good solution. But not the kind you can expect from a beginner who is just now learning nested loops.

Not to mention that conditional chains like that are usually code smell.

-1

u/AlwaysHopelesslyLost 8h ago

I typed it in 20 seconds on my phone on JS fiddle then had to retype it because the copy+paste doesn't work on jsfiddled mobile site and OCR are ligatures. 

Chained conditionals CAN be a code smell, but when used sparingly they can help reduce cognitive complexity.

The version a beginner would make is a version with 7 or 8 separate booleans and multiple print statements which is besides the point. Logically it is the same.

Ignoring that, the person I was replying to didn't know how to do it and explicitly said it needed a bunch of extra nonsense that it did not need. They were trying to be clever and failed.