2014-11-21 51 views
0

所以我很高兴我从以前的问题中获得了这张表的帮助;但我遇到了另一个问题。单词生成器适用于该工具的方式是使用math.random从1-9生成一个随机数,然后用math.floor将其舍入。然后,这个代码的function changeRandom()方面抓住这个数字,并根据该数字将数字改为一个随机单词,这样如果生成的随机数是3,它就会变成Word 3.现在,我试着去做答案是一样的,但似乎没有改变。只有调试控制台给我的错误是gen_nums没有为getRandomAnswer()定义,但是当我删除它时,处理+需要更长的时间,或者有关于最大文件空间的错误或其他信息。我需要对单词选择器功能做类似的处理以适用于9个框。这是代码。div innerHTML重写失败

注意:代码中的注释/注释可以忽略,因为它们仅供参考。

HTML

<div id="randomizer"> 
    <div id="wordOutput"> 
     <div id="output"> 
     <p id="outputNumber"></p> 
     </div> 
     <div id="button"> 
     <!-- This is the button that calls the getRandom() function to create the word. --><button id="myBtn">Randomize!</button><br> 
     <caption>Click this button to generate a random word! 
     </caption> 
     <!-- This is apart of the Randomizer tool, which can be changed to fit the words. It will output the answers based on --> 
     <div id="answers"> 
      <div class="output1" id="output1"></div> 
      <div class="output2" id="output2"></div> 
      <div class="output3" id="output3"></div><br> 
      <div class="output4" id="output4"></div> 
      <div class="output5" id="output5"></div> 
      <div class="output6" id="output6"></div><br> 
      <div class="output7" id="output7"></div> 
      <div class="output8" id="output8"></div> 
      <div class="output9" id="output9"></div> 
     </div> 
     </div> 

的JavaScript

