/    Sign up×
Community /Pin to ProfileBookmark

Help with declaring and working percentages

Hi,

I need help in how to get my program to output the
information required.

I want the program to display the information as shown below:

book1 sales: a—% of total sales: b%…of total
buyers: c%
book2 sales: a—% of total sales: b%…of total
buyers: c%
book3 sales: a—% of total sales: b%…of total
buyers: c%
Book with highest sales is ..d.. with ..e.. sales.

• a = book sales figure (entered by user)
• b = percentageSales1Array
• c =percentageSales2Array
• d = book with the highest sales
• e = sales figure of book with highest sales

How do i declare the percentage and variable in order to get the code working? What am i doing wrong?

Please see code written so far. Help needed.

Thanks

<HTML>
<HEAD>
<TITLE>
Book_sales
</TITLE>
<SCRIPT LANGUAGE = “JavaScript”>
/* Program to calculate total number of books sold and display the book with highest sales */

[B]var[/B] bookArray = new Array(3); // Store for books with 8 elements
[B]var[/B] totalBuyers; // store for number of buyers that visited amazon during that period
[B]var[/B] totalSales; // Store for total book sales
[B]var [/B]highestBookSalesIndex; // Store for highest number of books sold
[B]var [/B]bookSalesArray = [1,2,3]; //store for single book sale
[B]var [/B]percentageSales1Array = 0; // Store for percentage sales after multiplying book sales with 100 and dividing by total sales
[B]var[/B] percentageSales2Array =0; // Store for percentage sales after multiplying book sales with 100 and dividing by total buyers

/* loop to read in and add the 8
books to book array *
/
[B]for[/B] ([B]var[/B] count = 0; count < 3; count =count + 1)
{
totalBuyers = parseFloat(window.prompt(‘Please enter the total number of buyers’,”));
totalSales = parseFloat(window.prompt(‘Please enter the total number of all books sold’,”));

[B]for[/B] ([B]var [/B]count = 0; count < 3; count = count + 1)
{
bookSalesArray = parseFloat(window.prompt(‘Please enter the sales for book ‘ + (count + 1),”));

/* determines book with highest sales */
highestBookSalesIndex = 0;
[B]for[/B] ([B]var[/B] book = 1; book < bookSalesArray.length; book = book + 1)
{
[B] if [/B](bookSalesArray[book] > BookSales[highestBookSalesIndex])
{
highestBookSalesIndex = book;
}
}
}

/* calculates percentage of each book sold using total sales of all the books*/

[B]for[/B] ([B]var[/B] book = 0; book < bookSalesArray.length; book = book + 1)
{
percentageSales1Array[book] = Math.round2dp((bookSalesArray[book] / totalSales) * 100)
}
/*
calculates percentage of each book sold using total buyers */

[B]for[/B] ([B]var[/B] book = 0; book < bookSalesArray.length; book = book + 1)
{
percentageSales2Array[book] = Math.round2dp((bookSalesArray[book] / totalBuyers) *
100)
}
}

