Miljan Puzović
Miljan Puzović

Reputation: 5820

Find greatest value in array (in set of integers)

My friend asked me to help him with homework, and I'm stuck. Here is assignment:

user must enter in first prompt box number of elements in array. Then, he will get prompt box for each number to enter. Now, output must be greatest number in array. But that simply doesn't work. With my code below, I always get the element who has greatest first digit. (it's doesn't matter if number is negative or positive, code doesn't work as it should)

Here is my code (it even doesn't work in jsfiddle, just in my file)

<button onclick="duzinaNiza()">Do it!</button>

and here is JavaScript

function duzinaNiza() {
    var brClanova = prompt("Enter the number of array elements:");

    if (brClanova > 0) {
        var niz = new Array();

        for (i=0; i<brClanova; i++) {
            var redniBr = i+1;
            niz[i] = prompt("Enter "+ redniBr +". array number:");
            \\ prompt for geting each array element
        }


        var maximum = niz[0];

        for (a=0; a<brClanova; a++) {

            if (maximum < niz[a]) {
                maximum = niz[a];
            }
        }

        document.write("Greatest value in array is: " + maximum);   
    }
}

My friend's proffesor doesn't want to use functions for sorting arrays, this must be done with loops.

P.S. Yeah, I know... But don't ask about document.write thing, it must be printed in that way...

Upvotes: 1

Views: 1978

Answers (7)

kennebec
kennebec

Reputation: 104760

You don't need parseInt- if you subtract strings that can be converted to numbers, they are converted. So you can subtract the maximum from the next number, and see if it leaves a remainder.

Also, parseInt will destroy decimals, so you won't know that 1.5 is greater than 1.

Your comment used the wrong characters- `('\' should be '//')

function duzinaNiza(){
    var brClanova= prompt("Enter the number of array elements:");
    if(brClanova>0){
        var niz= new Array();
        for(var i= 0;i<brClanova;i++){
            var redniBr= i+1;
            niz[i]= prompt("Enter "+ redniBr +". array number:");
            //prompt for geting each array element
        }
        var maximum= niz[0];
        for(var a= 0;a<brClanova;a++){
            if(niz[a]-maximum>0){
                maximum= niz[a];
            }
        }
        document.write("Greatest value in array is: " + maximum);
    }
}

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

try this out, [Tip: i just utilised the '+' operator for casting the value to number (values from prompt.). The '+' operator will return NaN, if the entered value could not get converted into a number. so in that situation, you should use isNan function to get rid of that.]

  duzinaNiza = function () {
    var brClanova = prompt("Enter the number of array elements:");

    if (brClanova > 0) {
        var niz = new Array();
        var maximum;

        for (i=0; i<brClanova; i++) {
            var temp = +prompt("Enter "+ i+1 +". number:");
            if(i===0) { maximum = temp }
            else { maximum = (temp > maximum)?temp:maximum; }

        }

        alert("Greatest value in array is: " + maximum);   
    }
}

Upvotes: 1

ZER0
ZER0

Reputation: 25322

So, the main problem here is that you're not threat your numbers as "number", but as string. The method prompt returns a string, so you need to convert them:

 function duzinaNiza() {
    var brClanova = +prompt("Enter the number of array elements:");

    if (!brClanova)
      return;

    var niz = [];

    for (var i=0; i < brClanova; i++) {
        var redniBr = i + 1;
        niz[i] = +prompt("Enter "+ redniBr + ". array number:");
    }

    var max = niz[0]; 

    for (var a = 1; a < brClanova; a++) {

      if (max < niz[a])
         max = niz[a];

    }

    document.write("Greatest value in array is: " + max);
}

I used the Unary Plus Operator for that.

Just for to know, in JS you can actually avoid the last loop using Math.max to get the maximum of an array of numbers. So instead of:

    var max = niz[0]; 

    for (var a = 1; a < brClanova; a++) {

      if (max < niz[a])
         max = niz[a];

    }

    document.write("Greatest value in array is: " + max);

You will have:

    var max = Math.max.apply(null, niz);

    document.write("Greatest value in array is: " + max);

In that case, you don't even need the unary plus operator because Math.max takes care of that.

Upvotes: 1

Marlon Bernardes
Marlon Bernardes

Reputation: 13853

The problem is that you are comparing two strings, when you wanted to compare two numbers. In other words, the following expression is LEGAL in javascript and evaluates to true:

if('4' > '393939393'){
    //true! string '4' is greater than string '3' (first char of '393939393')
}

What you should do is cast the value received from the function prompt, so it is treated as a number. You can do that using the following function:

parseInt(prompt("Enter "+ redniBr +". array number:"), 10);

The first parameter is the value you want to cast to a number, while the second is the radix (or "base") of the number.

Upvotes: 1

Niels
Niels

Reputation: 49919

That is because the input is a String, you have to parse it to a Integer. Like:

niz[i] = parseInt(prompt("Enter "+ redniBr +". array number:"), 10);

Upvotes: 1

Tom
Tom

Reputation: 4592

Try this:

function duzinaNiza() {
    var brClanova = prompt("Enter the number of array elements:");

    if (brClanova > 0) {
        var niz = new Array();

        for (i=0; i<brClanova; i++) {
            var redniBr = i+1;
            niz[i] = parseInt(prompt("Enter "+ redniBr +". array number:"));
            // prompt for geting each array element
        }


        var maximum = niz[0];

        for (a=0; a<brClanova; a++) {

            if (maximum < niz[a]) {
                maximum = niz[a];
            }
        }

        document.write("Greatest value in array is: " + maximum);   
    }
}

Upvotes: 1

Anoop
Anoop

Reputation: 23208

Modified Code JSFIDDLE

function duzinaNiza() {
    var brClanova = prompt("Enter the number of array elements:")*1; //convert string to intger

    if (brClanova > 0) {
        var niz = new Array();

        for (i=0; i<brClanova; i++) {
            var redniBr = i+1;
            niz[i] = prompt("Enter "+ redniBr +". array number:")*1;
            // prompt for geting each array element
        }


        var maximum = niz[0];

        for (a=0; a<brClanova; a++) {

            if (maximum < niz[a]) {
                maximum = niz[a];
            }
        }

        document.write("Greatest value in array is: " + maximum);   
    }
}

Upvotes: 0

Related Questions