0

I have stored a set of strings in an array in JavaScript. When I tried to retrieve the first string from that array by using indices, it retrieves the first character instead of the first string.

How do I get the entire string in the first index of the array, instead of getting the characters?

Here is the code:

var name=["hello","avr","karthik","ajay"];
var count=-1;

function begin(which) {
    if(which==0){
        if(count==0){
            count=3;
        }
        else{
          count--;
        }
    }
    else{
       count++;
       if(count>3){
           count=0;
       }
    }
    var message=document.getElementById("message");
    message.innerHTML=name[count];
}
AGE
  • 3,752
  • 3
  • 38
  • 60
  • You should properly indent your code – SLaks Dec 29 '15 at 15:39
  • I have tried it by using editplus. The browser in it has retrieved the entire string instead of characters. But chrome and firefox are not retrieving the string. –  Dec 29 '15 at 15:40
  • could you make sure that `name` in the function `begin` is the array defined as in your post ? – Hacketo Dec 29 '15 at 15:41
  • @Hacketo, ya. Chrome and browser have displayed h at the 1st call, e at the 2nd call and l at the 3rd call when count is incremented. It tells that the "name" is the same as defined above. –  Dec 29 '15 at 15:43
  • what input are you feeding the which variable ? because if which = 0 , then count becomes -2 . and that would be out of bounds . – saruftw Dec 29 '15 at 15:43
  • 1
    MMh it seems to work: https://jsfiddle.net/kajyr/wzywrzrh/ ( with chrome) – Carlo Dec 29 '15 at 15:43
  • @saru, ya. I have corrected it and make sure that it will not become -2. Which will be given either 0, indicating display the previous name, or 1 indicating display the next name. But it has corrected the possibility of array index out of bound but not displaying the characters. –  Dec 29 '15 at 15:47
  • 1
    Change your variable name from `name` to something else...I had the same problem that fixed it. – brso05 Dec 29 '15 at 15:48
  • http://www.w3schools.com/js/js_reserved.asp – brso05 Dec 29 '15 at 15:50
  • @brso05, ya. Thanks. That has solved my problem too. But can someone explain the reason why the specified browsers didn't get the entire string when I have used "name" as a variable? –  Dec 29 '15 at 15:51
  • 1
    oh - of course. You're clashing with `window.name`. – James Thorpe Dec 29 '15 at 15:53
  • Oh. I didn't know that. I have google about it. Now I understood why they have behaved so. Thank you @James –  Dec 29 '15 at 15:55

1 Answers1

1

I recreated your example and had the same problem. Try changing var name to something else like var test for example. That solved the problem.

brso05
  • 13,142
  • 2
  • 21
  • 40
  • Ya. That has solved my problem. Thank you. :-) But can someone explain the reason why the specified browsers didn't get the entire string when I have used "name" as a variable? –  Dec 29 '15 at 15:53
  • Check it out - http://www.w3schools.com/js/js_reserved.asp – brso05 Dec 29 '15 at 15:54
  • 4
    @AvinashReddyPaduri When running in a browser, your global variable `name` is clashing with the `window.name` property, which can only ever be set to a string. So it's actually doing a `.join` on your array, then assigning that, which is why you're only seeing characters indexed within that joined string, rather than individual array items. – James Thorpe Dec 29 '15 at 15:54
  • The reason is that when you use `name` you are actually setting `window.name` . See https://developer.mozilla.org/en-US/docs/Web/API/Window/name – user5325596 Dec 29 '15 at 15:54
  • @JamesThorpe this should be the answer :) – Hacketo Dec 29 '15 at 15:55
  • @Hacketo Yeah, but I also feel this is a dupe, though I went looking for a decent canonical one the other day without luck, perhaps it's time to write one. – James Thorpe Dec 29 '15 at 15:55
  • @Hacketo [Found this question](http://stackoverflow.com/questions/10523701/using-the-variable-name-doesnt-work-with-a-js-object) - will be using it from now on as a dupe target. Can't do so here as I already CVed (then retracted) for another reason. – James Thorpe Dec 29 '15 at 16:05