0

I always get this error "Uncaught SyntaxError: Unexpected token else ". But I can't find the said error in my code. I already check the brackets and its nothing wring. Help me to find out what's the error, please. Thanks!

Here's the code (This code automatically transformed by xslt):

 <html>
 <head>
 <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <script type="text/javascript" src="https://www.google.com/jsapi"></script><script type="text/javascript">
  google.load("visualization", "1", {packages:["corechart"]});
  </script>
  </head>
  <body># Of Issues (all) Per HelpDesk Agent Per Day<div id="chartdiv_1"></div><script type="text/javascript">
google.setOnLoadCallback(drawChart_1);
function drawChart_1() {
    var isProportional=1;
    var chart = new google.visualization.ColumnChart(document.getElementById('chartdiv_1'));
    var options = {width:1000, height:400, isStacked: true};
    var data = google.visualization.arrayToDataTable([
['Date','User1','User2','User3']
,['01/21/2014',5,0,0]
,['01/22/2014',0,13,0]
,['01/23/2014',0,4,0]
,['01/24/2014',7,0,0]
,['01/25/2014',0,0,0]
    ]);
    if (isProportional==1) {
        var view  = new google.visualization.DataView(data);
        var columns=[0];
        for (var i=1; i &lt; data.getNumberOfColumns(); i++) {
            columns.push({
                type: 'number',
                label: data.getColumnLabel(i),
                calc: (function (col) {
                    return function (dt, row) {
                        var val = dt.getValue(row, col);
                        var total = 0;
                        for (var j = 1; j &lt; dt.getNumberOfColumns(); j++) {
                            total += dt.getValue(row, j);
                        }
                        return (total == 0) ? null : {v: val / total, f: val.toString()};
                    };
               })(i)
            });
            columns.push({
                type: 'string',
                role: 'annotation',
                sourceColumn: i,
                calc: 'stringify'
            });
            view.setColumns(columns);
            chart.draw(view, options);
        } else {
            chart.draw(data, options);
        }
    }
</script></body>
</html>
yingyang
  • 47
  • 2
  • 12

2 Answers2

3

line 25, for (var i=1; i &lt; data.getNumberOfColumns(); i++) { should be for (var i=1; i < data.getNumberOfColumns(); i++) { (notice the <)

NoGray
  • 1,149
  • 7
  • 9
  • but i use < because of the xslt rule, so i think that's not the cause of error – yingyang Apr 16 '14 at 04:13
  • yes i know, but the html file is transformed by xslt and i try to change the < into <, nothing happened. There's still an error – yingyang Apr 16 '14 at 04:17
  • 1
    After cleaning the code a bit, you can see the else is also inside your if statement and it should be underneath it – NoGray Apr 16 '14 at 04:18
  • @Yang It should at least tell you what line the error occurs on. Can you point that line out in your question? – ajp15243 Apr 16 '14 at 04:18
  • another line have the same problem `for (var j = 1; j < dt.getNumberOfColumns(); j++) { total += dt.getValue(row, j); }` – Fikri Marhan Apr 16 '14 at 04:22
  • @ajp15243 that's the problem, when I inspect the element, there's no specified line that cause an error – yingyang Apr 16 '14 at 04:26
  • @Yang, first move your else statement below (See my comments above) and if you still have the error, change the &alt; to <. – NoGray Apr 16 '14 at 04:30
  • 1
    check this one - http://stackoverflow.com/questions/435005/xslt-javascript-and-unescaped-html-entities – vivek_nk Apr 16 '14 at 04:34
  • Nothing happened @NoGray – yingyang Apr 16 '14 at 04:36
1

I Linted your code, with JSHint and you're not closing you first for statement. If you really need to write the code inside the script tag, I really recommend to write it using a Lint tool in a external file then copy and paste it, it will prevent errors like this.

dpsxp
  • 11
  • 1
  • Can confirm that the `for` loop's closing `}` doesn't exist (and causes a syntax error in my IDE), and adding it fixes the error. – ajp15243 Apr 16 '14 at 12:48