+ 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.
9 odpowiedzi
+ 2
changePrice is a method of the Product function that returns the discounted price, that is, product costs $5 and discounted costs $4.9
+ 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“
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.
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));
}
}