I'm writing a code to gather the last certain number of products viewed on an e-commerce site. I'm able to write it just fine, but it seems like there should be an easier way to do it with a for or a while loop. If I ever decide I need more products to be stored in the future, then I have to write more code, and it get's n+1 longer with each item. Here's the code right now (up to 3 items)
var url = $.cookie('mycookie');
var myArray = url.split('»');
var myArrayLen=myArray.length;
if (myArrayLen == 1){
if (url==currentUrl)
{
var recentProducts='';
$.cookie('mycookie', '»'+currentUrl, { path: '/', expires: cookieExp });
}
else
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
$.cookie('mycookie', url+'»'+currentUrl, { path: '/', expires: cookieExp });
}
}
else if (myArrayLen == 2){
if (currentUrl==myArray[1])
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
$.cookie('mycookie', '»'+myArray[2]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
else if (currentUrl==myArray[2])
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
$.cookie('mycookie', '»'+myArray[1]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
else
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
$.cookie('mycookie', '»'+myArray[1]+'»'+myArray[2]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
}
else if (myArrayLen == 3){
if (currentUrl==myArray[1])
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[2] + '">Link1</a>';
recentProducts += '<a href="' + myArray[3] + '">Link2</a>';
$.cookie('mycookie', '»'+myArray[2]+'»'+myArray[3]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
else if (currentUrl==myArray[2])
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
recentProducts += '<a href="' + myArray[3] + '">Link2</a>';
$.cookie('mycookie', '»'+myArray[1]+'»'+myArray[3]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
else if (currentUrl==myArray[3])
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
$.cookie('mycookie', '»'+myArray[1]+'»'+myArray[2]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
else
{
var recentProducts='<div class="mycookie"><h4>Recently Viewed Products</h4>';
recentProducts += '<a href="' + myArray[1] + '">Link1</a>';
recentProducts += '<a href="' + myArray[2] + '">Link2</a>';
recentProducts += '<a href="' + myArray[3] + '">Link3</a>';
$.cookie('mycookie', '»'+myArray[1]+'»'+myArray[2]+'»'+myArray[3]+'»'+currentUrl, { path: '/', expires: cookieExp });
}
}
recentProducts='</div>';
As you can see, 1 items requires one if/else, 2 items requires an if, else if, and else; 3 items requires an if, else if, else if, and else. And so on.
This script is looking at the current URL and comparing it to the array, if it's in the array, then it won't display the current URL, but it'll append it to the end of the array so when you go to the next item it will display. Let me know if you have any questions. Help is greatly appreciated!