2012-08-14 98 views
0

我有一个.js文件下面的代码:为什么JavaScript不能在浏览器中工作?

$.extend(KhanUtil, { 
    // takes a number and returns the sign of that number 
    steveSign: function(num){ 
     num = parseFloat(num) 
     if (num>=0){return 1} 
     else{return -1} 
    }, 

    // takes a function, a lower bound for a zero,an upper bound for a zero, and locates 
    // that zero by iteratively halving the interval. 
steveRoot: function(f,xmin,xmax){ 
    var l = xmin 
    var r = xmax 
    var z = 0 
    for (i=0;i<6;i++){ 
     z = (l + r)/2 
     if (KhanUtil.steveSign(f(l)) === KhanUtil.steveSign(f(z))){ l = z} 
     else{r = z}  
    } 
    return z 
}, 

}); 

在我的HTML文件中,我定义var f = function(x){return x**2 - 2}和运行steveRoot(f,1,2),但我的浏览器胡扯了。这是为什么发生?

编辑:

我张贴我的代码全部,因为它是在意见中的要求。感谢一群试图帮助我的人。奇怪的是,代码运行正常的十次中有9次。它只是偶尔会“cra出”。这里有很多随机变量,但我无法想象为什么steveRoot会关心获得稍微不同的功能。代码完全正常工作,当我不包含调用steveRoot的变量时,从未出现过问题。

的HTML文件:

<!DOCTYPE html> 
<html data-require="math graphie graphie-helpers play polynomials steveMath"> 
    <head> 
     <title>Piecewise-defined function</title> 
     <script src="../khan-exercise.js"></script> 
    </head> 
    <body> 
     <div class="exercise"> 
      <div class="vars"> 


    <var id = "n">randRange(2,4)</var> 
    <var id = "abscissas">makeXList()</var> 
    <var id = "ordinates">makeYList(-9,9,abscissas.length)</var> 
    <var id = "points">makeCoordinates(abscissas,ordinates)</var> 
    <var id = "f">(function(x){return niceFunction(x,points)})</var> 
    <var id = zeros>steveRoot(f,-10,10)</var> 





      </div> 

      <div class="problems"> 
       <div id="problem-type-or-description"> 
        <p class="problem">You are going to have to answer 5</p> 
        <p class="question">Answer 5</p> 
        <div class="graphie" id="grid"> 
       graphInit({ 
        range: 10, 
        scale: 20, 
        tickStep: 1, 
        axisArrows: "<->" 
       }); 

      a =style({ 
         stroke: "red", 
         strokeWidth: 2 
        }, function() { 
         plot(function(x) { return niceFunction(x,points); 
         }, [ -10, 10 ]); 
        });; 
      a.plot(); 
      </div> 

        <p class="solution">5</p> 
       </div> 



      </div> 

      <div class="hints"> 
       <!-- Any hints to show to the student. --> 
      </div> 
     </div> 
    </body> 

的js文件:

