Practicing array manipulations with for loops, will appreciate help with the salad I've made

The function superSize accepts numbers only, and arranges them from largest to smallest, the code looks Ok to me but it needs proper syntax, please help me. I’m also not sure why is the var i is undefined inside the if statement.

*** Function edited, now facing a different problem, if statement in line 4 always returns false, I can’t understand why, is it the counter i that doesn’t work right?

function superSize(num){    //function to sort numbers inside array 'num'
  if (num.isarray) {        //making sure it's an array
      for (var i = 0; i < num.length; i++) { //checking every index in the array
        if (Number.isInteger(num[i])) {      //  if contains only numbers, returns false despite containing numbers 
          for (var i = 0; i < num.length; i++) {
            console.log(num[i],num[i+1]);
            if (num[i] < num[i+1])
              { 
              num.push(num[i]);
              num.shift;
              }
            } 

         }
      else {
        return "Please enter only numbers";}
        }
    return num;}
else {return "Please enter an array of numbers";}
  }

A little breakdown of your code:

for (var i = 0; i < superSize.length; i++) {

superSize is a function that you’ve just created. It indeed has length property that holds a number of arguments this function expects (one argument num in your case), so loop will go through

if (Number.isInteger(superSize[i])) {    }

superSize[i] will be undefined (as it’s a function and not an Array), therefore this if statement will not fire and the code will move along to else statement that regardless of the input will always be returning "Please enter only numbers".

So the code isn’t OK, I’m afraid

If num is supposed to be an Array of integers and you want to “arrange them from largest to smallest”, then it’s called sorting and this might help you:

1 Like

I appreciate the explanation and the tip for better code.

Should I have used num[i] or num[0] to access the index in the array than? I think now I should have.