Shorter code for cookies

I have an exam shortly.
The code I use for 2 cookies qsts is too long to remember at my age(night class). Could the code be made shorter if possible.
Q1.//checks the validity of an irish PPS number
function checkPPS(){
var formValue=document.forms.pps.ppsValue.value;
if(formValue.length!=8){
alert("the Irish PPS number is length 8 and type 1234567X");
}
else{
var last=formValue.substring(7,8);
last=last.toUpperCase().charCodeAt(0);
var number=formValue.substring(0,7);
var testValue=0;
var multiplier=8;
for(i=0; i=0)&&(testValue<=10)){
valid="IS";
}
else{
valid="IS NOT";
}
alert(formValue+" "+valid+" a valid ISBN-10 number");
}
}
function checkIDN(){
var map= new Array("T","R","W","A","G","M","Y","F","P","D","X","B","N","J","Z","S","Q","V","H","L","C","K","E");
var formValue=document.forms.IDN.IDNValue.value;
var lastElementPosition = formValue.length-1;
var lastDigit = formValue.charAt(lastElementPosition);
lastDigit= lastDigit.toUpperCase();
var regExp= new RegExp("[^A-Z]$");
if((formValue.length!=9) && (regExp.test(lastDigit))){
alert("the IDN number is length 9 and type 12345678X");
}
else{
var idn=parseInt(formValue.substring(0,lastElementPosition));
idn=idn%23;
if(lastDigit == map[idn]){
valid="IS";
}
else{
valid="IS NOT";
}
alert(formValue+" "+valid+" a valid IDN number");
}
}
Q2.html>


function getCookie(c_name)
{
if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
username=getCookie('username');
if (username!=null && username!="")
{
alert('Welcome again '+username+'!');
}
else
{
username=prompt('Please enter your name:',"");
if (username!=null && username!="")
{
setCookie('username',username,365);
}
}
}







Any input would be appreciated

Comments

  • : I have an exam shortly.
    : The code I use for 2 cookies qsts is too long to remember at my
    : age(night class). Could the code be made shorter if possible.

    Not by much. Using regex and building arrays is highly efficient. That's the problem with validation... no easy way about it. Where do you study that you know the test questions before test day!
  • : : I have an exam shortly.
    : : The code I use for 2 cookies qsts is too long to remember at my
    : : age(night class). Could the code be made shorter if possible.
    :
    : Not by much. Using regex and building arrays is highly efficient.
    : That's the problem with validation... no easy way about it. Where do
    : you study that you know the test questions before test day!

    Internet studies & web design. There will be 2 javascript questions, both on cookies. Both of these are what was covered on the course. Other likely were ruled out," We did not have enough time to cover them".
    The general view is those two questions, I hope, time will tell.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion