0
function object(obj){
    var theArray =[];
    var i =0;
    for (var k in obj){

        theArray[i] = 0;
        i++;
    }
     return theArray;
 }


  var car = new object("hi");

 console.log(car);

its answer is:[0,0]. If i pass hello it returns me[0,0,0,0,0]. i am passing a string to a single parameter it should have to return a single answer why is it returning me a every character of string even function have only ONE parameter(obj) ? how is that loop working i mean i am not adding i instead using a k in object. explain it in simple way so i can understand it do not give me hints.

1
  • Why are you using new? Commented Jan 18, 2016 at 4:08

1 Answer 1

2

When you do this:

for (var k in obj)

when obj is a string, it iterates each of the indexes of the string. So you end up getting a k index for each letter of the string.

If you log the values of k in your for loop, you will see 0, 1, etc..., one for each letter in the string.

You can see here:

function object(obj){
    var theArray =[];
    var i =0;
    for (var k in obj){
        // capture the value of k so we can see what it is doing
        theArray[i] = k;
        i++;
    }
    return theArray;
 }


 var car = new object("hello");

 document.write(JSON.stringify(car));

I can't speak to the exact logic for why it does this. You'd ultimately have to ask someone who designed this aspect of Javascript. But, since you can index the various letters of a string by property index like this:

var str = "hello"
console.log(str[1]);   // "e"

You could argue that "0", "1","2", etc... are properties of the string object so since the for loop is just iterating the properties, it should iterate those.

Edit

If you try these:

console.log(Object.getOwnPropertyNames("hello"));
console.log(Object.keys("hello"));

You will see that Javascript clearly thinks that the indexes "0", "1", "2", "3", "4", "5" are all properties on a string. Thus, for (var k in obj) will iterate them just like any other property.

Sign up to request clarification or add additional context in comments.

4 Comments

why my reputation falls down from 6 to 4 was that bad question ? or what i did not understand code so post over here but after posting it my reputations falls down ?
@AliAlmis - Your question got downvoted by someone (not me) probably because they didn't think it was a very clearly stated question or they didn't think it was an appropriate type of question for stack overflow (I don't know which). It is important on this site that a question you asked is very clear about what you're asking and what you're tried so far to solve the problem and that it's on-topic for the type of questions that are appropriate here. It would be nice if downvoters explained why they are downvoting in a comment so you could correct the question. Some people do that, some do not.
@AliAlmis - I added some more to the end of my answer.
@AliAlmis - If this post answered your question, then you can accept it by clicking the green checkmark to the left of the answer. That will indicate to the stack overflow community that your question has now been answered and will earn both you and the person who provided the answer some reputation points for following the desired procedure. If this did not answer your question, then please indicate what is not yet answered.

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.