We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
Help with JavaScript problem.
tincat
Posts: 935 Forumite
in Techie Stuff
M150 TMA 02 (09B) Hi all, I'm really stuck on a problem as I can't seem to get it to sink in after re reading the textbook and stuff. Could anyone tell me if I'm on the right track with my answers (in blue).
Two variables have been set as follows:
firstVar = 2
secondVar = 4
The following lines of JS are executed in sequence, and you should write down value of firstVar and secondVar after each line is executed:
a)firstVar = secondVar
b)secondVar = firstVar
c)firstVar = firstVar + secondVar / 2
As far as I can understand, it doesn't matter what the declared values are, as you can specify them as something else and from that point on, the variable takes on the new specified value
I have no idea whether I'm correct so if anyone can explain it to me I'd be really grateful. Thanks
Two variables have been set as follows:
firstVar = 2
secondVar = 4
The following lines of JS are executed in sequence, and you should write down value of firstVar and secondVar after each line is executed:
a)firstVar = secondVar
b)secondVar = firstVar
c)firstVar = firstVar + secondVar / 2
(a) firstVar = secondVar, so firstVar = 4.
(b) secondVar = firstVar, so firstVar = 4.
(c) firstVar = firstVar + secondVar/2.
=firstVar = 4 + (4/2)
=firstVar = 6
As far as I can understand, it doesn't matter what the declared values are, as you can specify them as something else and from that point on, the variable takes on the new specified value
I have no idea whether I'm correct so if anyone can explain it to me I'd be really grateful. Thanks
0
Comments
-
Yes, that seems about right0
-
As far as I can understand, it doesn't matter what the declared values are, as you can specify them as something else and from that point on, the variable takes on the new specified value

Yes BUT only for value / immutable types.
if you assign a reference type to another reference type both become a reference to the same object. so changing one variable will effect both.0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 352.1K Banking & Borrowing
- 253.6K Reduce Debt & Boost Income
- 454.2K Spending & Discounts
- 245.2K Work, Benefits & Business
- 600.8K Mortgages, Homes & Bills
- 177.5K Life & Family
- 259K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards