62 570
Assignments Done
99%
Successfully Done
In June 2018

Answer to Question #3432 in AJAX | JavaScript | HTML | PHP for sharifah

Question #3432
QUESTION 1
Answer in javascript ..You are given a table that shows the total number of cars sold by a local car manufacturer in Johor Bharu for the first six months in the year 2008.

Month Jan Feb Mar Apr May Jun
Number of cars sold (units) 191 196 269 308 236 145
Based on the figures given above, you required to write a Java program using array which can perform the following:

• Average number of cars sold
• The month that achieve lowest sales
Expert's answer
var sales = new Object; // The associative array with the cars sold during the specific month
sales["Jan"] = 191;
sales["Feb"] = 196;
sales["Mar"] = 269;
sales["Apr"] = 308;
sales["May"] = 236;
sales["Jun"] = 145;

// This function calculates the average number of cars sold
function find_average(pSales)
{
var totalSales = 0;
var count = 0;
for (var numberOfSales in pSales)
{
& totalSales += pSales[numberOfSales];
& count++;
}
return (totalSales / count) - (totalSales % count);
}

// This function finds the month that achieve lowest sales
function find_lowest_sales_month(pSales)
{
var lowestSalesMonth;
var lowestSales = 1000000;
for (var month in pSales)
{
& if (pSales[month] < lowestSales)
& {
& lowestSalesMonth = month;
& lowestSales = pSales[month];
& }
}
return lowestSalesMonth;
}


document.write("The average number of cars sold is: " + find_average(sales) + "
");
document.write("The month that achieve lowest sales is: " + find_lowest_sales_month(sales) + "
");

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions