For...in over an object?

For...in over an object?
0

#1

I need to also print “name” and “age”, I just can’t figure out how?

function yourCode(console) {
    var str = "";
    var obj = {
    name: 'matt',
    age: 58
    
    }
    for (var i in obj){
    str = str + obj[i];
  }
console.log(str);
}
yourCode(console)

#2

Technically it is displaying the name and age to the console, but it is merging them together as “matt58”. How are you wanting them to display in the console?


#3

Yes, I get this…

AssertionError: expected [ ‘matt58’ ] to deeply equal [ ‘name’, ‘matt’, ‘age’, 58 ]
+ expected - actual

EDIT: Essentially I need both name and age in there, too.


#4

Are you working on a specific challenge or project? If so, please post the url to it. Also, please post all of your code related to it also.


#5

I’m not. This is a challenge from a website that needs an invitation. The instructions are “Use a for…in loop to log each property and value in the object”


#6

Does it give you a full example of what the expected output should be for a specific call to the function?

Would I be correct if I assumed you are supposed to return an array containing each object property name / value pair with each pair separated into single elements?

If the answer to the above question is Yes, then why are you creating a string and returning it?

We need more details about what exactly is expected from this function. Is it just supposed to use console.log statement or is it actually supposed to return a value?


#7

Yes…i need name : Matt age : 58


#8

I edited my previous post. Can you answer all the questions I posed?


#9

How do I get an invitation so I can read the challenge content?


#10

You can’t. I need to console.log an array that shows the property and values.


#11

OK, now we are getting somewhere. If you are supposed to display an array, then you need to be creating an array instead of a string. Do you know how to declare an array and add elements to it?


#12

I have this currently…

function studentCode(console) {
    var arr = [];
    var obj = {
    name: 'matt',
    age: 58
    
    }
    for (var i in obj){
    arr = arr + obj[i];
  }
console.log(arr);
}
studentCode(console)

Still VERY confused why they have “console” as an argument for this function. Worst word to use.


#13

That is not how you add elements to an array. I have seen some of your projects you have worked on in the past, so you should know how to do this correctly.

You can name your function parameters anything you want. You can rename console (inside the function definition) to easterBunny for that matter as long long as you refer to easterBunny where needed in your function. You are not even referencing console in your function. I assume the tests are calling your function with an object being passed to function. If so, console represents that function. You have hard coded an object and assigned it to a variable named obj. That means no matter what the test cases use to call your function your function will always return the exact same answer. That will be a problem if that is the case.


#14

Yeah, sorry. I never worked with for…in loops and somehow never went over these.


#15

Take a look at the documentation for for in.


#16

Yeah, this is what I looked at for reference and why I started with a string originally, just trying to work off of that. It’s obvious to me I need an array, but still not understanding how to use .push to add it to the array or console it? Ugh, these moments make my head explode.


#17

If I gave you the following code, how would you add a new element to the array named “arr”? Specifically, how would you add the string 'Goodbye" to arr?

var arr = ['Hello'];
// code to add 'Goodbye' to arr goes here

#18

Quite honestly, I’m not so sure. Either I’m tired or stupid. I have this, but maybe now I’m considering that I need an object instead?

function studentCode(console) {
    var arr = [];
    var obj = {
    name: 'matt',
    age: 58
    
    }
    for (var i in obj){
    arr.push(obj[i]);
  }
console.log(arr);
}
studentCode(console)

EDIT: I would assume arr.push("Goodbye")


#19

OK, now you are pushing each object property’s value into arr. That is a good start. The above code creates an array which looks like [ ‘matt’, 58 ].

So you will notice the variable i in your for loop represents a property name of the object during each iteration of the for loop. So why not try pushing i to arr before pushing the property’s value?


#20

Sorry, I don’t understand what you mean when you say “your for loop represents the property name of the object.” Do you mean the property value? I tried arr.push(i) but only got the words name and age.