0
Can someone please help me with this method challenge, the discount is not being calculated.
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.discount = discount; return price - (discount * price); } } Values for the first case are... Price : 1700; Discount : 15;
6 odpowiedzi
+ 2
Challenge has been solved.
Here is the solution:
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.discount = discount;
this.price = this.price - (this.discount/100 * this.price);
}
}
+ 1
Ipang it does not work, answer is incorrect
+ 1
Ipang we are all learning together. Your answer helped a bit. So thank you for your response
0
Try this in changePrice function
this.discount = discount;
this.price = this.price * ((100 - this.discount) / 100);
(Edited - Wrong calculation)
0
Brandon Banks
Sorry I was giving a wrong calculation.
Good job with solution 👍
0
Same solution but maybe this is more neater :))
function main() {
var prodID = readLine();
var price = parseInt(readLine(),10);
var discount = parseInt(readLine(),10);
//call out
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) {
this.discount = discount ;
//your code goes here
this.discount = discount/100;
this.price = this.price -(this.discount * this.price)
}
}