PDA

View Full Version : basic JavaScript - help!



Gun_Runner
Jan 17th, 2014, 01:23 PM
Hi Guys,

First post on here. I'm hoping some of you could offer some assistance with a JavaScript project I'm working on.

It's pretty basic but I'm struggling to fully understand how JavaScript actually works...

Anyway, here is the task:



Part 1 (if statement, operator)
A shop requires a small program to check the age of customers trying to buy cigarettes.
Create an application that:

1 Asks the user to enter their age.
2 Checks the user is 18 or over and redirects user to this website (http://www.nhs.uk/smokefree)
or otherwise displays a message ‘unable to buy’.


The second part is:


Part 2 (loop)
A teacher requires a program to add seven pupil names and their ages. Create
an application that:

1 Repeats seven times asking for pupil name and age.
2 Displays each pupil name and age on screen.


I'd greatly appreciate any help on this!

Thanks

Philip M
Jan 17th, 2014, 02:42 PM
Have a look at forum rule #1.5.

This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others (especially code that appears to be for homework), but try to help with fixing code that doesn't work. You'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Gun_Runner
Jan 17th, 2014, 03:07 PM
Sorry my apologies...here is the code I have both on the HTML page and my JavaScript file.



<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Cigarette Age Test</title>
<script type="text/javascript" src="javascript.js"></script>
</head>

<body>
<input type="button" value="Enter age" onClick="cigarette()" /><br />

</body>
</html>




function cigarette()
{
var enterAge = prompt ("Enter your age");

if (enterAge>=16)
{
alert(http://www.nhs.uk/smokefree);
}
else (enterAge<16)
{
alert("unable to buy");
}
}


So I'm struggling with how to get the page to automatically redirect to the NHS site if they are older than 18.

Philip M
Jan 18th, 2014, 08:16 AM
So I'm struggling with how to get the page to automatically redirect to the NHS site if they are older than 18.

18 or 16?



if (Number(enterAge)>=16) { // remember that Javascript input values are strings unless/until converted to numbers
window.location = "http://www.nhs.uk/smokefree)";
}

See my recent post in http://www.codingforums.com/showthread.php?t=316739
showing how to prompt repeatedly for a number.

BTW, it is recommended that the opening brace { is placed on the same line as the function, if, else, do, while, or for statement and not on the following line. This is because of something known as automatic semi-colon insertion. If you disregard this advice it is looking out for its chance and one day it will rise up and bite you in the undercarriage, and create an error which can be hard to find.

Here is some help with your second assignment:-



<script type = "text/javascript">

var names = [];
while (name = prompt("Enter the Next Name and Age (separated by a space)","")) {names.push(name)};
alert (names);

// entering nothing will stop script - also no check on validity
</script>