RE: Variables in JavaScript

avatar

You are viewing a single comment's thread:

Very good. Perhaps not include the line numbers in your code sharing though.

Here's a test for you...how do you swap the contents of these two variables?

var a = 1;
var b = 2;

// how would you make a = b and b = a?


0
0
0.000
9 comments
avatar

Maybe this way?

var myNumber=2;
var myNumber=1;

Posted using Partiko Android

0
0
0.000
avatar

'a' and 'b' could contain anything, so we can't just assign a = 2, b = 1. Also in your answer, you have declared the variable name 'myNumber' twice. I don't think that is permitted in that context.

Here's the solution:

var a = 1;
var b = 2;

// swap 'a' and 'b'

var temp = a;  // contains 1
a = b;  // contains 2
b = temp;  // contains 1

Does that make sense?

0
0
0.000
avatar

I see... It's above my pay-grade, haha! Tomorrow I will pick up a book JavaScript for Kids I ordered from Amazon. Do you prefer to study from books or from online sources?

0
0
0.000
avatar

I see... no numbering in code sharing?

Posted using Partiko Android

0
0
0.000
avatar

I think so. Because if someone copies and pastes the code snippet, they will have to remove the line numbers to get it to work.

0
0
0.000
avatar

Thanks for the tip again! I'm sorry I can't return the favor by teaching you anything new... You know everything I know...

0
0
0.000
avatar

That’s not true! You are the master of organ improvisation! I wish I could improvise even basic things 😢

Posted using Partiko iOS

0
0
0.000