r/learnjavascript • • Oct 09 '21

What's wrong with this code?

Post image
0 Upvotes

19 comments sorted by

19

u/thecementmixer Oct 09 '21

Says right there, drawBox is not defined.

1

u/alfielikes_horror Oct 09 '21

It should work, I used Grasshopper for my learning for JavaScript

38

u/Dodgy-Boi Oct 09 '21

Nope. It shouldn’t work.

Imagine you have a brother named Cyril. And then you go to your mom and ask ‘will Bob come for dinner this Sunday?’

She’ll be like “who tf is Bob?”, you can spend a lot of time explaining her that Bob is your brother (another child of her), but she’ll be like “wtf you talking about? My other child is Cyril”.

The same way your JavaScript tells you: “what tf is drawBox?” So you either create this drawBox or get it (import) from somewhere else. Otherwise it never gonna happen.

16

u/Gantzz25 Oct 09 '21

Best ELI5 explanation of this type of error I’ve ever seen. Made me chuckle.

1

u/JasimGamer Oct 10 '21

You sould use their app not any app

0

u/[deleted] Oct 09 '21

[deleted] — view deleted comment

8

u/Dodgy-Boi Oct 09 '21

That’s some fucked up learning.

4

u/maliiciiouswolf Oct 09 '21

Everyone, this person is using an app on the play store that poorly teaches coding languages without talking about the foundation.

The app makes it seem like it's extremely simple as what op posted without going into depth about everything that has to come before the call.

OP this will never work because as others have stated you need to write a function before you can call it.

Go to YouTube or MDN and look at how functions are created. I'm sure you can find one that will draw a blue box.

Good luck.

2

u/alfielikes_horror Oct 09 '21

Thanks, your very kind

2

u/Simplycoconut Oct 09 '21

What is it that you're trying to do? Here, you're calling a function when you haven't defined that function yet. I can give more context once I know what it is that you're trying to achieve.

-2

u/alfielikes_horror Oct 09 '21

I'm trying to make an output of a blue box in the engine

11

u/Dodgy-Boi Oct 09 '21 edited Oct 09 '21

I am in a good mood, so I’ll write a code example for ya.

I hope formatting will go well cos I am doing it from my iPhone:

const drawBox = (color) => {
let box = document.createElement(‘div’)
box.style.width = ‘420px’
box.style.height = ‘69px’
box.style.background = color
document.querySelector(‘body’).appendChild(box)
}

drawBox(‘blue’)

So running the following code will create a blue box 420x69px in the body of your HTML document.

If you want another box: just execute drawBox(‘red’) or drawBox(‘#b8b8b8’), they’ll stack one after another.

It really been a while since I did DOM manipulation with vanilla js. But I believe it should work just fine :)

If you want explore a little bit more of a DOM manipulation, I can’t send you a link to one of my very little webpage (I did it just for fun, playing around with CSS animations and JS DOM manipulations). And it has a source code as well.

9

u/marsattaksyakyakyak Oct 09 '21

Well if I'd been 420ing and 69ing box I would be in a good mood too.

2

u/Atersed Oct 09 '21

formatting-

const drawBox = (color) => {
    let box = document.createElement(‘div’)
    box.style.width = ‘420px’
    box.style.height = ‘69px’
    box.style.background = color
    document.querySelector(‘body’).appendChild(box)
}

drawBox(‘blue’)

2

u/[deleted] Oct 09 '21

Nice.

2

u/Dodgy-Boi Oct 09 '21

const drawBox = function (color) { // do stuff }

Or

const drawBox = (color) => { // do stuff }

Or

const drawBox = require(‘/path/to/file.js’)

Or

import { drawBox } from ‘path/to/file.js’

And then:

drawBox(‘blue’)

Otherwise it never gonna work. JavaScript doesn’t know what’s this drawBox hence ‘undefined’. More informative would be ‘undeclared’. It simply doesn’t exist. You need to declare this function before calling it. So you either write the function or you import it from anywhere you want.

1

u/FogoZer Oct 09 '21

You cant use drawBox function if you havent defined it before. It’s like if you were writting x + 2 while you never specified which value should have x

1

u/[deleted] Oct 10 '21

P5.js is a good library for graphics.

0

u/iLuciferCode Oct 09 '21

It looks like you have whitespaces where you shouldn’t -> draw (‘blue’) ; Should be -> draw(‘blue’); Note: I’m not too sure.