...

# output two values of an array

confusedinjava
06-20-2010, 11:28 PM
Hi
How can i output two maximum numbers of an array if they are equal?

var flower["roses", "violets", "buttercups", "daisies"];
var flowerAmounts[9, 8, 3, 9];

Output should be:
The maximum amount of flowers is 9.
There are 9 roses and 9 daisies.

Hope someone can help, thanks :confused:

jmrker
06-21-2010, 02:28 AM
Can you modify this to your needs?

<html>
<title>Max of 2 arrays</title>
<script type="text/javascript">

var flower = ["roses", "violets", "buttercups", "daisies"];
var flowerAmounts = [9, 8, 3, 9];
var arr = [];
for (var i=0; i<flower.length; i++) {
arr.push(flowerAmounts[i]+','+flower[i]);
}
arr.sort().reverse();
var maxValue = 0;
var tmp = [];
for (var i=0; i<arr.length; i++) {
tmp = arr[i].split(',');
if (tmp[0] > maxValue) { maxValue = tmp[0]; }
}
var str = 'The maximum amount of flowers is '+maxValue+'\n';
for (var i=0; i<arr.length; i++) {
tmp = arr[i].split(',');
if (tmp[0] == maxValue) { str += arr[i]+'\n'; }
}
</script>
<body>

</body>
</html>

Sort of convoluted and not the way I would approach it, but it works...

Good Luck!
:)

Philip M
06-21-2010, 09:15 AM
Another approach:-

<script type = "text/javascript">

function sortBy(prop){return function(a,b){return a[prop]>b[prop] ? 1 : a[prop]<b[prop] ? -1 : 0; }}

var myArray = [{"name":"Roses","qty":9},{"name":"Violets","qty":8}, {"name":"Buttercups","qty":"3"}, {"name":"Daisies","qty":"9"}];
var len = myArray.length;
document.write("Original Data <br>");
for (var i = 0; i<=len-1; i++) {
document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
}
document.write ("<br>");
document.write("Sorted By Name <br>");
myArray.sort(sortBy("name"));
for (var i = 0; i<=len-1; i++) {
document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
}
document.write ("<br>");
document.write("Sorted By Quantity <br>")
myArray.sort(sortBy("qty"));
for (var i = 0; i<=len-1; i++) {
document.write (myArray[i].name + " " + myArray[i].qty + "<br>");
}
document.write ("<br>");

var x = myArray[len-1].qty;
document.write("The flower(s) with the highest numbers are:-" + "<br>")
for (var i =0; i<=len-1; i++) {
if (myArray[i].qty == x) {
document.write(myArray[i].name + " " + myArray[i].qty + "<br>");
}
}
</script>

"I used to think I was poor. Then they told me I wasn't poor, I was needy. Then they told me it was self-defeating to think of myself as needy. I was deprived. (Oh well, not deprived but rather underprivileged.) Then they told me that underprivileged was overused. I was disadvantaged. I still don't have a dime. But, I have a great vocabulary." - Jules Feiffer, cartoonist.

confusedinjava
06-21-2010, 11:14 AM
Thanks to both of you! :thumbsup:

Is there any way of achieving the same result without using functions? i.e just using an if loop? Reason being we haven't started with functions yet, i don't want to appear to know more than i do else they may expect to much from me in the future...

Here's what i have so far... If it's right or not is a different matter all together:o

var flower["roses", "violets", "buttercups", "daisies"];
var flowerAmounts[9, 8, 3, 9];
var max1;
va max2;

max1 = flowerAmounts[0];

for (var i = 0; i < flowerAmounts.length;i = i++)
{
if (flowerAmounts[1] > max1)
{
max1 = flowerAmount[i];
}
}
document.write('The maximum amount of flowers is ' + max + '<BR>');
document.write('There are ' + max1[flower]+ 'and ' + max2[flower] + '<BR>');

Philip M
06-21-2010, 11:44 AM
More homework! :( Your teachers will always have a rose-tinted view of your abilities if we keep doing your homework for you. Remember that you cannot expect to go through life asking others to do your work for you.

<script type = "text/javascript">
var flower = ["roses", "violets", "buttercups", "daisies"];
var flowerAmounts = [9, 8, 3, 9];
var largest = [];
var len = flower.length;
var max = -99;
var count = 0;

for (var i =0; i< len; i++){
if (flowerAmounts[i] > max) {
max = flowerAmounts[i];
}
}

for (var i = 0; i< len; i++){
if (flowerAmounts[i] == max) {
largest[i] = flower[i];
count++;
}
}

largest.sort();

document.write("The largest value(s):- <br>");
for (var i = 0; i<=count-1; i++) {
document.write(max + " " + largest[i] + "<br>")
}

</script>

confusedinjava
06-21-2010, 12:07 PM
jeje - my point exactly! :eek:

The first two parts of the code make sense, i still have to somehow get around using

largest.sort();

Have just tried (off the top of my not very good head) to switch largest.sort() to a for loop the second value turns to undefined and the script keeps looping...
:(
Have also tried to find a code breakdown of largest.sort() case it gave me any ideas but no luck.

Must admit, i enjoy javascript but arrays have thrown me into confusion, how long does it take for this to make any sense?

Thanks again

Philip M
06-21-2010, 01:00 PM
The first two parts of the code make sense, i still have to somehow get around using
largest.sort();

The sort was not really necessary.

document.write("The largest value(s):- <br>");
for (var i = 0; i< len; i++) {
if (flowerAmounts[i] == max) {
document.write(max + " " + largest[i] + "<br>")
}
}

confusedinjava
06-21-2010, 01:22 PM
Thanks :thumbsup:

As way of explaining myself, i had spent 3 days adapting all the exercises i have been given to see if any would work, none did. I also re-read the info on Arrays, and still no joy... I used the forum as a last resort because at this point i wanted to understand it more than i wanted to complete the exercise.

Once again, i really do appreciate your help