OCUK This Week Only Savings % Script

Ive had a crack at your code (I hope!)

Now that I can help with.
Basically you'd missed changing a few TB to KW.
I also noticed that a few times the W is lowercase so fixed it to be case insensitive. Also I don't believe they list any PSU in KW, so there are some bits that aren't needed, but I left those bits in, just in case.
Also there are a few Corsair AX PSUs that don't state their wattage, so don't get picked up :(

Anyway, he's the slightly amended code for you to compare with yours:
Code:
var table = document.getElementById("productList");
var tbody = table.getElementsByTagName('tbody')[0];
var rows = tbody.getElementsByTagName('tr'); 
var rowArray = new Array();
for (var i = 0; i < rows.length; i++) {
  var size = rows[i].cells.item(1).innerHTML.match(/\d+(W|KW)/i);
  if (size != null) {
    var psuSize = size[0];
    var units = psuSize.match(/(W|KW)/i)[0];
    var price = parseFloat(rows[i].cells.item(4).innerHTML.match(/<span class=\"price\">£\d+\.\d+/)[0].replace(/<span class=\"price\">£/, '').replace(/,/g, '').replace(/[\(\)]/, ''));
    var sizeInW = parseFloat(psuSize.replace(/W/i, "").replace(/KW/i, ""));
    if (units.toUpperCase() == 'KW') {
      sizeInW = sizeInW * 1000;
    }
    var pricePerW = price/sizeInW;
    rows[i].cells.item(4).innerHTML = rows[i].cells.item(4).innerHTML.replace(/<p>/g, '').replace(/<\/p>/g, '').concat("<br/><span style=\"font-size: 12pt; color: red\">£" + pricePerW.toFixed(2) + "/W</span>").concat("<br/><span style=\"font-size: 10pt; color: grey\">£" + (pricePerW*1000).toFixed(2) + "/KW</span>");
  }
  rowArray[i] = new Object;  
  rowArray[i].oldIndex = i;  
  rowArray[i].value = rows[i].getElementsByTagName('td')[4].innerHTML;
}
rowArray.sort(comparePricePerKW);

var newTbody = document.createElement('tbody');  
for (var i=0, length=rowArray.length ; i<length; i++) {  
  newTbody.appendChild(rows[rowArray[i].oldIndex].cloneNode(true)); 
}
table.replaceChild(newTbody, tbody);

function comparePricePerKW(a,b) {
  var aValue = a.value.match(/<span style=\"font-size: 10pt; color: grey\">£\d+.\d+\/KW<\/span>/);
  if (aValue != null) {
    aValue = parseFloat(aValue[0].replace(/<span style=\"font-size: 10pt; color: grey\">£/, '').replace(/\/KW<\/span>/, ''));
  } else {
    aValue = 10000;
  }
  var bValue = b.value.match(/<span style=\"font-size: 10pt; color: grey\">£\d+.\d+\/KW<\/span>/);
  if (bValue != null) {
    bValue = parseFloat(bValue[0].replace(/<span style=\"font-size: 10pt; color: grey\">£/, '').replace(/\/KW<\/span>/, ''));
  } else {
    bValue = 10000;
  }
  return (aValue == bValue ? 0 : (aValue > bValue ? 1 : -1));
}
 
Also I don't believe they list any PSU in KW, so there are some bits that aren't needed, but I left those bits in, just in case.

Brilliant - thanks :D

The only reason I used W and KW is that I didnt want to edit too much of the code so i thought it would be a good substitute for GB and TB :)

Fantastic mate, much appreciated :)
 
Back
Top Bottom