/*function playSound() { 

} 
*/ 
/* This section is for the first tool; the Randomizer.*/ 
/* This is the new function for getting a random number; also used in the getRandomAnswer() function. To select different numbers, adjust the range. */ 
function getRandom() { 
    var nums = [1,2,3,4,5,6,7,8,9]; 
    var gen_nums = []; 

    function in_array(array, el) { 
    for(var i = 0 ; i < array.length; i++) 
     if(array[i] == el) return true; 
    return false; 
    } 

    function get_rand(array) { 
    var rand = array[Math.floor(Math.random()*array.length)]; 
    if(!in_array(gen_nums, rand)) { 
     gen_nums.push(rand); 
     return rand; 
    } 
    return get_rand(array); 
    } 

    for(var i = 0; i < 9; i++) { 
    return (get_rand(nums)); 
    } 
} 
function timeOut(){ 
    /* This can be ignored, as it was a testing function for creating, making, and fixing the randomizer tool, but may change based on creating new tools that need to be troubleshooted. */ 
    setTimeout (changeRandom, 1); 
} 
/* This is the function that grabs the innerHTML (what the box says) of the #output1 and changes that number to the word specified. [In example, if the number [randomly] generated was 5, then this function detects that the number was 5 and changes it to Word 5.] */ 
function changeRandom() { 
    /* Using the "var x" command, this tells the function that whenever there's an x in the code, it will read it as "document.getElementById('output1').innerHTML", and will grab whatever is inside that #output1 element. */ 
    var x = document.getElementById('outputNumber').innerHTML 
    /* Here's the tutorial for modifying this tool for the possible answers. This tool [by default] has it set to */ 
    if(x == 1) { 
    document.getElementById("outputNumber").innerHTML = "Word 1"; 
    } else if(x == 2) { 
    document.getElementById("outputNumber").innerHTML = "Word 2"; 
    } else if(x == 3) { 
    document.getElementById("outputNumber").innerHTML = "Word 3"; 
    } else if(x == 4) { 
    document.getElementById("outputNumber").innerHTML = "Word 4" 
    } else if(x == 5) { 
    document.getElementById("outputNumber").innerHTML = "Word 5" 
    } 
    else if(x == 6) { 
    document.getElementById("outputNumber").innerHTML = "Word 6" 
    } 
    else if(x == 7) { 
    document.getElementById("outputNumber").innerHTML = "Word 7" 
    } 
    else if(x == 8) { 
    document.getElementById("outputNumber").innerHTML = "Word 8" 
    } 
    else if(x == 9) { 
    document.getElementById("outputNumber").innerHTML = "Word 9" 
    } 
    else if(x == 10) { 
    document.getElementById("outputNumber").innerHTML = "Word 10" 
    } 
    /* If the function is broken [by user modifications], it will output "Error :c", which means the user should check their modifications. */ 
    else { 
    document.getElementById("output1").innerHTML = "Error :c" 
    } 
} 
document.getElementById("myBtn").addEventListener("click", function(){document.getElementById("outputNumber").innerHTML = getRandom(); changeRandom();}) 
function testFunction() { 
    if(document.getElementById("output1").innerHTML == 3) { 
    document.getElementById("output1").innerHTML = "Three"; 
    } 
    else { 
    document.getElementById("output1").innerHTML = "N3" 
    } 
} 
function getRandomAnswer() { 
    var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
    var copy = nums.slice(); 

    for (var i = 0, len = nums.length; i < len; i++) { 
    var j = Math.floor(Math.random() * copy.length); 
    var rand = copy[ j ]; 

    // remove from array 
    copy.splice(j, 1); 

    // add to output 
    document.getElementById('output' + (i + 1)).innerHTML = rand; 
    } 
    return gen_nums; 
    document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer(); 
} 
function changeRandomAnswer() { 
    /* Using the "var x" command, this tells the function that whenever there's an x in the code, it will read it as "document.getElementById('output1').innerHTML", and will grab whatever is inside that #output1 element. */ 
    /* Here's the tutorial for modifying this tool for the possible answers. This tool [by default] has it set to */ 
    if(document.getElementById("output1").innerHTML = "1") { 
    document.getElementById("output1").innerHTML = "Answer 1"; 
    } 
    else if(document.getElementById("output1").innerHTML = "2") { 
    document.getElementById("output2").innerHTML = "Answer 2"; 
    } 
    else if(document.getElementById("output1").innerHTML = "3") { 
    document.getElementById("output3").innerHTML = "Answer 3"; 
    } 
    else if(document.getElementById("output1").innerHTML = "4") { 
    document.getElementById("output4").innerHTML = "Answer 4" 
    } 
    else if(document.getElementById("output1").innerHTML = "5") { 
    document.getElementById("output5").innerHTML = "Answer 5" 
    } 
    else if(document.getElementById("output1").innerHTML = "6") { 
    document.getElementById("output6").innerHTML = "Answer 6" 
    } 
    else if(document.getElementById("output1").innerHTML = "7") { 
    document.getElementById("output7").innerHTML = "Answer 7" 
    } 
    else if(document.getElementById("output1").innerHTML = "8") { 
    document.getElementById("output8").innerHTML = "Answer 8" 
    } 
    else if(document.getElementById("output1").innerHTML = "9") { 
    document.getElementById("output9").innerHTML = "Answer 9" 
    } 
    /* If the function is broken [by user modifications], it will output "Error :c", which means the user should check their modifications. */ 
    else { 
    document.getElementById("output1").innerHTML = "Error :c" 
    } 
} 
document.getElementById("myBtn").addEventListener("click", function(){document.getElementById("answers").innerHTML = getRandomAnswer(); changeRandomAnswer();}) 

这里是一个可行的codepen的工作。

回答

0

您提供的代码有几件事是不必要的。

  • 就上线,你不需要输出从getRandomAnswer()分配给“答案”的innerHTML。该函数本身已经在执行将随机数分配给每个输出的操作。你可以只是简单地调用该函数:
    document.getElementById("myBtn").addEventListener("click", function(){ getRandomAnswer(); changeRandomAnswer();})

  • getRandomAnswer()的功能,你可以删除return gen_numbers;,你可以删除行:
    document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer();
    这导致了递归和完全因为这些数字在之前的for循环中得到了应用,所以没有必要。


功能changeRandomAnswer()有需要解决的一些问题:

  • 首先要定义,而不是每个if条件中比较的innerHTML的内容,通过使用一个等号(=)而不是两个(==)。
  • 你设置逻辑的方式,它只适用于它匹配的第一个盒子,并跳过其余部分。您也一直在检查“output1”的innerHTML值。

一种解决方案是遍历每个元素和运行的每个元素在同一条件检查:

