Syntax or Scope?

1
2
3
4
5
6
7
var number1;
 function draw(){
   if(number1 === undefined){
        var number2;
   }
   number2++;
 }

Find the mistake in the above JavaScript code. Click the type of error, followed by the line in which it occurs.

Syntax, Line 3 Scope, Line 4 Syntax, Line 6 Scope, Line 6

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

The answer is Scope, Line 6.

For those of you who don't know what scope is - The scope of a variable is the places in the code where it can be used.

For example :

The 'number2' was defined as a variable in the if condition from line 3 to 5. This means I can only use it inside that if condition.

This is called a local variable.

The variable 'number1' is defined outside of any function or condition, which allows it to be used everywhere.

This is called a global variable.

Thus, the answer is Scope, Line 6 as the local variable 'number2' is being used out of its scope.

I don't know very much about javascript

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

I guess it isn't available in Brilliant either, is it?

A Former Brilliant Member - 11 months, 3 weeks ago

But if you're interested, here's a free website - https://www.w3schools.com/

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

Yeah! This is a very good website. I use it when I get stuck, it’s good for website creation. If you use [ linktext ] (https://www.w3schools.com/ ) without spaces: linktext . And here is a good website: e-olymp

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member Thanks for the e-olymp and teaching me how to insert links. I'm guessing you already know about w3schools.

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member Yes. And what about you?(e-olymp) You can solve interesting problems.

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member E-Olymp is new for me. Thanks so much!

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member The biggest problem with this website there are a couple of good tasks in Ukrainian or Russian. What about this problem: link ? Brute-force or you can make a formula or recursive solution?

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member I'm more of a brute-force person than a think about it person. I'm impatient so i can't wait long enough to use algebra. i just start guessing.

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member Ok, but in these problems there are time limits :) My brother used the simple brute-force(start from 1) and it doesn't work. So you should use brute-force in the powers.

A Former Brilliant Member - 11 months, 3 weeks ago

@A Former Brilliant Member Is the answer 6? Because if n = 6 a, b = 2,3 and k=1

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member The k is given and you should calculate the n.

A Former Brilliant Member - 11 months, 3 weeks ago

@Páll Márton - You were wrong, Hamza Anushath is a girl, she told me.

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

I know it too:

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

bad picture

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member wher did you see this??

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@Páll Márton , did my account have any weird messages anywhere, my older cousin was using my pc for two minutes, but he said he didn't touch brilliant, but i don't trust him so did he do anything??

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

Please help me, if he did something stupid and gave wrong solutions i have to delete them.

A Former Brilliant Member - 11 months, 3 weeks ago

like this?

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

Damn it, i hope they don't see that, im on my ipad and i can't delete them for some reason.(stupid excuse of a cousin)

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member I have 5 cousins. And only one is normal.

A Former Brilliant Member - 11 months, 3 weeks ago

Log in to reply

@A Former Brilliant Member Bye! I don't have too much time.

A Former Brilliant Member - 11 months, 3 weeks ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...