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!

8
  • You are pushing the index to array and not object at i index in array. What is "superfluity"? Commented Apr 26, 2017 at 1:16
  • Push the item instead of the index ageDiscrimArray.push(objectArray[i]);. The same goes for the other array. Commented Apr 26, 2017 at 1:17
  • 1
    @guest271314— superfluity, cf superfluous. Commented Apr 26, 2017 at 1:22
  • Your tutor might have expected you to use Array.prototype.filter. Oh, the syntax of your array literal is incorrect. Commented Apr 26, 2017 at 1: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 or const if these are newly created variable names. Commented Apr 26, 2017 at 1:31

4 Answers 4

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]
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0

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

Comments

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!

1 Comment

Thanks! I appreciate your comprehensive breakdown. As a beginner, I'm helped by thorough explanations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.