2

I'm a noob learning javascript. My tutor gave me this homework.

HOMEWORK: Write a function which takes an array of objects and a number, maxAge. Each object will have two properties: name, age. Return a new array of objects, only containing objects whose age is less or equal to maxAge.

Here's what I did:

const objectArray = [

firstObject = {
    name: "Ryan",
    age: 32
},

secondObject = {
  name: "Caroline",
  age: 1
},

thirdObject = {
  name: "Steve",
  age: 35
},

fourthObject = {
  name: "Sheila",
  age: 67
},

fifthObject = {
  name: "Ron",
  age: 67
},

sixthObject = {
  name: "deadGuy",
  age: 150
},

];



const maxAge = 67;


const makeAgeDiscrimArray = (objectArray) => {
  const ageDiscrimArray = [];
  const above67Array = [];
  const length = objectArray.length;
  for (let i = 0; i < objectArray.length; i++) {
    if ((objectArray[i].age <= maxAge)) {
      ageDiscrimArray.push(i)} else {
      above67Array.push(i); // I know, it is a superfluity
      }
    }
    return ageDiscrimArray;
};

console.log(makeAgeDiscrimArray(objectArray));

The function currently returns

[ 0, 1, 2, 3, 4 ]

I see what is happening, but I don't fully understand why.

Thanks in advance for your help!

elderlyman
  • 500
  • 8
  • 24
  • You are pushing the index to array and not object at `i` index in array. What is "superfluity"? – guest271314 Apr 26 '17 at 01:16
  • Push the item instead of the index `ageDiscrimArray.push(objectArray[i]);`. The same goes for the other array. – ibrahim mahrir Apr 26 '17 at 01:17
  • 1
    @guest271314— [*superfluity*](https://www.google.com.au/search?q=define+superfluity&ie=utf-8&oe=utf-8&safe=active&gws_rd=cr&ei=Qvb_WPyqKsK20QSMlZyYDw), *cf* superfluous. – RobG Apr 26 '17 at 01:22
  • Your tutor might have expected you to use [*Array.prototype.filter*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). Oh, the syntax of your array literal is incorrect. – RobG Apr 26 '17 at 01:24
  • Attaching the objects to variable names (`firstObject` etc.) within the array is a bit of an unusual thing to do - are you sure that's what you want? Generally [you should use `var`](http://stackoverflow.com/questions/1470488/what-is-the-purpose-of-the-var-keyword-and-when-to-use-it-or-omit-it) or `const` if these are newly created variable names. – Stuart Apr 26 '17 at 01:31
  • @guest271314 - thanks. – elderlyman Apr 30 '17 at 01:09
  • @ibrahimmahrir - thanks for a concise response using code. – elderlyman Apr 30 '17 at 01:10
  • @RobG - I do not yet have Array.prototype.filter in my toolkit yet, but I'll look into it thanks to your tip. – elderlyman Apr 30 '17 at 01:12

4 Answers4

5

You are adding "i" to the array instead of the object. Change to this:

for (let i = 0; i < objectArray.length; i++) {
  if ((objectArray[i].age <= maxAge)) {
      ageDiscrimArray.push(objectArray[i])} // <= objectArray[i]
  else {
      above67Array.push(objectArray[i]); // <= objectArray[i]
  }
}
luly
  • 614
  • 3
  • 9
0

Essentially the function makeAgeDiscrimArray separates the array into two distinct arrays. It does this using the max age.

You need to be sure to add the array object at position i as opposed to the actual position i to the array.

Also you could look into filter and map which are functions built into JS and are useful for performing operations on arrays.

henry
  • 129
  • 8
0

You are adding the index of the relevant object in objectArray to the result arrays rather than the object itself

randbw
  • 490
  • 5
  • 13
0

I'm not sure if you're asking someone to explain how it all works, but I'll see if I can break it down.

In other languages this would be called a lambda or anonymous function. In this function you are passing in objectArray. That's called an argument in javascript and allows you to get data into your function.

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)

You made the parameter in the function the same name as your object, but you could name it something different altogether.

const makeAgeDiscrimArray = (objectArray) => {
}

Constants can't be changed, but if they are assigned an array in Javascript you can push new items to the array.

If you try to assign a new array to those variables though it will throw an error. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const)

const ageDiscrimArray = [];
const above67Array = [];

Here you are assigning the # of items in objectArray to a constant.

const length = objectArray.length;

Let's make this part a little easier to read.

for (let i = 0; i < objectArray.length; i++) {
    if (objectArray[i].age <= maxAge) {
      ageDiscrimArray.push(i)
    } 
    else {
      above67Array.push(i); // I know, it is a superfluity
    }
  }
return ageDiscrimArray;
};

For loop. Creating a new variable that counts up by 1(i++) until you reach the number of items in objectArray.

For each object in the array it first checks if the age is less than or equal to your maxAge. It then pushes that array object to your const array, ageDiscrimArray.

If it doesn't match your if statement it continues on to else and pushes that array item to above67Array.

You then return ageDiscrimArray to the function.

This is where you are actually calling your anonymous function.

console.log(makeAgeDiscrimArray(objectArray));

In this last piece of code you are telling it to print your variable, makeAgeDiscrimArray, which happens to be an anonymous function. It goes through everything I explained before and returns whatever you told the function to return(your array). So the important thing I guess is that it's not returning your actual object, it's returning the object number in your first array which is an important difference.

Anyway, hope this helps!

PeterG
  • 125
  • 2
  • 8