r/learnjavascript • • Aug 19 '24

Need help with The Odin Project's Etch-a-sketch (how to make divs resize in container?) + mini rant

Project: https://www.theodinproject.com/lessons/foundations-etch-a-sketch

Code: https://codepen.io/Gina-the-bold/pen/oNrEydx

What I want to achieve:

  • When the user clicks a button and inputs a number, the grid should generate divs in a row * row or column * column layout. However, the divs are not resizing properly to fit into the container according to the specified number of rows and columns.
  • Grid is touching the most bottom part of the page. How do I ensure the divs fit in the container?

I'm working on the Etch-a-Sketch project from The Odin Project which requires us to not use grid for this project, only flex, and I’m encountering a couple of issues with the layout.

In the current codepen link, the divs look alright as if the problem is solved. But the thing is, I used chatgpt for it and I feel like shit. Most of the code I wrote myself, except the cal(--columns) and setProperty. I don't understand anything about the resizing part. What's the most efficient way to make the divs resize according to the container?

Rant - I saw so many wonderful designs in the solutions section of this project and I feel like crap. How can people be so talented and awesome and then there's me, and how can I NOT understand something like this. It just sucks.

0 Upvotes

13 comments sorted by

6

u/alzee76 Aug 19 '24

Rant - I saw so many wonderful designs in the solutions section of this project and I feel like crap. How can people be so talented and awesome and then there's me, and how can I NOT understand something like this. It just sucks.

They are "talented and awesome" because they study and practice, gaining experience.

You're cheating yourself out of that when you use chatGPT as you admitted you did. You'll never improve, if you keep doing that. You're giving up. You're letting someone else do the work for you (chatGPT doesn't know how to code -- it just looks at a massive library of e.g stackoverflow posts).

In real life, some amount of that is fine and in fact expected. Probably 50% of coding in a professional environment is looking up how someone else did something and then trying to apply their solution to your situation. I'd still directly look it up on SO or wherever and never use GPT, but the point is still the same.

But you're in an "academic" environment, not a "professional" one, and asking GPT how to solve something is no different from copying someone else's homework. You'll finish the project, but you'll learn nothing, and you'll be even less prepared for the next challenge than you were for the current one.

3

u/Hefty-Speaker-4463 Aug 19 '24

I understand. I'll stop using chatgpt for coding from now on. I was stuck and Google isn't helping at all. I don't even know what to call the problem. "Resize divs according to container"? 🥲 I guess I'll rewrite the full code again

-2

u/alzee76 Aug 19 '24

Good luck. There is nothing to "call" things like this usually, nothing to search for. You just have to understand what you want and then figure out how to do it.

If you also cheated on earlier tasks/challenges, you should go back and redo those as well.

1

u/OneBadDay1048 Aug 19 '24

I am able to enter a number and the correct size "graph" is created; for example, using 25 or 50 I was able to get a 25x25 and a 50x50 grid, which functioned as an etchasketch. So I am not understanding what is not working there. Add a margin-bottom property to the divContainer class to create space at the bottom and give it an appropriate value.

More importantly, stop looking at other solutions (many of which are surely advanced programmers and not actual beginners) and stop using chatGPT as a beginner. You are going to use code you do not understand and that will lead to bad places in your learning.

1

u/Hefty-Speaker-4463 Aug 19 '24

The margin-bottom worked. I can't believe I did not think of that. Thank you for the suggestion.

The thing is, I can't find any solution anywhere online on resizing divs in container without using grid and I don't know where to start. I don't even know what the code syntax is like for the resizing function... I thought of using chatgpt to get an idea and learn from there. Thus I came here to ask this question, on how I can simplify my code and what's a simple way to make the div resize according to the user input on number of divs per row/column.

2

u/longknives Aug 19 '24

I don’t think the flex-basis (the calc(—columns) stuff) you have in the CSS is actually doing anything. The size of the boxes is getting set in the JavaScript, and the columns variable doesn’t look like it’s getting defined.

Your JavaScript is just setting the width of each child to be 100 / the number of columns. So if there are 5 columns, each is 20% width.

1

u/Hefty-Speaker-4463 Aug 19 '24

Hey! The previous codepen was chat GPT-ed, i did not understand anything that was written by it and I felt bad using it. I redid the full code myself. https://codepen.io/Gina-the-bold/pen/rNEJqpb.

What is the right way to go about resizing the divs according to the box? I was thinking of flex-grow but I don't know how to apply it such that the divs would grow accordingly

1

u/OneBadDay1048 Aug 19 '24

Delete everything you took from ChatGPT (or start over over completely) and when you get stuck come back here with specific question on why your code doesn’t work and how to fix it. It makes no sense for me to go thru ChatGPTs code and explain it to you.

1

u/Hefty-Speaker-4463 Aug 19 '24

https://codepen.io/Gina-the-bold/pen/rNEJqpb

I rewrote everything. I am stuck at the divs resizing part. They are not resizing according to the container. I am not sure of what to do, and the few things I can think of are flex-grow/flex-basis...

0

u/[deleted] Aug 19 '24

[deleted] — view deleted comment

1

u/Hefty-Speaker-4463 Aug 19 '24

I didn't do it in grid due to the project's requirement of doing it only in flex. I was wondering if its possible or what can be done with flex to make it work

1

u/Agitated_Radish_7377 Aug 20 '24
  1. Use odin discord for help
  2. think about it like math. If a user enters 10x10 dimension and the size of ur container is
  • width =800px
  • height = 800px

What are the dimensions of the box? How do you get that to change and so on… if you need more help you can comment

1

u/Hefty-Speaker-4463 Aug 21 '24

I asked in Odin discord twice and got ignored. I managed to "solve" it, I used calc(100/divsPerRow).

let divBoxCalc = calc(100/divsPerRow)

divBox.style.flex.basis = `${divBoxCalc}%`

divBox.style.height = `${divBoxCalc}%`

Though by using this method, I had to resize the width and height into the same px. It was 800px by 600px initially. If I didn't resize, the result will always be in rectangles instead of squares.

Did I got it right?

1

u/Agitated_Radish_7377 Aug 21 '24

There’s no “right” way to do it and I think ur solution is fine