Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,

I just ran my program in Chrome, and it wouldn't work, so I selected the Chrome console with F12, and this came up: Uncaught ReferenceError: Invalid left-hand side in assignment

I have no idea how to fix this, please help.

JavaScript
<html>
<body>
<script>
// Project - Level 2 JavaScript

// Array for IceCreams

var i;
var icecream = new Array();
icecream[0] = new Array();
icecream[0] = ['name'] = 'chocbar';
icecream[0] = ['price'] = parseFloat (2.50);
icecream[1] = new Array();
icecream[1] = ['name'] = 'joybar';
icecream[1] = ['price'] = parseFloat(2.75);
icecream[2] = new Array();
icecream[2] = ['name'] = 'trumpet';
icecream[2] = ['price'] = parseFloat(3.00);
icecream[3] = new Array();
icecream[3] = ['name'] = 'magnum';
icecream[3] = ['price'] = parseFloat(4.00);

for (i= 0; i < icecream.length; i++)
{
alert (icecream[i]['name'])
alert ('$' + icecream[i]['price'])
}
console.log(c);
</script>
</body>
</html>


Thanks.
Posted
Comments
vbmike 13-May-15 21:43pm    
It probably means you cannot use two '=' signs in your statement? icecream[0] = ['name'] = 'chocbar'; statement may not be valid.
Garlotch 13-May-15 22:44pm    
Thanks for the info. Is it a general rule, that you can't use two '=' signs in a single statement?
Sergey Alexandrovich Kryukov 13-May-15 23:42pm    
No! Isn't it obvious what part is wrong?
—SA

The problem is more than trivial. You don't need any "info", you need just some logical thinking.

Two assignments are irrelevant here. This is correct code:
JavaScript
var a, b;
a = b = 2

Why? Because you simply assign two objects to two variable; it is equivalent to
JavaScript
b = 2; a = b


In your invalid expression the invalid part is not the combination, but rightmost part: ['name'] = 'chocbar';. Indeed, what could it possibly mean? The object on left does not provide a reference to which you can use to access the object after the assignment. In C++ terminology, the expression ['name'] is "not an l-value", cannot appear on the left of assignment operator.

To me, it's quite hard to imagine how such a weird idea could visit anyone's head. Care to explain your logic behind that? :-)

—SA
 
Share this answer
 
v3
Comments
Garlotch 14-May-15 23:36pm    
Sorry Sergey, I just copied to the code from my instructor's screenshot. I definitely typed it wrong. Thanks for the answer though!! :D
Sergey Alexandrovich Kryukov 14-May-15 23:37pm    
My pleasure.
Good luck, call again.
—SA
may be you can try this :

C#
// constructor
function SetValue (name, price) {
    this.name = name ;
    this.price = parseFloat(price) ;
}

// instance
var icecream  = new Array() ;

icecream [0] =   new SetValue("chocbar",2.50) ;
icecream [1] =   new SetValue("joybar",2.75) ;
icecream [2] =   new SetValue("trumpet",3.00) ;
icecream [3] =   new SetValue("magnum",4.00) ;

for (var i= 0; i < icecream.length; i++)
{
    alert (icecream[i].name) ;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900