function changeRandomAnswer() { 

    var answers = document.getElementById("answers"); 

    for(var i = 0, len = answers.children.length; i < len; i++) { 
    var output = answers.children[i]; 
    var answer = output.innerHTML; 

    if(answer == "1") { 
     output.innerHTML = "Answer 1"; 
    } 
    else if(answer == "2") { 
     output.innerHTML = "Answer 2"; 
    } 
    else if(answer == "3") { 
     output.innerHTML = "Answer 3"; 
    } 
    else if(answer == "4") { 
     output.innerHTML = "Answer 4" 
    } 
    else if(answer == "5") { 
     output.innerHTML = "Answer 5" 
    } 
    else if(answer == "6") { 
     output.innerHTML = "Answer 6" 
    } 
    else if(answer == "7") { 
     output.innerHTML = "Answer 7" 
    } 
    else if(answer == "8") { 
     output.innerHTML = "Answer 8" 
    } 
    else if(answer == "9") { 
     output.innerHTML = "Answer 9" 
    } 
    else { 
     output.innerHTML = "Error :c" 
    } 
    } 
} 


以下是完整的JavaScript和提到的所有编辑(另外我在分叉一个工作版本codepen here):

/*function playSound() { 

} 
*/ 
/* This section is for the first tool; the Randomizer.*/ 
/* This is the new function for getting a random number; also used in the getRandomAnswer() function. To select different numbers, adjust the range. */ 
function getRandom() { 
    var nums = [1,2,3,4,5,6,7,8,9]; 
    var gen_nums = []; 

    function in_array(array, el) { 
    for(var i = 0 ; i < array.length; i++) 
     if(array[i] == el) return true; 
    return false; 
    } 

    function get_rand(array) { 
    var rand = array[Math.floor(Math.random()*array.length)]; 
    if(!in_array(gen_nums, rand)) { 
     gen_nums.push(rand); 
     return rand; 
    } 
    return get_rand(array); 
    } 

    for(var i = 0; i < 9; i++) { 
    return (get_rand(nums)); 
    } 
} 
function timeOut(){ 
    /* This can be ignored, as it was a testing function for creating, making, and fixing the randomizer tool, but may change based on creating new tools that need to be troubleshooted. */ 
    setTimeout (changeRandom, 1); 
} 
/* This is the function that grabs the innerHTML (what the box says) of the #output1 and changes that number to the word specified. [In example, if the number [randomly] generated was 5, then this function detects that the number was 5 and changes it to Word 5.] */ 
function changeRandom() { 
    /* Using the "var x" command, this tells the function that whenever there's an x in the code, it will read it as "document.getElementById('output1').innerHTML", and will grab whatever is inside that #output1 element. */ 
    var x = document.getElementById('outputNumber').innerHTML 
    /* Here's the tutorial for modifying this tool for the possible answers. This tool [by default] has it set to */ 
    if(x == 1) { 
    document.getElementById("outputNumber").innerHTML = "Word 1"; 
    } else if(x == 2) { 
    document.getElementById("outputNumber").innerHTML = "Word 2"; 
    } else if(x == 3) { 
    document.getElementById("outputNumber").innerHTML = "Word 3"; 
    } else if(x == 4) { 
    document.getElementById("outputNumber").innerHTML = "Word 4" 
    } else if(x == 5) { 
    document.getElementById("outputNumber").innerHTML = "Word 5" 
    } 
    else if(x == 6) { 
    document.getElementById("outputNumber").innerHTML = "Word 6" 
    } 
    else if(x == 7) { 
    document.getElementById("outputNumber").innerHTML = "Word 7" 
    } 
    else if(x == 8) { 
    document.getElementById("outputNumber").innerHTML = "Word 8" 
    } 
    else if(x == 9) { 
    document.getElementById("outputNumber").innerHTML = "Word 9" 
    } 
    else if(x == 10) { 
    document.getElementById("outputNumber").innerHTML = "Word 10" 
    } 
    /* If the function is broken [by user modifications], it will output "Error :c", which means the user should check their modifications. */ 
    else { 
    document.getElementById("output1").innerHTML = "Error :c" 
    } 
} 
document.getElementById("myBtn").addEventListener("click", function(){document.getElementById("outputNumber").innerHTML = getRandom(); changeRandom();}) 
function testFunction() { 
    if(document.getElementById("output1").innerHTML == 3) { 
    document.getElementById("output1").innerHTML = "Three"; 
    } 
    else { 
    document.getElementById("output1").innerHTML = "N3" 
    } 
} 
function getRandomAnswer() { 
    var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
    var copy = nums.slice(); 

    for (var i = 0, len = nums.length; i < len; i++) { 
    var j = Math.floor(Math.random() * copy.length); 
    var rand = copy[ j ]; 

    // remove from array 
    copy.splice(j, 1); 

    // add to output 
    document.getElementById('output' + (i + 1)).innerHTML = rand; 
    } 
    //return gen_nums; 
    //document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer(); 
} 
function changeRandomAnswer() { 

    var answers = document.getElementById("answers"); 

    for(var i = 0, len = answers.children.length; i < len; i++) { 
    var output = answers.children[i]; 
    var answer = output.innerHTML; 

    if(answer == "1") { 
     output.innerHTML = "Answer 1"; 
    } 
    else if(answer == "2") { 
     output.innerHTML = "Answer 2"; 
    } 
    else if(answer == "3") { 
     output.innerHTML = "Answer 3"; 
    } 
    else if(answer == "4") { 
     output.innerHTML = "Answer 4" 
    } 
    else if(answer == "5") { 
     output.innerHTML = "Answer 5" 
    } 
    else if(answer == "6") { 
     output.innerHTML = "Answer 6" 
    } 
    else if(answer == "7") { 
     output.innerHTML = "Answer 7" 
    } 
    else if(answer == "8") { 
     output.innerHTML = "Answer 8" 
    } 
    else if(answer == "9") { 
     output.innerHTML = "Answer 9" 
    } 
    else { 
     output.innerHTML = "Error :c" 
    } 
    } 

} 
document.getElementById("myBtn").addEventListener("click", function(){ getRandomAnswer(); changeRandomAnswer();}) 
+0

