Hey guys when i run this program it doesnt add my numbers, basically im supposed to print the salesman with the highest selling, and the most selling car. For example, when i type 50 amount for each car at the end it prints : 050505050 rather summing it up to a 200..
function salesPerson(name, id, amount) {
this.name = name;
this.id = id;
this.amount = amount;
};
//Create car array for each brand
var Car = new Array(4);
//Create array of salePersons
var Person = new Array(2);
//Every element in the array is a object type salesPerson
for (var i = 0; i < Person.length; i++) {
Person[i] = new salesPerson("", 0, 0);
}
var temp = 0;
var m = 0;
var a = 0;
var p = 0;
var b = 0;
for(var i = 0; i < Person.length; i++){
Person[i].name = prompt("Enter salesman name: ");
Person[i].id = prompt("Enter salesman id: ");
temp = prompt("Enter Mercedes-Benz amount: ");
Person[i].amount += temp;
m += temp;
temp = prompt("Enter Audi amount: ");
Person[i].amount += temp;
a += temp;
temp = prompt("Enter Porsche amount: ");
Person[i].amount += temp;
p += temp;
temp = prompt("Enter BMW amount: ");
Person[i].amount += temp;
b += temp;
}
var max = 0;
var name = "";
for (var i = 0; i < Person.length; i++) {
if (Person[i].amount > max) {
max = Person[i].amount;
name = Person[i].name;
}
}
alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);
The result of prompt is a string so + is concatenate. A very crude fix is to just do something like:
temp = +prompt("Enter Mercedes-Benz amount: ");
where the + causes a conversion. Note that this is very crude - it doesn't do any checking or error handling.
You need to convert temp
to number
; Use unary operator +
or parseInt()
for that.
for(var i = 0; i < Person.length; i++){
Person[i].name = prompt("Enter salesman name: ");
Person[i].id = prompt("Enter salesman id: ");
temp = +prompt("Enter Mercedes-Benz amount: ");
Person[i].amount += temp;
m += temp;
temp = +prompt("Enter Audi amount: ");
Person[i].amount += temp;
a += temp;
temp = +prompt("Enter Porsche amount: ");
Person[i].amount += temp;
p += temp;
temp = +prompt("Enter BMW amount: ");
Person[i].amount += temp;
b += temp;
}
Working Fiddle
before adding parse string as Integer.
function salesPerson(name, id, amount) { this.name = name; this.id = id; this.amount = amount; }; //Create car array for each brand var Car = new Array(4); //Create array of salePersons var Person = new Array(2); //Every element in the array is a object type salesPerson for (var i = 0; i < Person.length; i++) { Person[i] = new salesPerson("", 0, 0); } var temp = 0; var m = 0; var a = 0; var p = 0; var b = 0; for(var i = 0; i < Person.length; i++){ Person[i].name = prompt("Enter salesman name: "); Person[i].id = prompt("Enter salesman id: "); temp = parseInt(prompt("Enter Mercedes-Benz amount: ")); Person[i].amount += temp; m += temp; temp = parseInt(prompt("Enter Audi amount: ")); Person[i].amount += temp; a += temp; temp = parseInt(prompt("Enter Porsche amount: ")); Person[i].amount += temp; p += temp; temp = parseInt(prompt("Enter BMW amount: ")); Person[i].amount += temp; b += temp; } var max = 0; var name = ""; for (var i = 0; i < Person.length; i++) { if (Person[i].amount > max) { max = Person[i].amount; name = Person[i].name; } } alert("The best salesperson of the month is " + name + ", with the sales amount of $" + max);
When you take an input from the user, it is received as a string. You need to convert this input to an int. This can be done by using parseInt(value, radix)
. To know more about parseInt
function see http://www.w3schools.com/jsref/jsref_parseInt.asp
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.