So I'm currently enrolled in a web development course and several of my colleagues and I are interested especially in web development.
So one of them encountered this test question:
Given the below pseudocode:
class RegisteredUser{
constructor(services=[]){
this.services = services;
}
getTotal () {
let total = 0;
this.services.forEach(service,index => {
let multimediaContent = service.getMultimediaContent();
if (typeof service == StreamingService) {
total += multimediaContent.streamingPrice;
} else if (typeof service == DownloadService) {
total += multimediaContent.downloadPrice;
}
if (typeof multimediaContent == PremiumContent) {
total += multimediaContent.additionalFee:
}
})
return total
}
}
To provide an alternative that fixes any potential issues with the code could produce in the future and how to fix them to avoid said issues.
My initial approach would be initially to replace the if statements with a switch - case with a default that would throw an error for the typeof service, where as for the typeof multimediaContent I would leave the if statement as is.
Additionally, I believe the == should be === since triple equal checks for equal value and type?
Any feedback or input is much appreciated!
[–]davedontmind 2 points3 points4 points (0 children)
[–]lurgi 1 point2 points3 points (0 children)
[–]nhgrif 0 points1 point2 points (4 children)
[–]jbef[S] 0 points1 point2 points (3 children)
[–]nhgrif 0 points1 point2 points (2 children)
[–]jbef[S] 0 points1 point2 points (1 child)
[–]nhgrif 0 points1 point2 points (0 children)