为什么这个JavaScript在浏览器中不起作用

Why doesn't this javascript work in browser?

本文关键字:浏览器 不起作用 JavaScript 为什么      更新时间:2023-09-26

我在.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
},
});

在我的 html 文件中,我定义了var f = function(x){return x**2 - 2}并运行steveRoot(f,1,2),但我的浏览器崩溃了。 为什么会这样?

编辑:

正在发布我的全部代码,因为它是在评论中要求的。 感谢一群人试图帮助我。 奇怪的是代码在十次中有 9 次运行良好。 只是偶尔会"掰子"。 这里有很多随机变量,但我无法想象为什么 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
},

});

  1. 在javascript中,x**2是无效的。( SyntaxError: Unexpected token *

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