Push is not a function?


#1

Exercise : https://www.codecademy.com/courses/learn-javascript-classes/projects/build-a-library?course_redirect=introduction-to-javascript

CODE :

class CD extends Media {
constructor(title,artist,trackCount) {
super(title)
this._artist = artist
this._trackCount = trackCount
}

get artist() {
return this._artist
}

get trackCount() {
return this._trackCount
}

addTrack(tracks) {
this._trackCount.push(tracks)
}

qualify(tracks) {
const trackCount = this.track
if (this._trackCount === 0 ) {
return ‘EMPTY TRACK’
} else if (this.trackCount === true) {
return track
}
}

const test = new CD(‘INVALID’ , ‘INVALID’ , 2)
console.log(’ ')
test.addTrack()
console.log(test.qualify())
}

PROBLEM :
/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:119
this._trackCount.push(tracks)
^

TypeError: this._trackCount.push is not a function
at CD.addTrack (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:119:22)
at Object. (/home/ccuser/workspace/learn-javascript-classes-build-a-library/app.js:161:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)


#2

2 is a number literal, not an array with a push() method.

To wit:

The latter does not agree with the first two.

Consider, the track count is a singular value, an integer, so easy to work with. Since adding tracks is incremental, we need an intermediary and a setter. What’s important is that the two are well coordinated.

    get trackCount () {
        return this._trackCount;
    }
    set trackCount (newCount) {
        this._trackCount = newCount;
    }
    incrementTrackCount () {
        this.trackCount += 1;
    }

#3

Thxn Bud , I’m just stressing my self out yesterday!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.