谢谢,我会为codepen使用投票,但..代表不是15:P – 2014-11-21 20:35:19

1

你有一些作用域问题和递归问题。为了简化:

function foo() { 
    var gen_nums = []; 

    for (var i=0; i<10; i++) { 
    gen_nums.push(i) 
    } 

    return gen_nums; 
} 

function bar() { 
    var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
    var copy = nums.slice(); 

    return gen_nums; 
    bar() 
} 

console.log(foo()) // [0, ..., 9] 
console.log(bar()) // error - gen_nums is not defined 

之所以从getRandomAnswer()删除它使事情需要更长的时间,是因为它的存在,该功能将停止在该行,并没有达到下一行,这是

document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer();

它使函数递归。

+0

速度是100%罚款的东西,它只是它实际上并没有重写块div,@elzi数字。我提到gen_nums是因为我认为这与重写有冲突。 – 2014-11-21 18:15:20

+0

我只是告诉你你的JavaScript在做什么。当你试图返回一个未定义的变量时,这并不是100%。每次都会出错。 – elzi 2014-11-21 18:20:29

0

你getRandomAnswer功能是

function getRandomAnswer() { 
    var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
    var copy = nums.slice(); 

    for (var i = 0, len = nums.length; i < len; i++) { 
    var j = Math.floor(Math.random() * copy.length); 
    var rand = copy[ j ]; 

    // remove from array 
    copy.splice(j, 1); 

    // add to output 
    document.getElementById('output' + (i + 1)).innerHTML = rand; 
    } 
    return gen_nums; 
    document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer(); 
} 

在这部分代码被执行之前要返回gen_nums。所以写像,

function getRandomAnswer() { 
    var nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; 
    var copy = nums.slice(); 

    for (var i = 0, len = nums.length; i < len; i++) { 
    var j = Math.floor(Math.random() * copy.length); 
    var rand = copy[ j ]; 

    // remove from array 
    copy.splice(j, 1); 

    // add to output 
    document.getElementById('output' + (i + 1)).innerHTML = rand; 
    } 
    document.getElementById('output' + (i + 1)).innerHTML = getRandomAnswer(); 
    return gen_nums; 

} 

here是的jsfiddle你的工作代码。

+0

速度是100%的事情,它只是它并没有重写块div中的数字@WisdmLabs。我提到gen_nums是因为我认为这与重写有冲突。 – 2014-11-21 18:14:48