Jump to content

Recommended Posts

Okay guys and gals

I'm not "cheating" on this one because it's end of semester and I already turned in my code.

I'm going to fail the PHP class because I'm just not a programmer mind. I'm a creative mind.

I failed my SQL course in the tech school I attend because I couldn't get past outer joins.

It's like my mind hits a mental brick wall and can't burst through it.

 

BUT

 

this @%!# thing has given me so much trouble I want to see someone write it out for me to see where I went wrong.

Or rather, why I failed it.

Or rather, a reason why I shouldn't toss my computer off of a bridge from a moving vehicle.

 

Here's the assignment below. Yea yea it's basic stuff but like I said I AM NOT A PROGRAMMER. Can't DO this.

Didn't help that the PHP I course I took -- this assignment I paste is PHP II -- was over the summer semester and thus three weeks.

 

Made a promise to myself: no more summer courses.

 

=====

 

 

For this assignment, please write a PHP script that does the following:

1. Pre-populate an array(s) with 20 types of sushi and their associated prices.

2. Use a foreach() loop to parse the array(s) and:

Find the highest priced sushi

Find the lowest price sushi

Find the average sushi cost

3. Use a foreach() loop to parse the array(s) and:

If the sushi's price is above the average price echo/print out the sushi's name,its price and the string "expensive"

If the sushi's price is above the average price, echo/print out the sushi's name, its price and the string "cheap"

4. Write out a message displaying the most expensive and the least expensive sushi

Link to comment
https://forums.phpfreaks.com/topic/272080-how-do-you-do-this/
Share on other sites

Something like this. I modified to not use unnecessary loops.

 

$sushi = array('Unagi' => 4.59, 'Uni' => 6.99, 'Saba' => 5.99, 'Tamago' => 4.25, 'Toro' => 8.99, 'Masago' => 3.59);

$avg = round( array_sum($sushi) / count($sushi), 2 ); // no need for a loop for this
$min = min($sushi); // again no loop needed
$max = max($sushi); // and again . . .

echo "The most expensive sushi is $max<br>\n";
echo "The least expensive sushi is $min<br>\n";
echo "The average sushi is $avg<br>\n";

foreach( $sushi as $k => $v ) {
if( $v > $avg ) {
	echo "$k is expensive<br>\n";
} elseif( $v < $avg ) {
	echo "$k is cheap<br>\n";
} else {
	echo "$k is average<br>\n";
}
}

Link to comment
https://forums.phpfreaks.com/topic/272080-how-do-you-do-this/#findComment-1399770
Share on other sites

What is the 2 in there for? After the count?

i think I can understand everything else......I actually had the min and max functions in the code I turned in.

 

I'm assuming your "$k" is substitute for 'key' and your $v is substitute for 'value'.

But why round when creating/defining the variable 'avg'?

Link to comment
https://forums.phpfreaks.com/topic/272080-how-do-you-do-this/#findComment-1399772
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.