$.extend(KhanUtil, { 

//randomLines is a piecewise linear function in x, where the partition points are given by list_of_points. 
//list_of_points is an array of arrays, for example [[1,5],[2,-1],[3,4]] would indicate the points (1,5), (2,-1), and (3,4) 
//are on the curve. The points must be arranged in order of increasing abscissa. 


randomLines: function(x,list_of_points) 
{ 

    for (i=0;i<list_of_points.length-1;i++) 
    { 
     var x_1 = list_of_points[i][0] 
     var y_1 = list_of_points[i][1] 
     var x_2 = list_of_points[i+1][0] 
     var y_2 = list_of_points[i+1][1] 
     var m = (y_2-y_1)/(x_2-x_1) 
     var y = m*(x - x_1) + y_1 

     if (x > x_1 && x <= x_2){return y} 
    } 
    if (x<=list_of_points[0][0]){return 0} 
    if (x>list_of_points[list_of_points.length-1][0]){return 0} 
}, 

//randomLinesFunc: function(list_of_points){ 
// var f = function(x){ 
// return randomLines(x,list_of_points) 
// } 
// 
// return f 
//}, 

numInt: function(f,x){ 
    var delta = .01 
    var sum = 0 
    var i = 0 
    while ((delta*i-10)<=x) 

     {sum = sum+delta*f(-10+i*delta) 
     i++ 
     } 
    return sum 
    }, 

peace: function(x){return 2}, 

//////////////////////////////////////////////// 

//randRangeNZCU takes (min,max,n) and returns an array of nonzero numbers between max and min 
//with successive numbers being distinct. For example [-1,2,-1] could show up, but [2,2,5] will not. 
// NZCU stands for NonZeroConsecutiveUnique. 
randRangeNZCU: function(min,max,n){ 
    excluded = [0] 
    array = [KhanUtil.randRangeExclude(min,max,excluded)] 
    for (i=1;i<n;i++){ 
     excluded = [0,array[i-1]] 
     array.push(KhanUtil.randRangeExclude(min,max,excluded)) 
    } 
    return array 

}, 
// makeCoordinates takes two arrays of the same length and returns the array of ordered pairs. 
// Example: makeCoordinates([1,2,3],[4,5,6]) = [[1,4],[2,5],[3,6]] 
makeCoordinates: function(array1,array2){ 
    array = [] 
    for (i=0;i<array1.length;i++){ 
     array.push([array1[i],array2[i]]) 
    } 
    return array 
}, 

steveCubic: function(x){return -Math.pow(x,3)/2+3*x/2}, 

//niceFunction is a C^1 function which connects the points in "points". It is designed to be used 
//in my "curveSketchingIntuition" exercise. Every point in the list will have 0 slope, except the first and last point. 
niceFunction: function(x,points){ 

    len = points.length 

    var x1 = points[0][0] 
    var x2 = points[1][0] 
    var y1 = points[0][1] 
    var y2 = points[1][1] 
    var k = (y1 - y2)/Math.pow(x1-x2,2) 

    if (x<x2){return k*Math.pow(x-x2,2)+y2} 

    for (i=1;i<len-2;i++){ 
     var x1 = points[i][0] 
     var x2 = points[i+1][0] 
     var y1 = points[i][1] 
     var y2 = points[i+1][1] 

     xNew = (x-x1)*2/(x2-x1)-1 
     yNew = (KhanUtil.steveCubic(xNew)+1)*(y2-y1)/2+y1 
     if (x>=x1 && x<x2){return yNew} 

     } 


    var x1 = points[len-2][0] 
    var x2 = points[len-1][0] 
    var y1 = points[len-2][1] 
    var y2 = points[len-1][1] 
    var k = (y2 - y1)/Math.pow(x1-x2,2) 
    if (x>=x1){return k*Math.pow(x-x1,2)+y1} 

}, 

makeXList: function(){ 
array = [-10] 
i=0 
while(array[i]<10){ 
    x = array[i]+3*KhanUtil.randRange(1,3) 
    if (x<10){array.push(x)} 
    i=i+1 
    } 
array.push(10) 
return array 

}, 

makeYList:function(min,max,n){ 
    excluded = [0] 
    array = [KhanUtil.randRangeExclude(min,max,excluded)] 
    excluded.push(array[0]) 
    array.push[KhanUtil.randRangeExclude(min,max,excluded)] 
    excluded = [0] 
    for (i=1;i<n;i++){ 
     if (array[i-2]<array[i-1]){ 
      array.push(KhanUtil.randRangeExclude(min,array[i-1]-1,excluded)) 
      } 
     else{array.push(KhanUtil.randRangeExclude(array[i-1]+1,max,excluded))} 
     } 

    return array 

}, 

newtonRoot: function(f,a){ 
    var z = a 
    var m = (f(z+.01)-f(z-.01))/.02 
    for(i=0;i<2;i++){ 
     z = z-f(z)/m 
     m = (f(z+.01)-f(z-.01))/.02 
    } 
    return z 
}, 

steveSign: function(num){ 
    num = parseFloat(num) 
    if (num>=0){return 1} 
    else{return -1} 
}, 

steveRoot: function(f,xmin,xmax){ 
    var l = xmin 
    var r = xmax 
    var z = 0 
    for (i=0;i<6;i++){ 
     z = (l + r)/2 
     if (KhanUtil.steveSign(f(l)) === KhanUtil.steveSign(f(z))){ l = z} 
     else{r = z}  
    } 
    return z 
}, 

locateZeros: function(f,points){ 
    var len = points.length 
    var list = [] 

    for(i=0;i<len-1;i++){ 
     var x0 = points[i][0] 
     var x1 = points[i+1][0] 
     var y0 = points[i][1] 
     var y1 = points[i+1][1] 
     // var m = (y1-y0)/(x1-x0) 
     // var a = -y0/m+x0 
     // var z = KhanUtil.steveRoot(f,1,2) 
     list.push(KhanUtil.steveSign(f(x0))) 

    } 
    return list 
}, 

});

+2

“胡扯了” 的意思究竟是什么?控制台错误? – m90 2012-08-14 08:05:27

+0

我不确定,我对此很感兴趣。我得到一个死亡的纺车,我的页面没有加载。有没有一种很好的方法来查看究竟发生了什么? – user1597417 2012-08-14 08:08:51

+0

如果您使用Chrome按下CTRL + SHIFT +,我将打开开发工具,您可能会在控制台部分看到一些错误。 Firefox也有开发工具(右键单击并选择“检查”),即使IE浏览器也是如此(尽管我不知道如何到那里看看)。 – m90 2012-08-14 08:11:04

回答

1
  1. 在javascript x**2无效。 (SyntaxError: Unexpected token *

  2. 您需要运行KhanUtil.steveRoot(f,1,2);而不仅仅是steveRoot(f,1,2);

+0

nope没有错过返回IRL,只是在我的帖子里。 – user1597417 2012-08-14 08:07:51

+0

@ user1597417检查我的编辑。 – xdazz 2012-08-14 08:14:09

+0

对不起,这些错误在我的实际代码中也不是错误。函数f工作得很好,它只是运行steveRoot,将其混淆。 – user1597417 2012-08-14 08:17:19

相关问题