document.write(‘Total buyers: ‘ + ” + totalBuyers + ‘Total sales: ‘ + ” + totalSales + ‘<BR>’);
document.write(‘book1 ‘ + ” + ‘sales: ‘ + bookSalesArray[book] + ” + ‘% ‘ + ‘of total sales: ‘ + percentageSales1Array[book + 1] + ” + ‘% ‘ + ‘of total buyers: ‘ + percentageSales2Array[book + 1] + ” + ‘% ‘ + ‘<BR>’);
document.write(‘book2 ‘ + ” + ‘sales: ‘ + bookSalesArray[book] + ” + ‘% ‘ + ‘of total sales: ‘ + percentageSales1Array[book + 1] + ” + ‘% ‘ + ‘of total buyers: ‘ + percentageSales2Array[book + 1] + ” + ‘% ‘ + ‘<BR>’);
document.write(‘book3 ‘ + ” + ‘sales: ‘ + bookSalesArray[book] + ” + ‘% ‘ + ‘of total sales: ‘ + percentageSales1Array[book + 1] + ” + ‘% ‘ + ‘of total buyers: ‘ + percentageSales2Array[book + 1] + ” + ‘% ‘ + ‘<BR>’);
document.write(‘book4 ‘ + ” + ‘sales: ‘ + bookSalesArray[book] + ” + ‘% ‘ + ‘of total sales: ‘ + percentageSales1Array[book + 1] + ” + ‘% ‘ + ‘of total buyers: ‘ + percentageSales2Array[book + 1] + ” + ‘% ‘ + ‘<BR>’);
document.write(‘book5 ‘ + ” + ‘sales: ‘ + bookSalesArray[book] + ” + ‘% ‘ + ‘of total sales: ‘ + percentageSales1Array[book + 1] + ” + ‘% ‘ + ‘of total buyers: ‘ + percentageSales2Array[book + 1] + ” + ‘% ‘ + ‘<BR>’);
document.write(‘Book with highest sales is ‘ + highestBookSalesIndex[book] + ” + ‘with ‘ + highestBookSalesIndex + ‘% ‘ + ‘<BR>’);

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 22.2009 — Well, I didn't cleanup and fix everything, but this is a lot closer to what you want:
[code=html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Book Sales</title>
</head>

<body>
<script type="text/javascript">
/* Program to calculate total number of books sold and display the book with highest sales */
var bookArray = new Array(3); // Store for books with 8 elements
var totalBuyers; // store for number of buyers that visited amazon during that period
var totalSales; // Store for total book sales
var highestBookSalesIndex; // Store for highest number of books sold
var bookSalesArray = new Array(3); //store for single book sale
var percentageSales1Array = new Array(3); // Store for percentage sales after multiplying book sales with 100 and dividing by total sales
var percentageSales2Array = new Array(3); // Store for percentage sales after multiplying book sales with 100 and dividing by total buyers

/* loop to read in and add the 8 books to book array */
totalBuyers = parseFloat(window.prompt('Please enter the total number of buyers',''));
totalSales = parseFloat(window.prompt('Please enter the total number of all books sold',''));

for (var count = 0; count < 3; ++count)
{
bookSalesArray[count] = parseFloat(window.prompt('Please enter the sales for book ' + (count + 1),''));
}
/* determines book with highest sales */
highestBookSalesIndex = 0;
for (var book = 1; book < bookSalesArray.length; ++book)
{
if (bookSalesArray[book] > bookSalesArray[highestBookSalesIndex])
{
highestBookSalesIndex = book;
}
}

/* calculates percentage of each book sold using total sales of all the books*/
for (var book = 0; book < bookSalesArray.length; ++book)
{
percentageSales1Array[book] = (bookSalesArray[book] / totalSales) * 100;
}
/* calculates percentage of each book sold using total buyers */
for (var book = 0; book < bookSalesArray.length; ++book)
{
percentageSales2Array[book] = (bookSalesArray[book] / totalBuyers) * 100;
}

document.write('Total buyers: ', totalBuyers, ' Total sales: ', totalSales, '<BR>');
document.write('book1 ', 'sales: ', bookSalesArray[0], ' percent of total sales: ', percentageSales1Array[0], '&#37; ', 'of total buyers: ', percentageSales2Array[0], '% ', '<BR>');
document.write('book2 ', 'sales: ', bookSalesArray[1], ' percent of total sales: ', percentageSales1Array[1], '% ', 'of total buyers: ', percentageSales2Array[1], '% ', '<BR>');
document.write('book3 ', 'sales: ', bookSalesArray[2], ' percent of total sales: ', percentageSales1Array[2], '% ', 'of total buyers: ', percentageSales2Array[2], '% ', '<BR>');
document.write('Book with highest sales is ', highestBookSalesIndex, ' with ', highestBookSalesIndex, '% ', '<BR>');
</script>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@HazeleyesauthorJan 22.2009 — Hi,

Thanks for the help. I sincerely appreciate it.

One more thing please..... i tried rounding the percentage to 2 decimal places and kept getting error message. How do i round off the output percentages (in the document.write) to 2 decimal places?

Thanks
Copy linkTweet thisAlerts:
@HazeleyesauthorJan 22.2009 — Hi,

I have fixed the round off code and it works fine now.

But the find highest value is not working for now.

I want to find the book with highest sales so that display will show both the book and the highest sales figure for that particular book.

Below is the code for highestBookSalesIndex:

/* determines book with highest sales */

highestBookSalesIndex = bookSalesArray[0];

[B]for[/B] ([B]var[/B] book = 1; book < bookSalesArray.length; ++book)

{

[B]if [/B](bookSalesArray[book] > bookSalesArray[highestBookSalesIndex])

{

highestBookSalesIndex = book;

}

}

document.write('Book with highest sales is ', highestBookSalesIndex, ' with ', highestBookSalesIndex, '% ', '<BR>');

Please what am i doing wrong/did wrong or what is wrong as it doesn't display the book with the highest sales and the highest sales figure?

The complete code is as follows:

<html>

<head>

<meta http-equiv="Content-Language" content="en-us">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Book_sales</title>

</head>

<body>

<script type="text/javascript">

/* Program to calculate total number of books sold and display the book with highest sales */

[B]var[/B] bookArray = new Array(3); // Store for books with 3 elements

[B]var[/B] totalBuyers; // store for number of buyers that visited amazon during that period

[B]var[/B] totalSales; // Store for total book sales

[B]var[/B] highestBookSalesIndex; // Store for highest number of books sold

[B]var[/B] bookSalesArray = new Array(3); //store for single book sale

[B]var [/B]percentageSales1Array = new Array(3); // Store for percentage sales after multiplying book sales with 100 and dividing by total sales

[B]var [/B]percentageSales2Array = new Array(3); // Store for percentage sales after multiplying book sales with 100 and dividing by total buyers

/* loop to read in and add the 8 books to book array */

totalBuyers = parseFloat(window.prompt('Please enter the total number of buyers',''));

totalSales = parseFloat(window.prompt('Please enter the total number of all books sold',''));

[B]for[/B] ([B]var[/B] count = 0; count < 3; ++count)

{

bookSalesArray[count] = parseFloat(window.prompt('Please enter the sales for book ' + (count + 1),''));

}

/* determines book with highest sales */

highestBookSalesIndex = bookSalesArray[0];

[B]for [/B]([B]var [/B]book = 1; book < bookSalesArray.length; ++book)

{

[B]if [/B](bookSalesArray[book] > bookSalesArray[highestBookSalesIndex])

{

highestBookSalesIndex = book;

}

}

/* calculates percentage of each book sold using total sales of all the books*/

[B]for[/B] ([B]var[/B] book = 0; book < bookSalesArray.length; ++book)

{

percentageSales1Array[book] = Math.round((bookSalesArray[book] / totalSales) * 100);

}

/*
calculates percentage of each book sold using total buyers */

[B]for[/B] ([B]var[/B] book = 0; book < bookSalesArray.length; ++book)

{

percentageSales2Array[book] = Math.round((bookSalesArray[book] / totalBuyers) *
100);

}

document.write('Total buyers: ', totalBuyers, ' Total sales: ', totalSales, '<BR>');

document.write('book1 ', 'sales: ', bookSalesArray[0], ' percent of total sales: ', percentageSales1Array[0], '% ', 'of total buyers: ', percentageSales2Array[0], '% ', '<BR>');

document.write('book2 ', 'sales: ', bookSalesArray[1], ' percent of total sales: ', percentageSales1Array[1], '% ', 'of total buyers: ', percentageSales2Array[1], '% ', '<BR>');

document.write('book3 ', 'sales: ', bookSalesArray[2], ' percent of total sales: ', percentageSales1Array[2], '% ', 'of total buyers: ', percentageSales2Array[2], '% ', '<BR>');

document.write('Book with highest sales is ', highestBookSalesIndex, ' with ', highestBookSalesIndex, '% ', '<BR>');

</script>

</body>

</html>

MrNobody has earlier helped me out. My thanks and regards to him for pointing me in the right direction and i sincerely appreciate. My remaining concern now is how to get the program to display the highest sales figure and the book with the highest sales figure.

Thanks
Copy linkTweet thisAlerts:
@MrNobodyJan 22.2009 — [CODE]/* determines book with highest sales */
highestBookSalesIndex = bookSalesArray[0];
[B]for[/B] ([B]var[/B] book = 1; book < bookSalesArray.length; ++book)
{
[B]if [/B](bookSalesArray[book] > bookSalesArray[highestBookSalesIndex])
{
highestBookSalesIndex = book;
}
}

document.write('Book with highest sales is ', highestBookSalesIndex, ' with ', highestBookSalesIndex, '% ', '<BR>');[/CODE]


Please what am i doing wrong/did wrong or what is wrong as it doesn't display the book with the highest sales and the highest sales figure?[/QUOTE]

As far as it goes, that code is correct for displaying the [U]array index value[/U] of the book with the highest sales.
Copy linkTweet thisAlerts:
@HazeleyesauthorJan 23.2009 — Hi,

I have tried but it still doesn't work.

The output below is what it gives me:

Total buyers: 5000 Total sales: 3000

book1 sales: 1500 percent of total sales: 50% of total buyers: 30%

book2 sales: 1000 percent of total sales: 33% of total buyers: 20%

book3 sales: 500 percent of total sales: 17% of total buyers: 10%

Book with highest sales is 1500 with 1500%

Although it displays the highest sales but it doesn't display the book with highest sales correctly.

Is there any way to make it display the book with the highest sales as well?

Thanks
Copy linkTweet thisAlerts:
@MrNobodyJan 23.2009 — You'd have to have an array of your book names.
Copy linkTweet thisAlerts:
@HazeleyesauthorJan 24.2009 — Hi,

Thanks for the help.

One last thing, i have rounded my output to 1 decimal place using Math.round.

My question is: If i want to round my output to 2 or three decimal places, how should i do it?

Can i use Math.round2dp or Math.round3dp? If so, where should i use the code inside my program?

That is, if i want to display the results in 2 or 3 decimal places?

See attached code.

Thanks

<html>

<head>

<meta http-equiv="Content-Language" content="en-us">

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Book_sales</title>

</head>

<body>

<script type="text/javascript">

/* Program to calculate total number of books sold and display the book with highest sales */

[B]var[/B] bookArray = new Array(3); // Store for books with 3 elements

[B]var[/B] totalBuyers; // store for number of buyers that visited amazon during that period

[B]var[/B] totalSales; // Store for total book sales

[B]var[/B] highestBookSalesIndex; // Store for highest number of books sold

[B]var[/B] bookSalesArray = new Array(3); //store for single book sale

[B]var[/B] percentageSales1Array = new Array(3); // Store for percentage sales after multiplying book sales with 100 and dividing by total sales

[B]var [/B]percentageSales2Array = new Array(3); // Store for percentage sales after multiplying book sales with 100 and dividing by total buyers

/* loop to read in and add the 8 books to book array */

totalBuyers = parseFloat(window.prompt('Please enter the total number of buyers',''));

totalSales = parseFloat(window.prompt('Please enter the total number of all books sold',''));

[B]for[/B] ([B]var[/B] count = 0; count < 3; ++count)

{

bookSalesArray[count] = parseFloat(window.prompt('Please enter the sales for book ' + (count + 1),''));

}

/* determines book with highest sales */

highestBookSalesIndex = bookSalesArray[0];

[B]for [/B]([B]var[/B] book = 1; book < bookSalesArray.length; ++book)

{

if (bookSalesArray[book] > bookSalesArray[highestBookSalesIndex])

{

highestBookSalesIndex = book;

}

}

/* calculates percentage of each book sold using total sales of all the books*/

[B]for[/B] ([B]var[/B] book = 0; book < bookSalesArray.length; ++book)

{

percentageSales1Array[book] = Math.round((bookSalesArray[book] / totalSales) * 100);

}

/*
calculates percentage of each book sold using total buyers */

[B]for[/B] ([B]var[/B] book = 0; book < bookSalesArray.length; ++book)

{

percentageSales2Array[book] = Math.round((bookSalesArray[book] / totalBuyers) *
100);

}

document.write('Total buyers: ', totalBuyers, ' Total sales: ', totalSales, '<BR>');

document.write('book1 ', 'sales: ', bookSalesArray[0], ' percent of total sales: ', percentageSales1Array[0], '&#37; ', 'of total buyers: ', percentageSales2Array[0], '% ', '<BR>');

document.write('book2 ', 'sales: ', bookSalesArray[1], ' percent of total sales: ', percentageSales1Array[1], '% ', 'of total buyers: ', percentageSales2Array[1], '% ', '<BR>');

document.write('book3 ', 'sales: ', bookSalesArray[2], ' percent of total sales: ', percentageSales1Array[2], '% ', 'of total buyers: ', percentageSales2Array[2], '% ', '<BR>');

document.write('Book with highest sales is ', highestBookSalesIndex, ' with ', highestBookSalesIndex, '% ', '<BR>');

</script>

</body>

</html>
Copy linkTweet thisAlerts:
@MrNobodyJan 25.2009 — One last thing, i have rounded my output to 1 decimal place using Math.round.

My question is: If i want to round my output to 2 or three decimal places, how should i do it?[/QUOTE]

You can use the toFixed() method (click this link).
Copy linkTweet thisAlerts:
@HazeleyesauthorJan 25.2009 — Thanks MrNobody for the candid help.
×

Success!

Help @Hazeleyes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...