Javascript 34.2: Adding methods | Sololearn: Learn to code for FREE!

+1

Javascript 34.2: Adding methods

I haven’t been utilizing “main()” as the concept was never introduced, and it confuses me. I’ve been working around it through all challenges. I cannot figure this one out though. My attempt is in the comments.

11/27/2021 7:37:29 AM

Fan Mason

8 Answers

New Answer

+2

The return is not really needed Fan Mason . You can see this here: https://code.sololearn.com/WTK68kk8ILHs/?ref=app

+1

var x = readLine(); var y = parseInt(readLine(),10); var z = parseInt(readLine(),10); function Product(prodID, price, discount) { this.prodID = prodID; this.price = price; this.changePrice = function(discount) { this.price = price - (price * (discount / 100)); } } var prod1 = new Product(x, y, z); console.log(prod1.prodID + " price: " + prod1.price + "\n" + prod1.prodID + " new price: " + prod1.changePrice(z));

+1

Your changePrice method returns nothing.

+1

Hint: the prices change of prod1 should be done with the function changePrice (a setter function): console.log … „old price“ prod1.changePrice(price - (price*discount/100)); console.log … „new price“

+1

After adding return, the script is eorking Vasiliy and JaScript. Thanks, both of you. However, why is changePrice only a function of discount and not price too?

+1

changePrice is a method of the Product function that returns the discounted price, that is, product costs $5 and discounted costs $4.9

0

I mean why isnt it thischangePrice = function (price, discount) instead of just thischangePrice = function (discount)? Vasiliy

0

Fan Mason changePrice is a method of Product class, "this" is a reference to the object, and price is an attribute of the object. So method changePrice can already access price from object attributes, not needing it passed in.