2014-03-12 15 views
0

這是在我將之前在PHP中放置的同一個腳本轉換爲JS之後開始的(我試圖更改所有語法)。我在我的PHP文件中有一個JS腳本,它似乎並沒有執行

我試圖運行它,它是如何的PHP文件中,並沒有奏效:

<html> 
<head> 
<title>Learn | A Level Scientist</title> 
<link rel="shortcut icon" href="http://www.iconj.com/ico/f/0/f0ghi1ksdc.ico" type="image/x-icon" /> 
<style> 
    #menubar {;color:white;font-size:100%;padding-top:5px;padding-bottom:5px; 
    border-radius:5px;margin-top: 1%;margin-bottom:1%;margin-left:4%;margin-right:4%; background: rgba(0,73,126,0.6);} 

    span {margin-left:2.5%;margin-right:2.5%;} 
    #mainsection {background: rgba(0,73,126,0.6);color:white;margin-left:4%;margin-right:4%;border-radius:5px;padding-left:20px;padding-right:20px;padding-bottom:0.5%;text-align:center;} 
    body {background:radial-gradient(#00477C,#002E4F);} 
    #horizsep {width:100%;text-align:center;color:white;padding-top:0%;padding-bottom:0%;margin:0px} 
    #copyright{text-align:center;} 
    #welcomemsg {font-size:30px;margin:0px;padding:0px} 
    #surroundmid{font-size:26px; padding-bottom:160px;} 
    #start_learning:hover {width:155px;font-size:22px;color:white;background-color:#5288AB;border-width:0px;border-radius:5px;} 
    #tube_part {background-color:#DB2625;margin:0%;} 
    #you_part {margin:0%;color:black;border-radius:30px;text-align:center} 
    #fb_part {background-color:#3B5998;color:white;margin:0%;text-align:center} 
    #acebook_part {background-color:#3B5998;margin:0%} 
    a{color:white; underline:none} 
    a:hover {color: #4DB849 ; underline:none} 
    a:hover {color: #4DB849 ; underline:none} 
    a:hover {color: #4DB849 ; underline:none} 
    a:clicked {color: white; underline:none} 
    *.menubar {border-width:0px;border-radius:1px;background:rgba(0,0,0,0.1);color:white;} 
    *.menubar:hover{color:white;background-color:#5288AB;border-width:0px;border-radius:1px;} 
    #loginform {display:inline-block;margin:0px} 
    #chembutton {width:30%;height:10%;font-size:40px;color:white;background:rgba(17,214,118,0.3);border-width:0px;border-radius:5px;} 
    #chembutton:hover {width:32%;height:11%;font-size:45px;color:white;background:rgba(17,214,118,0.5);border-width:0px;border-radius:5px;} 
    #chembutton:active {width:33%;height:12%;font-size:45px;color:white;background:rgba(17,214,118,0.7);border-width:0px;border-radius:5px;} 
    #ytvid {margin-top:0%;margin-bottom:4.7%} 
    #video_navigation_next {width:40%;display:inline-block;} 
    #video_navigation_previous {width:40%;display:inline-block;} 
    #interface {display:block;width:900px;height:500px;background:rgba(0,46,79,0.4);margin:auto;border-radius:5px;margin-top:5px;margin-bottom:20px;vertical-align:top;} 
    #output{width:550px;border-radius:5px;height:450px;background:rgba(47,94,130,0.4);display:inline-block;margin:25px;vertical-align:top;font-size:18px} 
    #input{width:250px;border-radius:5px;height:450px;background:rgba(47,94,130,0.4);display:inline-block;margin:25px;margin-left:0px;vertical-align:top;} 
    #useranswer {margin-top:40px} 
    #helpsection {margin:10%} 
    </style> 
</head> 
<body> 
<section> 


<?php 
    include 'C:\xampp\htdocs\ALevelScientistTesting\menubar.php' ; 
?> 



<div id="mainsection"> 
<div id="welcomemsg"><strong><u> Working out Relative Formula/Molecular Masses </u></strong></div><br> 
<div id="video_navigation_previous"><a href="makible.php"><br><span id="nextvid"> <!-- <= Go to the Previous Exercise --> <span></a></div> 
<div id="video_navigation_next"><a href="chlowatreat.php"><br><span id="nextvid"> Go to the Next Exercise => <span></a></div> 
<div id="interface"> 
<div id="output"> 

    <?php 
    //echo 'The '.$CoMo.' : '.$SubName[$FormNo].' Has a Relative '.$FoMo.' Mass of : '.$x; 
    //echo '<br> Work out the Relative '.$FoMo.' Mass of the '.$CoMo.' : '.$SubName[$FormNo] ; 
    ?> 

    <script type="text/javascript"> 

    document.write('The Program got to here...'); 

    //The following 3 Arrays store 3 things. 1) The Element names. 2) The element Symbols. 3) The Relative Atomic Masses of the Elements. 
    var Elements = new Array("Hydrogen","Lithium","Sodium","Potassium","Rubidium","Caesium","Francium","Beryllium","Magnesium","Calcium","Strontium","Barium","Radium","Scandium","Yttrium","Lanthanum","Actinium","Titanium","Zirconium","Halfnium","Rutherfordium","Vanadium","Niobium","Tantalum","Dubnium","Chromium","Molybdenum","Tungsten","Seaborgium","Manganese","Technetium","Rhenium","Bohrium","Iron","Ruthenium","Osmium","Hassium","Cobalt","Rhodium","Iridium","Meitnerium","Nickel","Palladium","Platinum","Darmstadtium","Copper","Silver","Gold","Roentgenium","Zinc","Cadmium","Mercury","Boron","Aluminum","Gallium","Indium","Thallium","Carbon","Silicon","Germanium","Tin","Lead","Nitrogen","Phosphorus","Arsenic","Antimony","Bismuth","Oxygen","Sulfur","Selenium","Tellurium","Polonium","Flourine","Chlorine","Bromine","Iodine","Astatine","Helium","Neon","Argon","Krypton","Xenon","Radon"); 
    var ElementsSym = new Array("H","Li","Na","K","Rb","Cs","Fr","Be","Mg","Ca","Sr","Ba","Ra","Sc","Y","La","Ac","Ti","Zr","Hf","Rf","V","Nb","Ta","Db","Cr","Mo","W","Sg","Mn","Tc","Re","Bh","Fe","Ru","Os","Hs","Co","Rh","Ir","Mt","Ni","Pd","Pt","Ds","Cu","Ag","Au","Rg","Zn","Cd","Hg","B","Al","Ga","In","Tl","C","Si","Ge","Sn","Pb","N","P","As","Sb","Bi","O","S","Se","Te","Po","F","Cl","Br","I","At","He","Ne","Ar","Kr","Xe","Rn"); 
    var ElementsRAM = new Array("1.0","6.9","23.0","39.1","85.5","132.9","223","9.0","24.3","40.1","87.6","137.3","226","45.0","88.9","138.9","227","47.9","91.2","178.5","261","50.9","92.9","180.9","262","52.0","95.9","183.8","266","54.9","98","186.2","264","55.8","101.1","190.2","277","58.9","102.9","192.2","268","58.7","106.4","195.1","271","63.5","107.9","197.0","272","65.4","112.4","200.6","10.8","27.0","69.7","114.8","204.4","12.0","28.1","72.6","118.7","207.2","14.0","31.0","74.9","121.8","209.0","16.0","32.1","79.0","127.6","209","19.0","35.5","79.9","126.9","210","4.0","20.2","39.9","83.8","131.3","222"); 

    // The following 3 arrays store all of the molecule names and formulas, along with the subscripted versions of all of the formulas. 
    var CompoundsFormula = new Array("Al2O3","NH4N3","NH4ClO3","NH4ClO4","BaCrO4","BeCO3","C6H12N2O4Pt","CrO2F2","C3Cl3N3","GaP","LiCoO2","FeLiO4P","Li2SO4","OF2","KCaCl3","Ag2CrO4","AgBF4","H3NO3S","ZnBr2","Na2CO3","BaFe2O4","BrF5","CaCrO4","H2CO3","MgCO3","AgClO3","Ag3PO4","NaPO2H2","NaMnO4","Na2S2O8"); 
    var CompoundsName = new Array("Aluminium oxide","Ammonium azide","Ammonium chlorate","Ammonium perchlorate","Barium chromate","Beryllium carbonate","Carboplatin","Chromyl fluoride","Cyanuric chloride","Gallium phosphide","Lithium cobalt oxide","Lithium iron phosphate","Lithium sulfate","Oxygen difluoride","Potassium calcium chloride","Silver chromate","Silver fluoroborate","Sulfamic acid","Zinc bromide","Sodium carbonate","Barium ferrite","Bromine pentafluoride","Calcium chromate","Carbonic acid","Magnesium carbonate","Silver chlorate","Silver orthophosphate","Sodium hypophosphite","Sodium permanganate","Sodium persulfate"); 
    var SubCompoundsArray = new Array("Al<sub>2</sub>O<sub>3</sub>","NH<sub>4</sub>N<sub>3</sub>","NH<sub>4</sub>ClO<sub>3</sub>","NH<sub>4</sub>ClO<sub>4</sub>","BaCrO<sub>4</sub>","BeCO<sub>3</sub>","C<sub>6</sub>H<sub>1</sub><sub>2</sub>N<sub>2</sub>O<sub>4</sub>Pt","CrO<sub>2</sub>F<sub>2</sub>","C<sub>3</sub>Cl<sub>3</sub>N<sub>3</sub>","GaP","LiCoO<sub>2</sub>","FeLiO<sub>4</sub>P","Li<sub>2</sub>SO<sub>4</sub>","OF<sub>2</sub>","KCaCl<sub>3</sub>","Ag<sub>2</sub>CrO<sub>4</sub>","AgBF<sub>4</sub>","H<sub>3</sub>NO<sub>3</sub>S","ZnBr<sub>2</sub>","Na<sub>2</sub>CO<sub>3</sub>","BaFe<sub>2</sub>O<sub>4</sub>","BrF<sub>5</sub>","CaCrO<sub>4</sub>","H<sub>2</sub>CO<sub>3</sub>","MgCO<sub>3</sub>","AgClO<sub>3</sub>","Ag<sub>3</sub>PO<sub>4</sub>","NaPO<sub>2</sub>H<sub>2</sub>","NaMnO<sub>4</sub>","Na<sub>2</sub>S<sub>2</sub>O<sub>8</sub>"); 

    // The following 3 arrays store all the compound names and formulas, along with the subscripted versions of all the formulas. 
    var MoleculesFormula = new Array("C15H20O4","C12H8","CH3CO2Na","C3H4O2","C60","C6H12O6","C5H9N1O4","C5H8O4","CN","H2O2","C13H18O2","C12H22O11","C14H14O3","C10H8","C14H18N2O5","C18H22O2","C3H3O3","C7H5N1O3S1","C5H6N2O2","C3H9N","C16H13Cl1N2O1","C19H16O4","C6H3N3O6","C8H8O3","C21H22N2O2","C6H14O6","C9H11N1O6","C10H20O1","C8N8O16","C6H6N12O12","C6H5NO2"); 
    var MoleculesName = new Array("Abscisic acid","Acenaphthylene","Sodium acetate","Acroleic acid","Buckminsterfullerene","Fructose","Glutamate","Glutaric acid","Hydrogen Cyanide","Hydrogen Peroxide","Ibuprofen","Beta-Lactose","Naproxen","Naphthalene","Aspartame","Estrone","Pyruvate","Saccharin","Thymine","Trimethylamine","Diazepam","Warfarin","Trinitrobenzene","Vanillin","Strychnine","Sorbitol","Showdomycin","Menthol","Octanitrocubane","Hexanitrohexaazaisowurtzitane","Nitrobenzene"); 
    var SubMoleculesArray = new Array("C<sub>1</sub><sub>5</sub>H<sub>2</sub><sub>0</sub>O<sub>4</sub>","C<sub>1</sub><sub>2</sub>H<sub>8</sub>","CH<sub>3</sub>CO<sub>2</sub>Na","C<sub>3</sub>H<sub>4</sub>O<sub>2</sub>","C<sub>6</sub><sub>0</sub>","C<sub>6</sub>H<sub>1</sub><sub>2</sub>O<sub>6</sub>","C<sub>5</sub>H<sub>9</sub>N<sub>1</sub>O<sub>4</sub>","C<sub>5</sub>H<sub>8</sub>O<sub>4</sub>","CN","H<sub>2</sub>O<sub>2</sub>","C<sub>1</sub><sub>3</sub>H<sub>1</sub><sub>8</sub>O<sub>2</sub>","C<sub>1</sub><sub>2</sub>H<sub>2</sub><sub>2</sub>O<sub>1</sub><sub>1</sub>","C<sub>1</sub><sub>4</sub>H<sub>1</sub><sub>4</sub>O<sub>3</sub>","C<sub>1</sub><sub>0</sub>H<sub>8</sub>","C<sub>1</sub><sub>4</sub>H<sub>1</sub><sub>8</sub>N<sub>2</sub>O<sub>5</sub>","C<sub>1</sub><sub>8</sub>H<sub>2</sub><sub>2</sub>O<sub>2</sub>","C<sub>3</sub>H<sub>3</sub>O<sub>3</sub>","C<sub>7</sub>H<sub>5</sub>N<sub>1</sub>O<sub>3</sub>S<sub>1</sub>","C<sub>5</sub>H<sub>6</sub>N<sub>2</sub>O<sub>2</sub>","C<sub>3</sub>H<sub>9</sub>N","C<sub>1</sub><sub>6</sub>H<sub>1</sub><sub>3</sub>Cl<sub>1</sub>N<sub>2</sub>O<sub>1</sub>","C<sub>1</sub><sub>9</sub>H<sub>1</sub><sub>6</sub>O<sub>4</sub>","C<sub>6</sub>H<sub>3</sub>N<sub>3</sub>O<sub>6</sub>","C<sub>8</sub>H<sub>8</sub>O<sub>3</sub>","C<sub>2</sub><sub>1</sub>H<sub>2</sub><sub>2</sub>N<sub>2</sub>O<sub>2</sub>","C<sub>6</sub>H<sub>1</sub><sub>4</sub>O<sub>6</sub>"," C<sub>9</sub>H<sub>1</sub><sub>1</sub>N<sub>1</sub>O<sub>6</sub>","C<sub>1</sub><sub>0</sub>H<sub>2</sub><sub>0</sub>O<sub>1</sub>","C<sub>8</sub>N<sub>8</sub>O<sub>1</sub><sub>6</sub>","C<sub>6</sub>H<sub>6</sub>N<sub>1</sub><sub>2</sub>O<sub>1</sub><sub>2</sub>","C<sub>6</sub>H<sub>5</sub>NO<sub>2</sub>"); 

    //The following part is the section where the specific Formula will be randomly selected for the questions. 

    var MCselection = Math.floor(Math.random()*2); 
    document.write(MCselection); 

    if(MCselection == 0) { 
    var Formula = CompoundsFormula; 
    var Name = CompoundsName; 
    var SubName = SubCompoundsArray; 
    var CoMo = 'Compound'; 
    var FoMo = 'Formula'; 
    } else { 
    var Formula = MoleculesFormula; 
    var Name = MoleculesName; 
    var SubName = SubMoleculesArray; 
    var CoMo = 'Molecule'; 
    var FoMo = 'Molecular'; 
    } 

    var FormNo = Math.floor(Math.random()*30); 
    var Form = Formula[FormNo]; 
    var FormName = Name[FormNo]; 
    var FormSub = SubName[FormNo]; 
    var ElementSub = new Array(); 
    var FoRAM = new Array(); 
    var ElemProduct = new Array(); 
// Note : This is the substring Syntax : ACTUAL_STRINGHERE.substr(start,length) 
// Note : is_numeric will return TRUE if the substring in question is a number. False Otherwise. 
    var l = 0; 
    var y = 0; 
// The following Code is going to strip away the elements and each corresponding number of moles 
// of each element per unit compound/molecule into separate arrays. 
while (l < Form.length)) { 
    if(Form.substr(l+1,1).toLowerCase()==Form.substr(l+1,1) || !isNaN(Form.substr(l+1,1))) { 
     if (!isNaN(Form.substr(l+1,1))) { 
      Element[y] = Form.substr(l,1); 
      if (!isNaN(Form.substr(l+2,1))) { 
        ElementSub[y] = Form.substr(l+1,2); 
        l++; 
        l++; 
       } else { 
        ElementSub[y] = Form.substr(l+1,1); 
        l++; 
     } 
    } else { 
      Element[y] = Form.substr(l,2); 

      if (!isNaN(Form.substr(l+2,1))) { 
       if (!isNaN(Form.substr(l+3,1))) { 
        ElementSub[y] = Form.substr(l+2,2); 
        l+=3; 
       } else { 
        ElementSub[y] = Form.substr(l+2,1); 
        l+=2; 
       } 
      } else { 
       ElementSub[y] = 1; 
       l++; 
      } 
     } 
    } else { 
     Element[y] = Form.substr(l,1); 
     ElementSub[y] = 1; 
    } 
    l++; 
    y++; 

    } 

    // this resets the value of $l to 0 so that it can be recycled for another while loop. 
    l = 0; 
    x = 0; 
    // The following Code Identifies The Different Elements Present in the Array. 
    while(x < Element.length) { 
    while(l < ElementsSym.length) { 
     if (ElementsSym[l]==Element[x]) { 
      FoRAM[x] = ElementsRAM[l]; 
     } 
     l++; 
    } 
     l = 0; 
     x++; 
    } 

    // this also resets the value of $l to 0 so that it can be recycled for another while loop. 

    l = 0; 
    x = 0; 

    // This find the product of each element multiplied by the number of moles present per mole of the formula. 

    while(l<Element.length) { 
    ElemProduct[l] = FoRAM[l]*ElementSub[l]; 
//  echo '<br>'; 
     l++; 
    } 
    // This finds the total of all the molar elemental products b adding up the values in an array. 
     //x = array_sum(ElemProduct); 

var n = 0; 
var sum = 0; 

while(n<ElemProduct.length) { 
    sum += ElemProduct[n]; 
    n++; 
    } 


document.write("The " + CoMo + " : " + SubName[FormNo] + " Has a Relative " + FoMo + " Mass of : " + sum); 

    // The following Line Presents the Information. 

    </script> 

    <script type="text/javascript"> 
    </script> 
    <br><hr> 
</div> 
<div id="input"> 

    <form id="useranswer"> 
     Enter Your Answer Here<br> 
     <input type="text" id="useranswer"><br> 
     <input type="submit" id="usersubmit" value="Check Answer"> 

    </form> 

    <div id="helpsection"> 
    <hr><br> 
    Not sure what Relative Formula mass is? <br> 
    <a>Click Here.</a> <br> 
    Not sure what Relative Molecular mass is?<br> 
    <a>Click Here.</a> 
    <br> 
    <hr> 
    <br> 
    Haven't learned how to work this out yet? <br> 
    <a>Click Here.</a> 
    </div> 
</div> 
</div> 
<hr> 
<span id="copyright"> Copyright A Level Scientist 2014 | All rights reserved. <span> 
</div> 

</section> 
</body> 
</html> 

當我運行眼前這個代碼,結果是這樣的:

http://postimg.org/image/6993cuaqb/

有人可以請我解釋一下這段代碼有什麼問題!謝謝:)

---編輯/

這是我的新的腳本,它樣的工作,但沒有。如果有人願意進行測試,您可以瞭解我在說什麼(NAN,Long decimals)。當你測試它時,多次刷新並看看會發生什麼。

新的腳本:

<html> 
<head></head> 
<body> 
<script> 

    //The following 3 Arrays store 3 things. 1) The Element names. 2) The element Symbols. 3) The Relative Atomic Masses of the Elements. 
    var Elements = new Array("Hydrogen","Lithium","Sodium","Potassium","Rubidium","Caesium","Francium","Beryllium","Magnesium","Calcium","Strontium","Barium","Radium","Scandium","Yttrium","Lanthanum","Actinium","Titanium","Zirconium","Halfnium","Rutherfordium","Vanadium","Niobium","Tantalum","Dubnium","Chromium","Molybdenum","Tungsten","Seaborgium","Manganese","Technetium","Rhenium","Bohrium","Iron","Ruthenium","Osmium","Hassium","Cobalt","Rhodium","Iridium","Meitnerium","Nickel","Palladium","Platinum","Darmstadtium","Copper","Silver","Gold","Roentgenium","Zinc","Cadmium","Mercury","Boron","Aluminum","Gallium","Indium","Thallium","Carbon","Silicon","Germanium","Tin","Lead","Nitrogen","Phosphorus","Arsenic","Antimony","Bismuth","Oxygen","Sulfur","Selenium","Tellurium","Polonium","Flourine","Chlorine","Bromine","Iodine","Astatine","Helium","Neon","Argon","Krypton","Xenon","Radon"); 
    var ElementsSym = new Array("H","Li","Na","K","Rb","Cs","Fr","Be","Mg","Ca","Sr","Ba","Ra","Sc","Y","La","Ac","Ti","Zr","Hf","Rf","V","Nb","Ta","Db","Cr","Mo","W","Sg","Mn","Tc","Re","Bh","Fe","Ru","Os","Hs","Co","Rh","Ir","Mt","Ni","Pd","Pt","Ds","Cu","Ag","Au","Rg","Zn","Cd","Hg","B","Al","Ga","In","Tl","C","Si","Ge","Sn","Pb","N","P","As","Sb","Bi","O","S","Se","Te","Po","F","Cl","Br","I","At","He","Ne","Ar","Kr","Xe","Rn"); 
    var ElementsRAM = new Array(1.0,6.9,23.0,39.1,85.5,132.9,223,9.0,24.3,40.1,87.6,137.3,226,45.0,88.9,138.9,227,47.9,91.2,178.5,261,50.9,92.9,180.9,262,52.0,95.9,183.8,266,54.9,98,186.2,264,55.8,101.1,190.2,277,58.9,102.9,192.2,268,58.7,106.4,195.1,271,63.5,107.9,197.0,272,65.4,112.4,200.6,10.8,27.0,69.7,114.8,204.4,12.0,28.1,72.6,18.7,207.2,14.0,31.0,74.9,121.8,209.0,16.0,32.1,79.0,127.6,209,19.0,35.5,79.9,126.9,210,4.0,20.2,39.9,83.8,131.3,222); 
    // The following 3 arrays store all of the molecule names and formulas, along with the subscripted versions of all of the formulas. 
    var CompoundsFormula = new Array("Al2O3","NH4N3","NH4ClO3","NH4ClO4","BaCrO4","BeCO3","C6H12N2O4Pt","CrO2F2","C3Cl3N3","GaP","LiCoO2","FeLiO4P","Li2SO4","OF2","KCaCl3","Ag2CrO4","AgBF4","H3NO3S","ZnBr2","Na2CO3","BaFe2O4","BrF5","CaCrO4","H2CO3","MgCO3","AgClO3","Ag3PO4","NaPO2H2","NaMnO4","Na2S2O8"); 
    var CompoundsName = new Array("Aluminium oxide","Ammonium azide","Ammonium chlorate","Ammonium perchlorate","Barium chromate","Beryllium carbonate","Carboplatin","Chromyl fluoride","Cyanuric chloride","Gallium phosphide","Lithium cobalt oxide","Lithium iron phosphate","Lithium sulfate","Oxygen difluoride","Potassium calcium chloride","Silver chromate","Silver fluoroborate","Sulfamic acid","Zinc bromide","Sodium carbonate","Barium ferrite","Bromine pentafluoride","Calcium chromate","Carbonic acid","Magnesium carbonate","Silver chlorate","Silver orthophosphate","Sodium hypophosphite","Sodium permanganate","Sodium persulfate"); 
    var SubCompoundsArray = new Array("Al<sub>2</sub>O<sub>3</sub>","NH<sub>4</sub>N<sub>3</sub>","NH<sub>4</sub>ClO<sub>3</sub>","NH<sub>4</sub>ClO<sub>4</sub>","BaCrO<sub>4</sub>","BeCO<sub>3</sub>","C<sub>6</sub>H<sub>1</sub><sub>2</sub>N<sub>2</sub>O<sub>4</sub>Pt","CrO<sub>2</sub>F<sub>2</sub>","C<sub>3</sub>Cl<sub>3</sub>N<sub>3</sub>","GaP","LiCoO<sub>2</sub>","FeLiO<sub>4</sub>P","Li<sub>2</sub>SO<sub>4</sub>","OF<sub>2</sub>","KCaCl<sub>3</sub>","Ag<sub>2</sub>CrO<sub>4</sub>","AgBF<sub>4</sub>","H<sub>3</sub>NO<sub>3</sub>S","ZnBr<sub>2</sub>","Na<sub>2</sub>CO<sub>3</sub>","BaFe<sub>2</sub>O<sub>4</sub>","BrF<sub>5</sub>","CaCrO<sub>4</sub>","H<sub>2</sub>CO<sub>3</sub>","MgCO<sub>3</sub>","AgClO<sub>3</sub>","Ag<sub>3</sub>PO<sub>4</sub>","NaPO<sub>2</sub>H<sub>2</sub>","NaMnO<sub>4</sub>","Na<sub>2</sub>S<sub>2</sub>O<sub>8</sub>"); 

    // The following 3 arrays store all the compound names and formulas, along with the subscripted versions of all the formulas. 
    var MoleculesFormula = new Array("C15H20O4","C12H8","CH3CO2Na","C3H4O2","C60","C6H12O6","C5H9N1O4","C5H8O4","CN","H2O2","C13H18O2","C12H22O11","C14H14O3","C10H8","C14H18N2O5","C18H22O2","C3H3O3","C7H5N1O3S1","C5H6N2O2","C3H9N","C16H13Cl1N2O1","C19H16O4","C6H3N3O6","C8H8O3","C21H22N2O2","C6H14O6","C9H11N1O6","C10H20O1","C8N8O16","C6H6N12O12","C6H5NO2"); 
    var MoleculesName = new Array("Abscisic acid","Acenaphthylene","Sodium acetate","Acroleic acid","Buckminsterfullerene","Fructose","Glutamate","Glutaric acid","Hydrogen Cyanide","Hydrogen Peroxide","Ibuprofen","Beta-Lactose","Naproxen","Naphthalene","Aspartame","Estrone","Pyruvate","Saccharin","Thymine","Trimethylamine","Diazepam","Warfarin","Trinitrobenzene","Vanillin","Strychnine","Sorbitol","Showdomycin","Menthol","Octanitrocubane","Hexanitrohexaazaisowurtzitane","Nitrobenzene"); 
    var SubMoleculesArray = new Array("C<sub>1</sub><sub>5</sub>H<sub>2</sub><sub>0</sub>O<sub>4</sub>","C<sub>1</sub><sub>2</sub>H<sub>8</sub>","CH<sub>3</sub>CO<sub>2</sub>Na","C<sub>3</sub>H<sub>4</sub>O<sub>2</sub>","C<sub>6</sub><sub>0</sub>","C<sub>6</sub>H<sub>1</sub><sub>2</sub>O<sub>6</sub>","C<sub>5</sub>H<sub>9</sub>N<sub>1</sub>O<sub>4</sub>","C<sub>5</sub>H<sub>8</sub>O<sub>4</sub>","CN","H<sub>2</sub>O<sub>2</sub>","C<sub>1</sub><sub>3</sub>H<sub>1</sub><sub>8</sub>O<sub>2</sub>","C<sub>1</sub><sub>2</sub>H<sub>2</sub><sub>2</sub>O<sub>1</sub><sub>1</sub>","C<sub>1</sub><sub>4</sub>H<sub>1</sub><sub>4</sub>O<sub>3</sub>","C<sub>1</sub><sub>0</sub>H<sub>8</sub>","C<sub>1</sub><sub>4</sub>H<sub>1</sub><sub>8</sub>N<sub>2</sub>O<sub>5</sub>","C<sub>1</sub><sub>8</sub>H<sub>2</sub><sub>2</sub>O<sub>2</sub>","C<sub>3</sub>H<sub>3</sub>O<sub>3</sub>","C<sub>7</sub>H<sub>5</sub>N<sub>1</sub>O<sub>3</sub>S<sub>1</sub>","C<sub>5</sub>H<sub>6</sub>N<sub>2</sub>O<sub>2</sub>","C<sub>3</sub>H<sub>9</sub>N","C<sub>1</sub><sub>6</sub>H<sub>1</sub><sub>3</sub>Cl<sub>1</sub>N<sub>2</sub>O<sub>1</sub>","C<sub>1</sub><sub>9</sub>H<sub>1</sub><sub>6</sub>O<sub>4</sub>","C<sub>6</sub>H<sub>3</sub>N<sub>3</sub>O<sub>6</sub>","C<sub>8</sub>H<sub>8</sub>O<sub>3</sub>","C<sub>2</sub><sub>1</sub>H<sub>2</sub><sub>2</sub>N<sub>2</sub>O<sub>2</sub>","C<sub>6</sub>H<sub>1</sub><sub>4</sub>O<sub>6</sub>"," C<sub>9</sub>H<sub>1</sub><sub>1</sub>N<sub>1</sub>O<sub>6</sub>","C<sub>1</sub><sub>0</sub>H<sub>2</sub><sub>0</sub>O<sub>1</sub>","C<sub>8</sub>N<sub>8</sub>O<sub>1</sub><sub>6</sub>","C<sub>6</sub>H<sub>6</sub>N<sub>1</sub><sub>2</sub>O<sub>1</sub><sub>2</sub>","C<sub>6</sub>H<sub>5</sub>NO<sub>2</sub>"); 

    //The following part is the section where the specific Formula will be randomly selected for the questions. 

    var MCselection = Math.floor(Math.random()*2); 

    if(MCselection === 0) { 
    var Formula = CompoundsFormula; 
    var Name = CompoundsName; 
    var SubName = SubCompoundsArray; 
    var CoMo = 'Compound'; 
    var FoMo = 'Formula'; 
    } else { 
    var Formula = MoleculesFormula; 
    var Name = MoleculesName; 
    var SubName = SubMoleculesArray; 
    var CoMo = 'Molecule'; 
    var FoMo = 'Molecular'; 
    } 


    var FormNo = Math.floor(Math.random()*30); 
    var Form = Formula[FormNo]; 
    var FormName = Name[FormNo]; 
    var FormSub = SubName[FormNo]; 
    var ElementSub = new Array(); 
    var FoRAM = new Array(); 
    var ElemProduct = new Array(); 
    var Element = new Array(); 
// Note : This is the substring Syntax : ACTUAL_STRINGHERE.substr(start,length) 
// Note : is_numeric will return TRUE if the substring in question is a number. False Otherwise. 
    var l = 0; 
    var y = 0; 

// The following Code is going to strip away the elements and each corresponding number of moles 
// of each element per unit compound/molecule into separate arrays. 
while (l <= Form.length) { 
    if((Form.substr(l+1,1).toLowerCase()==Form.substr(l+1,1)) || (!isNaN(Form.substr(l+1,1)))) { 
     if (!isNaN(Form.substr(l+1,1))) { 
      Element[y] = Form.substr(l,1); 
      if (!isNaN(Form.substr(l+2,1))) { 
        ElementSub[y] = Form.substr(l+1,2); 
        l++; 
        l++; 
       } else { 
        ElementSub[y] = Form.substr(l+1,1); 
        l++; 
     } 
    } else { 
      Element[y] = Form.substr(l,2); 
      if (!isNaN(Form.substr(l+2,1))) { 
       if (!isNaN(Form.substr(l+3,1))) { 
        ElementSub[y] = Form.substr(l+2,2); 
        l+=3; 
       } else { 
        ElementSub[y] = Form.substr(l+2,1); 
        l+=2; 
       } 
      } else { 
       ElementSub[y] = 1; 
       l++; 
      } 
     } 
    } else { 
     Element[y] = Form.substr(l,1); 

     ElementSub[y] = 1; 
    } 
    l++; 
    y++; 

    } 

    document.write(Element); 
    // this resets the value of $l to 0 so that it can be recycled for another while loop. 
    l = 0; 
    x = 0; 
    // The following Code Identifies The Different Elements Present in the Array. 
    while(x < Element.length) { 
    while(l < ElementsSym.length) { 
     if (ElementsSym[l]==Element[x]) { 
      FoRAM[x] = ElementsRAM[l]; 
     } 
     l++; 
    } 
     l = 0; 
     x++; 
    } 

    // this also resets the value of $l to 0 so that it can be recycled for another while loop. 

    l = 0; 
    x = 0; 

    // This find the product of each element multiplied by the number of moles present per mole of the formula. 

    while(l<Element.length) { 
    ElemProduct[l] = FoRAM[l]*ElementSub[l]; 
//  echo '<br>'; 
     l++; 
    } 
    // This finds the total of all the molar elemental products b adding up the values in an array. 
     //x = array_sum(ElemProduct); 

var n = 0; 
var sum = 0; 

while(n<ElemProduct.length) { 
    sum += ElemProduct[n]; 
    n++; 
    } 

document.write("<br> The " + CoMo + " : " + SubName[FormNo] + " Has a Relative " + FoMo + " Mass of : " + sum); 

    // The following Line Presents the Information. 
    </script> 
</body> 

    </html> 

可有人請點我在正確的方向,爲什麼發生這種情況。謝謝。

+0

你的JS看起來有點bug,在本地範圍和各種啓動的變量。你可以確認JS作爲一個靜態HTML頁面的一部分,而不是從PHP中運行時正確運行? – Sam

+0

你檢查控制檯是否有錯誤? –

+0

PHP與此無關。如果在將它從靜態HTML代碼轉換爲由PHP_動態輸出的HTML代碼時將其搞亂了 - 那麼比較兩個版本的HTML代碼/輸出並找出差異。 – CBroe

回答

0

您的JS代碼有錯誤。在第125行,你也有一個括號「while(l < Form.length)){」。修復它通過刪除一個,並嘗試代碼

+0

謝謝!修復。 – Tyoms

+0

我不明白爲什麼我的程序會產生巨大的小數。 – Tyoms

+0

嘗試在逗號後面設置小數位。即document.write(「
」+ CoMo +「:」+ SubName [FormNo] +「Has a Relative」+ FoMo +「Mass of:」+ parseFloat(sum).toFixed(any_number_you_want)); – cr450v3rr1d3

相關問題