Click here to Skip to main content
15,884,748 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i read this code snippet in book but it have to show Hello Ima Reader based on description but my problem is it shows just a

What I have tried:

function makeHello(name) {
name[name.length] = "Hello " + name[0] + " " + name[1];
}
var name = new Array('Ima','Reader');
makeHello(name);
alert(name[2]); // displays "Hello Ima Reader"
Posted
Updated 1-Jan-17 19:57pm

1 solution

The problem is that you are overwriting the name property, which is the name of the window[^]. By changing this to an array, it will be automatically converted to a string, so name[0] will take a single char. To fix this problem, just rename your name variable to something else.
JavaScript
function makeHello(myName) {
    myName[myName.length] = "Hello " + myName[0] + " " + myName[1];
}
var myName = new Array('Ima','Reader');
makeHello(myName);
alert(myName[2]); // displays "Hello Ima Reader"
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900