Javascript functions

What am I doing wrong? I’m trying to display the predefined array. Then a function to display the highest number in the array and it’s index. The last thing is create a function that displays each number in the array if it’s odd or even. Thank You in advance.

var arr = [56, 77, 23, 12, 88, 59, 97, 33, 38, 64];
var string = newarr[10];
string[0] = "56";
string[1] = "77";
string[2] = "23";
string[3] = "12";
string[4] = "88";
string[5] = "59";
string[6] = "97";
string[7] = "33";
string[8] = "38";
string[9] = "64";
var max;
var maxIndex;
var newarr;

function findMax(arr) {

var max = arr[0];

for (var i = 0; i < arr.length; i++) {
    if (max < arr[i] ) {
        max = arr[i];
      maxIndex = i;
      
    }
}
}
function evenOrOdd(string) {
  
  if (string % 2 == 0) {
    console.log(string + "is even");
  } else {
    console.log(string + "is odd");
  }
}

console.log (arr);
console.log("The largest number in the array is " + max + " located at array index " + maxIndex);
console.log("The numbers were:");

what’s this? where does newarr come from? I think you have not given all your code

newarr is a placeholder for 10 arrays.

unless I should use “arr”

what 10 arrays? you have not defined newarr before that point
I can’t test your code, I get an error that newarr is not defined

var arr = [56, 77, 23, 12, 88, 59, 97, 33, 38, 64];
var newString;
stringArray = newString[10];
stringArray[0] = "56";
stringArray[1] = "77";
stringArray[2] = "23";
stringArray[3] = "12";
stringArray[4] = "88";
stringArray[5] = "59";
stringArray[6] = "97";
stringArray[7] = "33";
stringArray[8] = "38";
stringArray[9] = "64";
var max;
var maxIndex;

function findMax(arr) {

var max = arr[0];

for (var i = 0; i < arr.length; i++) {
    if (max < arr[i] ) {
        max = arr[i];
      maxIndex = i;
      
    }
}
}
function evenOrOdd(string) {
  
  if (string % 2 == 0) {
    console.log(string + "is even");
  } else {
    console.log(string + "is odd");
  }
}

console.log (arr);
console.log("The largest number in the array is " + max + " located at array index " + maxIndex);
console.log("The numbers were:");

TypeError: Cannot read property '10' of undefined

Don’t you get the error when you run your code?

I do. trying to figure out what it’s from.

Creating arrays

Another helpful page.

1 Like

Thank you MyTrueName

You have never given a value to newString, its value is undefined, and undefined is not something you can use bracket notation on. You need to define your values before using them

Thank you ieahleen for the feedback. Much appreciated!

No errors, but getting undefined on console.log

var arr = [56, 77, 23, 12, 88, 59, 97, 33, 38, 64];
var max, maxIndex;
function findMax() {

var max = arr[0];

for (var i = 0; i < arr.length; i++) {
    if (max < arr[i] ) {
        max = arr[i];
      maxIndex = i;
      
    }
}
}
console.log (arr);
console.log("The largest number in the array is " + max + " located at array index " + maxIndex);

Scope in JS

1 Like

Thank You bbsmooth. The attached help a lot.

I should have looked a little more carefully at your code. Yes, there is a potential scope issue, but also, you aren’t calling findMax, and the function doesn’t return a value, which is sort of OK if you are going to work on the global variables, but I recommend you pass arr into the function and then return the values you want.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.