/    Sign up×
Community /Pin to ProfileBookmark

JavaScript loop problem

Hi guys,

When I run my code, showProducts() function is called and prints text to the screen fine. But once it enters the for loop in the function, it messes up . I.e. it doesn’t print no more than one line of products. (by the way I’m not bothered about security issues withe logging in as it’s just an assignment).

—————————————CODE———————————

<HTML>
<HEAD> M150 TMAO3 Question 2
<TITLE>SOFASPEND – IN THE BEST POSSIBLE TASTE!</TITLE>
<SCRIPT language=”JavaScript” type=”text/javascript”>
// These are arrays containing customer data
var customerCodes = [‘lt-17’, ‘gw-3’, ‘lg-9’, ‘il-77’, ‘fy-32’];
var customerPasswords = [‘password’, ‘bluesky’, ‘dr45gr6’, ‘onlyme’, ‘lothlorien’];

// These are arrays containing product data
var productCodes = [‘a-23-009’, ‘k-246-07’, ‘d-555-01’, ‘n-87-012’, ‘u-521-08’, ‘t-99-002’];
var productDescriptions = [‘Superslurry electric blender’, ‘Apple – iPod (second-hand)’, ‘CoziNap nylon duvet tog 2’, ‘Headbanger mini hi-fi 20W’, ‘MagiBoot shoe cleaning kit’, ‘The PushmiPulu lawnmower’];
var productPrices = [45, 50, 15, 25, 75, 7, 60];
var productStock = [200, 0, 2, 500, 500, 15];

// These are useful variables
var customerIndex;
/*……………………………………………………………………………..*/

var bFlag = false;
var start;
while (bFlag == false)
{
start = window.prompt(‘Please enter your unique password.’,”);
checkPassword(start);
};
document.write(‘<BR><BR>Welcome, ‘ + customerCodes[customerIndex] + ‘. Ready to start shopping?<BR>’);
showProducts();

function checkPassword(password)
{
for (var i = 0; i < 5; i++)
{
if (password == customerPasswords[i])
{
customerIndex = i;
bFlag = true;
return;
}
}
}

function showProducts()
{
document.write(‘<BR> Here are the products currently in stock:<BR>’);
document.write(‘Item—Code———Product———————-Price– -Stock<BR>’);
document.write(‘============================================<BR>’);
for (var i = 0; i < 5; i++)
{
document.write(i + ‘—–‘ + productCodes[i] + ‘—-‘ + productDescriptions[i] + ‘—–‘ + productPrices[i] + ‘—-‘ + productStock[i] + ‘<BR’);
}
}

</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

to post a comment
HTML

1 Comments(s)

Copy linkTweet thisAlerts:
@AltriakMar 05.2008 — You're missing an enclosing for the BR symbol. See if that helps ?
×

Success!

Help @n_codrington 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...