/    Sign up×
Community /Pin to ProfileBookmark

Concantenating numeric variable with string.

Hi,
I am trying to work through javascript using a tutorial. I came upon a problem that I cannot figure out why it doesn’t work.

The idea is to create a random number using an external file; then to combine it with a string value which names an image file, then load the image file into the html page.
(note: the random.js works fine. I tested by just using the variable and 5 random numbers appeared)

The script I have is:
==============================================

<head>
<script>
function showimg() {
/* The showimg() function displays a random image from the 0.jpg

through 9.jpg files. The random image is designed to thwart

hackers attempting to enter the library records database by
requiring visual confirmation.*
/

var imgNumber = randomInteger(9); */ return a random number from 0
to 9*/
document.write (“<img src=’imgNumber.jpg alt=” /’>”);

}
</script>
</head>
<body>
(showimg)

(showimg)

(showimg)

(showimg)

(showimg)

</body>

I am open to constructive criticism, so any and all information is appreciated.

Thanks in advance,
jake

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ZeroKilledOct 26.2008 — this line

document.write ("<img src='imgNumber.jpg alt='' /'>");

should be

document.write ("<img src='" + imgNumber + ".jpg' alt='' />");

javascript don't have the ability of variable interpolation, that is, to use the value of a variable as part of a string. so, the solution here is to split the string into various pieces and concatenate them using others value wherever necessary.
Copy linkTweet thisAlerts:
@jake7363authorOct 26.2008 — Zerokill,

It worked perfectly! Thanks very much for the explanation as well.

jake
×

Success!

Help @jake7363 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...