On the project where we must create a class media with subclasses book, movie, cd. I get an error on the addRating method.
Heres my code so far
class Media {
constructors(title){
this._title = title;
this._ratings = [];
this._isCheckedOut = false;
}
get title(){
return this._title;
}
get ratings(){
return this._ratings;
}
get isCheckedOut(){
return this._isCheckedOut;
}
toggleCheckedOutStatus(){
if(this._isCheckedOut === true){
this._isCheckedOut = false;
}else {
this._isCheckedOut = true;
}
}
getAverageRating(){
let ratSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0) / this._ratings.length;
return ratSum.toFixed(2);
}
addRating(adding){
this._ratings.push(adding);
}
set isCheckedOut(checked){
this.isCheckedOut = checked;
}
}
class Book extends Media {
constructor(title, author, pages){
super(title);
this._author = author;
this._pages = pages;
}
get author(){
return this._author;
}
get pages(){
return this._pages;
}
}
class Movie extends Media{
constructor(title, director, runTime){
super(title);
this._director = director;
this._runTime = runTime;
}
get director(){
return this._director;
}
get runTime(){
return this._runTime;
}
}
const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);
historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating([4,5,5]);
console.log(historyOfEverything.getAverageRating());
I get the error TypeError: Cannot read property ‘push’ of undefined
at Book.addRating
Hello, @blogpro21075 , and welcome to the forums.
Could you post your entire code? Be sure to format it properly using the preformatted text button </>. See this post for more details on formatting code.
Well, if you add in a console.log() to inspect the value in question, you’ll find something interesting:
Try it, and compare the output to what you would expect.
Hint
Notice anything missing from the output?
this: Book { _author: ‘Bill Bryson’, _pages: 544, _isCheckedOut: true }
Where are _title
and _ratings
?
Why would they be missing?
Where is the code that is supposed to add those properties?
Is anything amiss there?
Thanks! I didn’t know we needed to list title and ratings in the extended constructor since it was already in the super.
You don’t. Look closely at the constructor for your Media class:
Hint
The properties in this constructor were not added due to the misspelling.
class Media {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._ratinggs = [];
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get ratings() {
return this._ratings;
}
set isCheckedOut(value) {
this._isCheckedOut = value;
}
toggleCheckOutStatus() {
this.isCheckedOut = !this.isCheckedOut;
}
getAverageRating() {
let ratingsSum = this.ratings.reduce((accumulator, rating) => accumulator + rating
);
return ratingSum / this.ratings.length;
}
addRating(value) {
this.ratings.push(value);
}
}
class Book extends Media {
constructor(author, title, pages) {
super(title);
this._author = author;
this._pages = pages;
}
get author() {
return this._author;
}
get pages() {
return this._pages;
}
}
class Movie extends Media {
constructor(director, title, runTime) {
super(title);
this._director = director;
this._runTime = runTime;
}
get director() {
return this._director;
}
get runTime() {
return this._runTime;
}
}
const historyOfEverything = new Book(
"Bill Bryson",
"A Short History of Nearly Everything",
544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);[quote="blogpro21075, post:1, topic:561435, full:true"]
On the project where we must create a class media with subclasses book, movie, cd. I get an error on the addRating method.
Heres my code so far
```
class Media {
constructors(title){
this._title = title;
this._ratings = [];
this._isCheckedOut = false;
}
get title(){
return this._title;
}
get ratings(){
return this._ratings;
}
get isCheckedOut(){
return this._isCheckedOut;
}
toggleCheckedOutStatus(){
if(this._isCheckedOut === true){
this._isCheckedOut = false;
}else {
this._isCheckedOut = true;
}
}
getAverageRating(){
let ratSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0) / this._ratings.length;
return ratSum.toFixed(2);
}
addRating(adding){
this._ratings.push(adding);
}
set isCheckedOut(checked){
this.isCheckedOut = checked;
}
}
class Book extends Media {
constructor(title, author, pages){
super(title);
this._author = author;
this._pages = pages;
}
get author(){
return this._author;
}
get pages(){
return this._pages;
}
}
class Movie extends Media{
constructor(title, director, runTime){
super(title);
this._director = director;
this._runTime = runTime;
}
get director(){
return this._director;
}
get runTime(){
return this._runTime;
}
}
const historyOfEverything = new Book('A Short History of Nearly Everything', 'Bill Bryson', 544);
historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating([4,5,5]);
console.log(historyOfEverything.getAverageRating());
I get the error TypeError: Cannot read property 'push' of undefined
at Book.addRating
[/quote]
I am not getting
what you are trying to say here. I followed the tutorial line by line. Can you elaborate? My code is below.
class Media {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._ratinggs = [];
}
get title() {
return this._title;
}
get isCheckedOut() {
return this._isCheckedOut;
}
get ratings() {
return this._ratings;
}
set isCheckedOut(value) {
this._isCheckedOut = value;
}
toggleCheckOutStatus() {
this.isCheckedOut = !this.isCheckedOut;
}
getAverageRating() {
let ratingsSum = this.ratings.reduce((accumulator, rating) => accumulator + rating
);
return ratingSum / this.ratings.length;
}
addRating(value) {
this.ratings.push(value);
}
}
class Book extends Media {
constructor(author, title, pages) {
super(title);
this._author = author;
this._pages = pages;
}
get author() {
return this._author;
}
get pages() {
return this._pages;
}
}
class Movie extends Media {
constructor(director, title, runTime) {
super(title);
this._director = director;
this._runTime = runTime;
}
get director() {
return this._director;
}
get runTime() {
return this._runTime;
}
}
const historyOfEverything = new Book(
"Bill Bryson",
"A Short History of Nearly Everything",
544);
historyOfEverything.toggleCheckOutStatus();
console.log(historyOfEverything.isCheckedOut);
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
undefined
has no property called push
. This is telling you that you are trying to use the push
method on something that isn’t defined (doesn’t exist in the current scope). The line throwing the error is below.
this.ratings
is calling the getter here:
That looks fine, so we keep following the source. Where is this._ratings
defined?
Looks like it was intended to be here, but there’s a problem
Thank you for the help. I am getting ready for interviews and I needed some help with making some projects for my portfolio. Preferably, I would like one big project so I can walk them through the process. I am in a cohort right now. We have to work on a 100 hours project. We are primarily working on the MERN stack which is Mongo DB, Express, React, and Node. We have to make a full-stack application. I have an idea to do a note-taker for Web Developers and UX Designers. I haven’t really thought it through.
I need some smaller projects to explain what I am learning and show them that I know how to code. I am concerned about the presentation. I want my work to look polished and solve a real problem.
I was wondering if you were available for a coffee chat for like 15 minutes or so. I would like to chat and learn more about your journey in coding.
Wayne Tucker
| midlindner June 3 |
text7520705075:
I get the error TypeError: Cannot read property 'push' of undefined
at Book.addRating
undefined
has no property called push
. This is telling you that you are trying to use the push
method on something that isn’t defined (doesn’t exist in the current scope). The line throwing the error is below.
text7520705075:
addRating(value) {
this.ratings.push(value); //look in your code, and see what this refers to
}
this.ratings
is calling the getter here:
text7520705075:
get ratings() {
return this._ratings; //from here we need to look at the _ratings property
}
That looks fine, so we keep following the source. Where is this._ratings
defined?
Looks like it was intended to be here, but there’s a problem
midlindner
Split this topic
October 13, 2022, 4:12pm
12
A post was split to a new topic: Build Library Project Issue