Here is what I've wrote for an assignment..I would simply like to add a space between the song name and year and enclose the year in parentheses. Thanks yet again.

Jake

<script type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

var favSongs = new Array(19);


favSongs[0] = "Falls Apart";
favSongs[1] = "Monster";
favSongs[2] = "All the Right Moves";
favSongs[3] = "Beautiful Thieves";
favSongs[4] = "Walk Away";
favSongs[5] = "The Way You Lived";
favSongs[6] = "Welcome Home";
favSongs[7] = "Wish You Were Here";
favSongs[8] = "Times Like These";
favSongs[9] = "I'd Come for You";
favSongs[10] = "Second Chance";
favSongs[11] = "So Far So Good";
favSongs[12] = "Sooner or Later";
favSongs[13] = "Little Sister";
favSongs[14] = "Down";
favSongs[15] = "Gasoline";
favSongs[16] = "Lonely Day";
favSongs[17] = "Are You With Me";
favSongs[18] = "Lost In You";
favSongs[19] = "Savior";

var songYear = new Array(19);

songYear[0] = "2007";
songYear[1] = "2009";
songYear[2] = "2009";
songYear[3] = "2009";
songYear[4] = "2009";
songYear[5] = "2005";
songYear[6] = "2005";
songYear[7] = "2001";
songYear[8] = "2002";
songYear[9] = "2008";
songYear[10] = "2008";
songYear[11] = "2004";
songYear[12] = "2004";
songYear[13] = "2005";
songYear[14] = "2002";
songYear[15] = "2002";
songYear[16] = "2005";
songYear[17] = "2010";
songYear[18] = "2009";
songYear[19] = "2008";


document.write("<p>Here are 20 of my favorite songs along with the year each was published.</p>");

for (var count =  0; count < 20; ++count) {
	document.write(favSongs[count] + songYear[count] + "<br />");
}



// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>

Recommended Answers

All 3 Replies

You need to modify your string composition a little bit as below.

for (var count =  0; count < 20; ++count) {
	document.write(favSongs[count] + " (" + songYear[count] + ")<br />");
}

That worked. Thanks. I had played around with the quotation marks and parentheses but couldn't get that to work. Could you explain to me as to why I need a "+" sign on each side of songYear[count]? Especially when there is one alreaady on the outside of the left parentheses? Thanks again.

OK, let we look at the line...

document.write(favSongs[count] + " (" + songYear[count] + ")<br />");

If you dissect this code into 2 major part, one is the function itself and the other is an argument. You would see...

document.write(string)  // a function which requires a string as its argument
favSongs[count] + " (" + songYear[count] + ")<br />"  // argument

Your question is related to the argument (string). When you compose a string, you may use multiple variables concatenate with multiple strings. There are ways to compose a string. In this case, the string is concatenate (using plus (+) signs) in 1 line of code. Anything in between double quotation (") are strings. Anything outside it must be variables.

favSongs[count]  // an array variable (favSongs) with integer index variable (count)
" ("  // a string
songYear[count]  // another array variable (songYear) with integer index variable (count)
")<br />"  // another string

// example for concatenating a string one by one
aString = favSongs[count]   // assign the value in favSongs[count] to aString
aString += " ("             // concatenate " (" to aString
aString += songYear[count]  // concatenate the value in songYear[count] to aString
aString += ")<br />"        // concatenate ")<br />" to aString
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.