+ 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.

27th Nov 2021, 7:37 AM
Fan Mason
9 Respostas
+ 2
changePrice is a method of the Product function that returns the discounted price, that is, product costs $5 and discounted costs $4.9
27th Nov 2021, 6:18 PM
Solo
Solo - avatar
+ 2
The return is not really needed Fan Mason . You can see this here: https://code.sololearn.com/WTK68kk8ILHs/?ref=app
27th Nov 2021, 8:59 PM
JaScript
JaScript - avatar
+ 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));
27th Nov 2021, 7:37 AM
Fan Mason
+ 1
Your changePrice method returns nothing.
27th Nov 2021, 10:03 AM
Solo
Solo - avatar
+ 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“
27th Nov 2021, 11:10 AM
JaScript
JaScript - avatar
+ 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?
27th Nov 2021, 4:12 PM
Fan Mason
0
I mean why isnt it thischangePrice = function (price, discount) instead of just thischangePrice = function (discount)? Vasiliy
27th Nov 2021, 6:23 PM
Fan Mason
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.
29th Nov 2021, 12:41 AM
Emerson Prado
Emerson Prado - avatar
0
function main() { var prodID = readLine(); var price = parseInt(readLine(),10); var discount = parseInt(readLine(),10); var prod1= new Product(prodID, price); console.log(prod1.prodID + " price: " + prod1.price); prod1.changePrice(discount); console.log(prod1.prodID + " new price: " + prod1.price); } function Product(prodID, price) { this.prodID = prodID; this.price = price; this.changePrice = function(discount) { //your code goes here this.price = price - (price * (discount / 100)); } }
23rd Jul 2023, 9:47 AM
Amir Mohammednur Jemal
Amir Mohammednur Jemal - avatar