简体   繁体   English

为什么此javascript在浏览器中不起作用?

[英]Why doesn't this javascript work in browser?

I have the following code in a .js file: 我在.js文件中有以下代码:

$.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
},

});

In my html file I define var f = function(x){return x**2 - 2} and run steveRoot(f,1,2) , but my browser craps out. 在我的html文件中,我定义了var f = function(x){return x**2 - 2} steveRoot(f,1,2) var f = function(x){return x**2 - 2}并运行steveRoot(f,1,2) ,但是我的浏览器无法正常运行。 Why is this happening? 为什么会这样呢?

EDIT: 编辑:

I am posting the entirety of my code, because it was requested in the comments. 我正在发布我的代码的全部,因为注释中要求提供它。 thanks a bunch for trying to help me out guys. 感谢一群试图帮助我的家伙。 The weird thing is the code runs fine 9 times out of ten. 奇怪的是,代码可以正常运行十分之九。 It is just occasionally that it "craps out". 只是偶尔它“突然出现”。 There are a lot of random variables in here, but I can't imagine why steveRoot would care about getting a slightly different function. 这里有很多随机变量,但是我无法想象为什么steveRoot会关心获得稍微不同的功能。 The code works totally fine and never craps out when I don't include a variable which calls steveRoot. 当我不包含调用steveRoot的变量时,代码可以正常工作,并且不会崩溃。

The HTML file: 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>

The .js file: .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
},

}); });

  1. In javascript x**2 is not valid. 在javascript中, x**2无效。 ( SyntaxError: Unexpected token * ) SyntaxError: Unexpected token *

  2. You need to run KhanUtil.steveRoot(f,1,2); 您需要运行KhanUtil.steveRoot(f,1,2); not just steveRoot(f,1,2); 不只是steveRoot(f,1,2); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM