-1

I’m having a lot of trouble learning how to use for loops to fill a new variable. As an example say if I have var year = [2010, 2000, 1992]; and var age = [];.

How would I use a for loop to fill in the age variable?

If this is a bad example, don’t use this. I just would like some help with understanding how to fill in empty arrays.

var names = ["Ace", "yoshi", "Lassie"];
var age = [25, 23, 5];
var u24 = [];
for (var i = 0; i < names.length; i++) {
 if ([age] < 24) {
 u24 += age[i]
 console.log("hello " + names + " " + "you are" + age);
 }
}

Sebastian Simon
19.8k8 gold badges61 silver badges88 bronze badges
asked Dec 3, 2017 at 18:21
6
  • Please check this code and try yourself, You can use forEach functions and inside loop function you just subtract year with current year and store. w3schools.com/jsref/jsref_forEach.asp Commented Dec 3, 2017 at 18:30
  • Do you expect [age] to be a single number to be compared with 24? Are you familiar with the Array filter and push functions? Commented Dec 3, 2017 at 18:31
  • if i understand correctly then instead of u24 += age[i]you can use u24[] = age[i] or u24.push(age[i]) Commented Dec 3, 2017 at 18:32
  • @keja No, you cannot use u24[] = age[i]. Commented Dec 3, 2017 at 18:33
  • @Xufox you are right, dont know what i was thinking :D prob not thinking.. Commented Dec 3, 2017 at 18:49

3 Answers 3

2

If all you want to do is fill in the age array with a loop, you can try this:

let years = [2010, 2000, 1992],
 age = [],
 d = new Date().getFullYear();
years.forEach(year => age.push(d - year));
console.log(age);

As regards the relationship between age and names, I think Intervalia has explained that.

answered Dec 4, 2017 at 8:13
Sign up to request clarification or add additional context in comments.

Comments

1

It is better to create objects that contain relevant data. Combining the name and age into a person object would help.

var persons = [
 {
 name: "Ace",
 age: 25
 },
 {
 name: "yoshi",
 age: 23
 },
 {
 name: "Lassie",
 age: 5
 }
];
var u24=[];
for (var i =0; i < persons.length; i++) {
 var person = persons[i];
	if(person.age < 24){
		u24.push(person.age);
		console.log("hello " + person.name + " " + "you are " + person.age);
	}
}
console.log(u24);

But you can also use forEach like this:

var persons = [
 {
 name: "Ace",
 age: 25
 },
 {
 name: "yoshi",
 age: 23
 },
 {
 name: "Lassie",
 age: 5
 }
];
var u24=[];
persons.forEach(
 function(person) {
 if(person.age < 24){
 u24.push(person.age);
 console.log("hello " + person.name + " " + "you are " + person.age);
 }
 }
);
console.log(u24);

By making objects that include all relevant data your loops will never get out of sync. If you remove a person from the persons array then their name and age will go together.

UPDATE: Using a filter

var persons = [
 {
 name: "Ace",
 age: 25
 },
 {
 name: "yoshi",
 age: 23
 },
 {
 name: "Lassie",
 age: 5
 }
];
var youngPersons = persons.filter(
 function(person) {
 return (person.age < 24);
 }
);
console.log(youngPersons);

Or using an ES6 Arrow Function

 var persons = [
 { name: "Ace", age: 25 },
 { name: "yoshi", age: 23 },
 { name: "Lassie", age: 5 }
 ];
 var youngPersons = persons.filter((person) => person.age < 24);
 console.log(youngPersons);

This provides back an array of the persons that match your Age under 24 criteria.

answered Dec 3, 2017 at 18:32

9 Comments

Just use filter instead of forEach.
Yes, filter can work if you are just trying to get a filtered list. Since @Ace didn't post what his intent was I may have assumed a different intent.
Thank you for your help. Quick question and it may be a dumb question but what is another way of putting the new information in to u24 beside using push. As in would it be u24 =person.age?
You set up u24 as an array. So I assumed you wanted an array of ages.
@intervalia I do
|
0

A working version. Please compare it with your code to see the differences. Arrays always got me when I was starting out, and the syntax with different across languages, despite the reuse of bracket symbols.. AutoIt language still trips me up :P

var names = ["Ace", "yoshi", "Lassie"];
var age = [25, 23, 5];
//Use array.push() to append values
var u24 = [];
//variable i counts up to names.length
//because i++ means 'add one' to i
for (var i = 0; i < names.length; i++) {
 //if ([age] < 24) {u24 += age[i];
 //age at the count 'i' (which is
 //counting)
 //is achieved by array[at_index]
 if (age[i] < 24) {
 u24.push(age[i]); //add it to the end
 console.log("Hello " + names[i] +
 ", you are " + age[i]);
 }
}
answered Dec 3, 2017 at 18:55

3 Comments

This is very helpful as well thank you very much. I'm still in my first month of learn javascript so every bit of info helps.
No problemo bro, we've all been there! There isn't anything wrong with asking questions, so don't let the grump down votes get you :P
I know it's just hard enough having self teaching, then you come here and have people make you feel slow for asking a question.The html and css was easy but this stuff is a bit more difficult

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.