Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialmickaelescroignard3
3,996 Pointssetter function gives me: Invalid left-hand side in assignment
in the following challenge: Inside the major() setter method, set the student's major to a backing property "_major". If the student's level is Junior or Senior, the value of the backing property should be equal to the parameter passed to the setter method. If the student is only a Freshman or Sophomore, set the "_major" backing property equal to 'None'.
I get the Invalid left-hand side in assignment error message. This is my Code: set major(majorName) { if ( this.level = 'Senior' || this.level = 'Junior' ) { this._major = majorName; } else { this._major = 'None'; } }
Any idea what's wrong with it? My tired eyes cant see the problem :(
Thanks in advance. M.
class Student {
constructor(gpa, credits){
this.gpa = gpa;
this.credits = credits;
}
stringGPA() {
return this.gpa.toString();
}
set major(majorName) {
if ( this.level = 'Senior' || this.level = 'Junior' ) {
this._major = majorName;
} else {
this._major = 'None';
}
}
get level() {
if (this.credits > 90 ) {
return 'Senior';
} else if (this.credits > 60) {
return 'Junior';
} else if (this.credits > 30) {
return 'Sophomore';
} else {
return 'Freshman';
}
}
}
var student = new Student(3.9, 60);
2 Answers
Joseph Yhu
PHP Development Techdegree Graduate 48,638 PointsThe problem is that you are trying to assign either the string 'Senior'
or the string 'Junior'
to this.level
instead checking if this.level
is equal to either 'Senior'
or 'Junior'
. Remember, single equals is for assignment; you'd need double or triple equals for comparison.
mickaelescroignard3
3,996 PointsI found a work around if any of you have a similar issue in the future... Instead of using: if ( this.level = 'Senior' || this.level = 'Junior' ) { I just used: if ( this.credits > 60 ) {
:)
jaya annurjaya
Courses Plus Student 2,247 Pointsthank you οΌit's useul for me
mickaelescroignard3
3,996 Pointsmickaelescroignard3
3,996 Pointsthank you.. that explains why my original code didnt work :) should have remembered in JS, = is === :)
Thanks again!