/    Sign up×
Community /Pin to ProfileBookmark

Help with understanding what to do…

Okay I’m not wanting anyone to do my HW so please don’t lecture me. I just don’t even understand what the hell I am supposed to do. Maybe someone can break it down for me? LOL

Question states:
A mail order house sells five different products whose retail prices are: product 1- $2.98, product 2 $4.50, product 3- $9.98, product 4- $4.49, and product 5- $6.87. Write a script that reads a series of pairs of numbers as follows:

a) Product Number
b) Quantity sold for one day

Your program should use a switch structure to help determine the retail price for each product. Your program should calculate and output HTML that displays the total retail value of all products sold last week. Use a prompt dialog to obtain the product number from the user. Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.

to post a comment
JavaScript

20 Comments(s)

Copy linkTweet thisAlerts:
@KravvitzSep 19.2006 — Well, have you learned about Object(), switch(), prompt(), and while()?

Have you learned about the DOM or just document.write()?

Learning how to think like a programmer is tricky. I remember trying to do some assignments that seemed impossible at first during my first programming class.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 19.2006 — we've learned all of that except DOM..

yeah I'm hoping it gets better... so far I don't like javascript.. eck
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 19.2006 — oh we havent learned object either.
Copy linkTweet thisAlerts:
@KravvitzSep 19.2006 — Have you learned about [url=http://www.devguru.com/technologies/ecmascript/QuickRef/array.html]Array()[/url] then?

I love JavaScript but dealing with cross-browser compatibility issues can be annoying.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 19.2006 — no array yet but I keep seeing it when I ask people to help me. lol
Copy linkTweet thisAlerts:
@KravvitzSep 19.2006 — Oh! Silly me. Since it tells you to use a switch structure, you don't need an array or object. The label associated with each case statement is a product ID and then do perform some action, e.g. set a variable, in the code block for each case.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 19.2006 — Oh! Silly me. Since it tells you to use a switch structure, you don't need an array or object. The label associated with each case statement is a product ID and then do perform some action, e.g. set a variable, in the code block for each case.[/QUOTE]

do you have AIM or Yahoo? I really need like one on one help with this.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — I feel so lost.. please help me SOMEONE...


[code=php]<HTML>

<!-- Sierra Bass -->
<!-- Section 01 -->
<!-- Due 9.20.06 -->

<HEAD>
<TITLE>Exercise 15.12</TITLE>

<SCRIPT LANGUAGE = "JavaScript">
var product, // Product number entered by user
quantity, // Number of items sold entered by user
price, // Price of each product
totalprice, // The total for all products sold
validInput = true; // Indicates if input is valid

//User inputs product number and quantity sold
product = window.prompt( "Enter product number:", "1" );
quantity = window.prompt( "Enter number of items sold:", "1" );

//Switch multiple-selection structure
switch ( product ) {
case "1":
price: "2.98"
break;
case "2":
price: "4.50"
break;
case "3":
price: "9.98"
break;
case "4":
price: "4.49"
break;
case "5":
price: "6.87"
break;
default:
validInput = false;
}

// Calculate products sold
totalprice = product*quantity

// To show
if ( validInput == true ) {
document.write("The total retail value of all
product " + product + "'s sold last week is: $" + totalprice);
}
else
document.writeln( "Invalid choice: " + product );

</SCRIPT>

</HEAD>
<BODY>
<P>Click Refresh (or Reload) to run the script again</P>
</BODY>
</HTML>
[/code]
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — [code=php]<HTML>

<!-- Sierra Bass -->
<!-- Section 01 -->
<!-- Due 9.20.06 -->

<HEAD>
<TITLE>Exercise 15.12</TITLE>

<SCRIPT LANGUAGE = "JavaScript">
var product, // Product number entered by user
quantity, // Number of items sold entered by user
price, // Price of each product
totalprice, // The total for all products sold
totalCounter, // Total for the week
validInput = true; // Indicates if input is valid

// Initialization phase
total = 0; // clear total
totalCounter = 0; // prepare to loop

//User inputs product number and quantity sold
product = window.prompt( "Enter product number, -1 to quit:", "1" );
quantity = window.prompt( "Enter number of items sold:", "1" );


//Switch multiple-selection structure
switch ( product ) {
case "1":
price: "2.98"
break;
case "2":
price: "4.50"
break;
case "3":
price: "9.98"
break;
case "4":
price: "4.49"
break;
case "5":
price: "6.87"
break;
default:
validInput = false;
}

// Calculate products sold
while (product != -1){
for (var t = 1; t<=5; t++);
}


// Show results:
if ( validInput == true ) {
document.write("The total retail value of all
product " + product + "'s sold last week is: $" + totalprice);
}
else
document.writeln( "Invalid choice: " + product );

</SCRIPT>

</HEAD>
<BODY>
<P>Click Refresh (or Reload) to run the script again</P>
</BODY>
</HTML>
[/code]
Copy linkTweet thisAlerts:
@KravvitzSep 20.2006 — I have AIM, Yahoo, MSN, and ICQ accounts, but I only tell my paying clients and other programmers who are close to my level of proficiency.

What browser are you testing in? IE6? I recommend you use Firefox 1.5 and its JavaScript Console.

What's the for loop for?
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — we have to have a loop so they can enter each product number in and how many of each product
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — the prof. tells us to test in IE
Copy linkTweet thisAlerts:
@KravvitzSep 20.2006 — we have to have a loop so they can enter each product number in and how many of each product[/QUOTE]
Yes, that's why you need the while loop. That doesn't explain the for loop.

Also, the loop isn't very useful if the prompt() calls aren't in it.

the prof. tells us to test in IE[/QUOTE]
I recommend you get it working in Firefox and then make sure it works right in IE. [url=http://www.mozilla.com/firefox/]Firefox[/url] is a much better development platform than IE6.

P.S. I get email notifications of responses to this this thread, so you don't need to PM me when you respond.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — I think I meant to put an if statement there not while. woops.

Sorry about PMing you. Wasn't sure if you had it email you.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — I know what I have to do in my head but it's hard trying to write it down now.

I need to prompt the user for each product and it's quantity. Then they will type -1 when they are done entering the information. Then it will show HTML of the total retail value of all products sold the last week.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — [code=php]<HTML>

<!-- Sierra Bass -->
<!-- Section 01 -->
<!-- Due 9.20.06 -->

<HEAD>
<TITLE>Exercise 15.12</TITLE>

<SCRIPT LANGUAGE = "JavaScript">
var product, // Product number entered by user
quantity, // Number of items sold entered by user
price, // Price of each product
totalprice, // The total for all products sold
totalCounter, // Total for the week
validInput = true; // Indicates if input is valid

// Initialization phase
total = 0; // clear total
totalCounter = 0; // prepare to loop

//User inputs product number and quantity sold
product = window.prompt( "Enter product number, -1 to quit: ", "1" );
quantity = window.prompt( "Enter number of items sold:", "1" );


//Switch multiple-selection structure
switch ( product ) {
case "1":
price = "2.98"
break;
case "2":
price = "4.50"
break;
case "3":
price = "9.98"
break;
case "4":
price = "4.49"
break;
case "5":
price = "6.87"
break;
default:
validInput = false;
}

// Calculate products sold
if (product != -1){
totalprice = product*quantity;
}


// Show results:
if ( validInput == true ) {
document.write("The total retail value of all product's sold last week is: $" + totalprice);
}
else
document.writeln( "Invalid choice: " + product );

</SCRIPT>

</HEAD>
<BODY>
<P>Click Refresh (or Reload) to run the script again</P>
</BODY>
</HTML>
[/code]
Copy linkTweet thisAlerts:
@Arty_EffemSep 20.2006 — [code=php]<HTML>

<!-- Sierra Bass -->
<!-- Section 01 -->
<!-- Due 9.20.06 -->

<HEAD>
<TITLE>Exercise 15.12</TITLE>

<SCRIPT LANGUAGE = "JavaScript">
var product, // Product number entered by user
quantity, // Number of items sold entered by user
price, // Price of each product
totalprice, // The total for all products sold
totalCounter, // Total for the week
validInput = true; // Indicates if input is valid

// Initialization phase
total = 0; // clear total
totalCounter = 0; // prepare to loop

//User inputs product number and quantity sold
product = window.prompt( "Enter product number, -1 to quit: ", "1" );
quantity = window.prompt( "Enter number of items sold:", "1" );


//Switch multiple-selection structure
switch ( product ) {
case "1":
price = "2.98"
break;
case "2":
price = "4.50"
break;
case "3":
price = "9.98"
break;
case "4":
price = "4.49"
break;
case "5":
price = "6.87"
break;
default:
validInput = false;
}

// Calculate products sold
if (product != -1){
totalprice = product*quantity;
}


// Show results:
if ( validInput == true ) {
document.write("The total retail value of all product's sold last week is: $" + totalprice);
}
else
document.writeln( "Invalid choice: " + product );

</SCRIPT>

</HEAD>
<BODY>
<P>Click Refresh (or Reload) to run the script again</P>
</BODY>
</HTML>
[/code]
[/QUOTE]

Your code seems to be intending to show results using document.write, which is incorrect as it deletes the current content of the document.

Your easiest option is to write to the .innerHTML property of a selected div.

<div id='results'></div>

document.getElementById('results').innerHTML=myResultString;

I see you've changed your switch-case statement so that it actually assigns something, but you still need the prompts and switch statements in a loop, I would recommend a do-while loop.

[CODE] totalprice = product*quantity;[/CODE]This will just multiply the item code by the quantity

You need to store the totals and quantities for each item separately before adding them.

You also need a way of recording the entry of a quantity for each item, so you know when they are all entered.

Keep at it, it can be [URL=http://www.hotspot.freeserve.co.uk/sales/sales.htm]done[/URL].
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — I can't get it to stop prompting now.. haha


[code=php]<HTML>

<!-- Sierra Bass -->
<!-- Section 01 -->
<!-- Due 9.20.06 -->

<HEAD>
<TITLE>Exercise 15.12</TITLE>

<SCRIPT LANGUAGE = "JavaScript">
var product, // Product number entered by user
quantity, // Number of items sold entered by user
price, // Price of each product
total, // Sum of all products sold
totalCounter, // Total for the week
validInput = true; // Indicates if input is valid

// Initialization phase
total = 0; // clear total
totalCounter = 0; // prepare to loop


//User inputs product number and quantity sold
product = window.prompt( "Enter product number, or -1 to quit: ", "1" );
quantity = parseInt (window.prompt( "Enter number of items sold:", "1" ));

// Calculate products sold
while (product != -1){
total = product*quantity;
totalCounter =totalCounter + 1;
quantity = parseInt (window.prompt(
"Enter number of items sold:", "1" ));
}

//Switch multiple-selection structure
switch ( product ) {
case "1":
price = "2.98"
break;
case "2":
price = "4.50"
break;
case "3":
price = "9.98"
break;
case "4":
price = "4.49"
break;
case "5":
price = "6.87"
break;
default:
validInput = false;
}

// Show results:
if ( validInput == true ) {
document.write("The total retail value of all product's sold last week is: $" + total);
}
else
document.writeln( "Invalid choice: " + product );

</SCRIPT>

</HEAD>
<BODY>
<P>Click Refresh (or Reload) to run the script again</P>
</BODY>
</HTML>
[/code]
Copy linkTweet thisAlerts:
@Arty_EffemSep 20.2006 — I can't get it to stop prompting now.. haha


while (product != -1){
total = product*quantity;
totalCounter =totalCounter + 1;
quantity = parseInt (window.prompt(
"Enter number of items sold:", "1" ));
}
[/QUOTE]
That's because nothing within your loop will change the test condition. You don't need that section of code inside a loop.
Copy linkTweet thisAlerts:
@lilredtheteaseauthorSep 20.2006 — well I can't figure out how to make it add the stuff together and loop
×

Success!

Help @lilredthetease 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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