2017-03-25 5 views
-4

私は最近、新しい放棄されたソフトウェア(より正確な製品ファミリー)の登録コードジェネレータを書いて、HTML/JavaScript。私はすべてのビジュアル要素を機能させるページのHTML側を理解しています。私が知りたいことは、すべてのJSが背後で何をしているのかということです。私はそれについて興味があった私の友人にもっと説明することができます。誰かが次のコードを私の友人に説明できる基本的なステートメントに分解できますか?私は本当に何もJSを知らないので、これは私にちょっとした情報を与えるでしょう。友人のコードのブロックを説明する方法

function generate() { 
var vc = 178890; 
var edition = [ 
    [223987, 543238], 
    [998732, 215588], 
    [776490, 366591] 
]; 
var module = [ 
    [322547, 226789], 
    [632788, 129874], 
    [399872, 112256], 
    [200876, 679032], 
    [666634, 188897], 
    [877334, 766952], 
    [222990, 777778], 
    [229347, 543832] 
]; 
var i; 
var sum = 0; 
var name = $('#name').val(); 
var addr = $('#address').val(); 
for (i = 0; i < name.length; i++) { 
    sum += name.charCodeAt(i); 
} 
for (i = 0; i < addr.length; i++) { 
    sum += addr.charCodeAt(i); 
} 
var s = 12*(sum + Math.floor(sum*67/12472)); 
if ($('#product').val() === 'avs') { 
    s += 886; 
} else { 
    s += 288; 
    if ($('#timed').prop('checked')) { 
     reg = moment($('#regdate').val(), 'MM/DD/YYYY'); 
     exp = moment($('#expdate').val(), 'MM/DD/YYYY'); 
     var date_re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 
     var valid = true; 
     $('#invalidreg').html(reg.creationData().input.search(date_re) >= 0 && reg.isValid() ? '' : (valid = false, ' invalid date')); 
     if (exp.creationData().input.search(date_re) >= 0 && exp.isValid()) { 
      if (reg.isSameOrAfter(exp)) { 
       $('#invalidexp').html('expiration date should be after registration date'); 
       valid = false; 
      } 
      else { 
       $('#invalidexp').html(''); 
      } 
     } else { 
      $('#invalidexp').html('invalid date'); 
      valid = false; 
     } 
     if (!valid) { 
      clear(); 
      return; 
     } 
     var epoch = moment('1800-12-28'); 
     s += 3*exp.diff(epoch, 'days') - 2*reg.diff(epoch, 'days') - 34; 
    } 
} 
$('#serialnum').html(67*sum); 
$('#versioncode').html(vc + s); 
var row = $('#edition tr:nth-child(1)'); 
for (i = 0; i < 3; i++) { 
    row.find('td:nth-child(2)').html(edition[i][0] + s); 
    row.find('td:nth-child(3)').html(edition[i][1] + s); 
    row = row.next(); 
} 
row = $('#modules tr:nth-child(1)'); 
for (i = 0; i < 8; i++) { 
    row.find('td:nth-child(2)').html(module[i][0] + s); 
    row.find('td:nth-child(3)').html(module[i][1] + s); 
    row = row.next(); 
} 

注:のために尋ねられたとき、これは言ったコード化されたサードパーティの開発者は、彼が1人の醜い子である

+4

このコードを書いた人はごめんなさい、それを維持しなければならない人にはさらに残念です。それは混乱です。それを正しく説明すると、それを書き換えるよりも時間がかかります。 – Gerrit0

答えて

0

「そのようなノートを持っていなかった」「設計時には、文書に使用されるプロセスを促進するためにノート」スクリプティングモンスターの1分あたりの鼓動率は指数関数的に上昇しました。

function generate() { 

    // Random (arrays of) magic numbers (probably the key to the universe) 

    var vc = 178890; 
    var edition = [ 
     [223987, 543238], 
     [998732, 215588], 
     [776490, 366591] 
    ]; 
    var module = [ 
     [322547, 226789], 
     [632788, 129874], 
     [399872, 112256], 
     [200876, 679032], 
     [666634, 188897], 
     [877334, 766952], 
     [222990, 777778], 
     [229347, 543832] 
    ]; 

    // Retrieving the values of Name & Address form inputs as "name" and "addr" with an 
    // external library called jQuery 

    var name = $('#name').val(); 
    var addr = $('#address').val(); 


    // Beginning of a block to calculate "sum" (set sum to zero) - the next couple of lines 
    // seem straightforward, I just have no idea what objective these incantations have. 

    var i; 
    var sum = 0; 

    // Iterate over all the letters in the "name", retrieve the letter's 
    // character code and increase sum by the value of that character code 

    for (i = 0; i < name.length; i++) { 
     sum += name.charCodeAt(i); 
    } 

    // Iterate over all the letters in the "addr", retrieve the letter's 
    // character code and increase sum by the value of that character code 

    for (i = 0; i < addr.length; i++) { 
     sum += addr.charCodeAt(i); 
    } 

    // Do some more magic number cacluations with sum, to retrieve a value "s" 

    var s = 12*(sum + Math.floor(sum*67/12472)); 

    // If the value of the "product" input is avs, increase the value of s by magic number 886 

    if ($('#product').val() === 'avs') { 
     s += 886; 
    } else { 

    // Otherwise, increase it by 288 

     s += 288; 

    // If the "timed" checkbox has been selected ... 

     if ($('#timed').prop('checked')) { 

    // Use the external library "moment" to normalize the input data from "regdate" and "expdate" fields 

      reg = moment($('#regdate').val(), 'MM/DD/YYYY'); 
      exp = moment($('#expdate').val(), 'MM/DD/YYYY'); 

    // As we don't trust the "moment" date validation, we#re also going to use a regex to check the 
    // validity of "regdate" 

    // The regex checks if the date is formatted with slashes, such as: 23/12/2004 

      var date_re = /^\d{1,2}\/\d{1,2}\/\d{4}$/; 

    // Somewhere in the murky depths further down there's probably a global check, if the input data 
    // is valid, so we need a global flag for it. 

      var valid = true; 

    // Now we apply the regex we defined earlier and use moment's own "isValid" method to check if the 
    // "reg" date is actually valid. 

    // If it's not, we inject an error message into the HTML element "invalidreg" and set the global 
    // valid flag to false 

    // If "reg" is a valid date, nothing happens. 

     $('#invalidreg').html(reg.creationData().input.search(date_re) >= 0 && reg.isValid() ? '' : (valid = false, ' invalid date')); 

    // Now we also validate the "exp" date in the same manner ... 

     if (exp.creationData().input.search(date_re) >= 0 && exp.isValid()) { 

    // ... but we also check, if it is the same as the "reg" date. If they are the same, we output 
    // an error message into the "invalidexp" HTML element and set the "valid" flag to false 

    // If they are not the same, we reset the contents of that HTML element. 

      if (reg.isSameOrAfter(exp)) { 
       $('#invalidexp').html('expiration date should be after registration date'); 
       valid = false; 
      } else { 
       $('#invalidexp').html(''); 
      } 

    // If "exp" is not valid we display an error message in the "invalidexp" HTML 
    // element (and set the valid flag to false) 

     } else { 
      $('#invalidexp').html('invalid date'); 
      valid = false; 
     } 

    // If the global flag is false (ie "exp" or "reg" date are invalid or the same) .. 

     if (!valid) { 

    // Call a "clear" method (not defined in this fragment of code) and cease further processing 
    // by calling a return 

      clear(); 
      return; 
     } 

    // If everything is valid (*yay*) - define a new magic date on the 28/12/1800 
    // (who know's why this particular date) 

     var epoch = moment('1800-12-28'); 

    // And we're back to magic number caclulations on the value of "s" 
    // where we are adding 3 times the difference between the magic epoch date and the "exp" date 
    // and then subtracting 2 times the difference between the epoch and the "reg" date. 
    // Oh, and subtractign 34 ... the *bleeep* knows why. 

     s += 3*exp.diff(epoch, 'days') - 2*reg.diff(epoch, 'days') - 34; 
    } 
} 

// We now wet an HMTL element "serialnum" to the 67 times the value of "sum" 
// (which we calculated earlier) 

$('#serialnum').html(67*sum); 

// The version code is the value of vc (you can find it right at the top of this script ... it's 178890) 
// plus the value of s. We display this value in an HTML element "versioncode" 

$('#versioncode').html(vc + s); 

// We now grab a new variable and set it to the first row in an HTML table element. 

var row = $('#edition tr:nth-child(1)'); 

// For the first three rows of the table (starting with the row we just defined), we set the values of 
// the 2nd and 3rd table cells to a random number calculation of "s" and "edition" 
// (edition is defined right at the top of the script) 

for (i = 0; i < 3; i++) { 
    row.find('td:nth-child(2)').html(edition[i][0] + s); 
    row.find('td:nth-child(3)').html(edition[i][1] + s); 
    row = row.next(); 
} 

// Same thing for a different table, and with the "module" magic number. 

row = $('#modules tr:nth-child(1)'); 
for (i = 0; i < 8; i++) { 
    row.find('td:nth-child(2)').html(module[i][0] + s); 
    row.find('td:nth-child(3)').html(module[i][1] + s); 
    row = row.next(); 
} 
関連する問題