Jump to content

Need Help with a project

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
KaiThxBai

KaiThxBai

    Newbie

  • Members
  • Pip
  • 5 posts
Hey guys im doing a year 11 course in javascript and i really need help i cant figure out how to make it so that if an answer in an if statement isnt true to make it keep going back to asking them till its right.

here is my code.
<SCRIPT LANGUAGE=JavaScript>


function main()

{

var name;

var pet;

var postcode;

var price;

var finalprice;

var card;


postcode = 0;


//asks the user there name.

name = prompt("Hello What is your name?","");


//asks them what pet they want

pet = prompt(""+name+" would you like to buy a cat or a dog?","");

if (pet == "cat") {

alert("Cats Cost $10");

}

else if (pet =="dog")  {

alert("Dogs Cost $20");

}

else {

alert("sorry please enter cat or dog.");

}


//asks them for there postcode.

postcode = prompt(""+name+" what is your postcode?","");

if(postcode.length !=4) {

	alert("Please enter a postcode of 4 digits");

}


//shows them what they have to pay

card = prompt(""+name+" will you be paying with card or cash?","");

if(card == "card") {

	alert("You selected card if you pay cash you get a 10% discount");

}

else if (card == "cash") {

	alert("You are paying cash you get a 10% discount")

}

else {

alert("please enter card or cash");

}



//price of pet

if (pet == "cat") {

	price = 10;

}

	

else if (pet == "dog") {

	price = 20;

}

//final price

if (card == "cash") {

	finalprice=price*0.9;

}


else if (card == "card") {

	finalprice=price*1

}



//the text dispallyed

document.write("Thankyou "+name+" for purchasing a "+pet+"");

document.write(" you are paying with "+card+" the final price is $"+finalprice+"")

document.write(" your Post Code is "+postcode+"")


}


</script>

i need a way to make the things like postcode and that not let them go through with out being 4 digits. any help really apperciated <3

thanks guys :)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
How about a while loop?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
KaiThxBai

KaiThxBai

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:

How about a while loop?

any hints on how to do that =] i tried a few attempts but none work

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Do you know what a while loop is?

JavaScript while Loop

Basically a loop allows you to run a set of code more than once. I.e. until the user inputs data correctly. :) Have a look at w3schools loop section.

#5
KaiThxBai

KaiThxBai

    Newbie

  • Members
  • Pip
  • 5 posts
Yeah i got a basic idea of while loops but i dont understand how to put them into the words part :S? would it just be like

while (postcode === postcode)

{

postcode = prompt(""+name+" what is your postcode?","");

if(postcode.length !=4) {

	alert("Please enter a postcode of 4 digits");

}

}


that just makes it loop and loop :S

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
validcode = false;
while (!validcode)
{
postcode = prompt(""+name+" what is your postcode?","");
if(postcode.length !=4) {
	alert("Please enter a postcode of 4 digits");
}else{
        validcode=true;
}
}

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
KaiThxBai

KaiThxBai

    Newbie

  • Members
  • Pip
  • 5 posts

WingedPanther said:


validcode = false;

while (!validcode)

{

postcode = prompt(""+name+" what is your postcode?","");

if(postcode.length !=4) {

	alert("Please enter a postcode of 4 digits");

}else{

        validcode=true;

}

}

Thank you soooooo much :) i would of never figured that out.

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
an alternative could be:

postcode = prompt(""+name+" what is your postcode?","");

while (postcode.length != 4) {

    alert("Please enter a postcode of 4 digits");

    postcode = prompt(""+name+" what is your postcode?","");

}


The disadvantage is that you have your prompt on two places, the advantage is no if and no status variable, probably easier to read.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#9
KaiThxBai

KaiThxBai

    Newbie

  • Members
  • Pip
  • 5 posts

orjan said:

an alternative could be:

postcode = prompt(""+name+" what is your postcode?","");

while (postcode.length != 4) {

    alert("Please enter a postcode of 4 digits");

    postcode = prompt(""+name+" what is your postcode?","");

}

The disadvantage is that you have your prompt on two places, the advantage is no if and no status variable, probably easier to read.

Thank you that one is much easyer to understand :) you guys are to smart finally done :)
<SCRIPT LANGUAGE=JavaScript>


function main()

{

var name;

var pet;

var postcode=0;

var price;

var finalprice;

var card;



//asks the user there name.

name = prompt("Hello What is your name?","");


//asks them what pet they want

pet = prompt(""+name+" would you like to buy a cat or a dog?","");

while (pet != "cat" && pet != "dog"){

alert (""+name+" Please enter cat or dog.")

pet = prompt(""+name+" please enter cat or dog.","");

}


//asks them for there postcode.


postcode = prompt(""+name+" what is your postcode?","");

while (postcode.length !=4) {

	alert("Please enter a postcode of 4 digits");

 postcode = prompt(""+name+" Please enter a 4 digit postcode","")

}



//shows them what they have to pay


card = prompt(""+name+" will you be paying with card or cash?","");

while (card !="cash" && card != "card") {

	alert(""+name+" please enter card or cash.");

	card = prompt (""+name+" Please enter card or cash","")

}


//price of pet

if (pet == "cat") {

	price = 10;

}

	

else if (pet == "dog") {

	price = 20;

}

//final price

if (card == "cash") {

	finalprice=price*0.9;

}


else if (card == "card") {

	finalprice=price*1

}



//the text dispallyed

document.write("Thankyou "+name+" for purchasing a "+pet+"");

document.write(" you are paying with "+card+" the final price is $"+finalprice+"")

document.write(" your Post Code is "+postcode+"")


}


</script>

Edited by KaiThxBai, 21 March 2009 - 02:47 PM.