0

SOLUTION: I realized that the current Regional Setting of test environment is set to Turkish, and it uses comma for decimal symbol. In my local, it is set to UK, and that's the reason that the code works in my local and doesn't work in test. I guess I'll replace all commas with dots beforehand. Thanks for all the replies.

I'm trying to fill a bar chart with following data:

var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];
var ticks = [[0, 'Atmosfer'],[1, 'Servis'],[2, 'Yemeklerimiz']];
var labels = ['Atmosfer','Servis','Yemeklerimiz'];
var mainQuest_d1 = 8,16666666666667;
var mainQuest_d2 = 7,95833333333333;
var mainQuest_d3 = 8,125;
var d_main_quest_bar = [[0, 8,16666666666667],[1, 7,95833333333333],[2, 8,125]];

I get this error:

Uncaught SyntaxError: Unexpected number

I can't see what's wrong the code above. It works fine in localhost, but when I publish it to the test server, it gives this error.

Complete code that's not yet rendered by Razor:

int i = 0;
            int j = 0;
            int m = 0;
            @Html.Raw("var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];");
            @Html.Raw("var ticks = [");

            if (Model.MainQuestionsRatingList != null)
            {
                foreach (var item in Model.MainQuestionsRatingList)
                {
                    j++;
                    @Html.Raw("["+(j-1)+", '"+item.QuestionText+"']")
                    if (j != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
                }
            }
            @Html.Raw("];");

            @Html.Raw("var labels = [");

            if (Model.MainQuestionsRatingList != null)
            {
                foreach (var item in Model.MainQuestionsRatingList)
                {
                    m++;
                    @Html.Raw("'"+item.QuestionText+"'")
                    if (m != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
                }
            }

            @Html.Raw("];");

            if (Model.MainQuestionsRatingList != null)
            {
                foreach (var item in Model.MainQuestionsRatingList)
                {
                   i++;
                  @Html.Raw("var mainQuest_d" + i + " = " + item.Avg + ";");

                }
            }


            i = 0;
              @Html.Raw("var d_main_quest_bar = [");
            if (Model.MainQuestionsRatingList != null)
            {
                foreach (var item in Model.MainQuestionsRatingList)
                {
                    i++;
                    @Html.Raw("[" + (i-1) + ", "+item.Avg+"]");
                    if (i != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
                }
            }
              @Html.Raw("];");
        }

        data.push({
                label: labels,
                data: d_main_quest_bar,
                bars: {
                    show: true,
                    barWidth: 0.2,
                    order: 1
                }
            });

EDIT: I ran the same code in my local, and figured out that the commas are automatically replaced with dots and that's why it works in my local as @T.J. Crowder said. But it doesn't happen when I run it in test. How is that possible?

Burak Karakuş
  • 1,289
  • 5
  • 20
  • 39
  • the decimal separator is the dot (.) symbol not the comma (,) – Fabrizio Calderan loves trees Sep 29 '14 at 14:54
  • JavaScript uses `.` in numbers and not `,`! – Sirko Sep 29 '14 at 14:54
  • Note that `var foo = (8,125)` would not result in an error because the parentheses delimit the expression and the comma operator comes into play (so `foo` will be `125` in the end). So, it's quite important you always get your decimal separator right. – Frédéric Hamidi Sep 29 '14 at 14:55
  • 4
    Downvoters: This isn't as silly a mistake as it may seem, there are lots of cultures where `,`, not `.`, is the decimal separator (and `.` is the thousands separator -- great fun). – T.J. Crowder Sep 29 '14 at 14:58
  • @T.J.Crowder I don't understand why people downvote although I stated that it works in my local. Thanks for your consideration. – Burak Karakuş Sep 29 '14 at 14:59
  • 1
    @BurakKarakuş: I very much doubt it works on `localhost`. Any JavaScript engine that would read `8,7` as a decimal number in code would be violating the specification, in a pretty dramatic way. – T.J. Crowder Sep 29 '14 at 15:00
  • I'll provide an example tomorrow if you are still interested. – Burak Karakuş Sep 29 '14 at 15:01

4 Answers4

4

You can't use localized decimal separator characters in JavaScript source code. You must use .:

var mainQuest_d1 = 8.16666666666667;
var mainQuest_d2 = 7.95833333333333;
var mainQuest_d3 = 8.125;

See What is the decimal separator symbol in JavaScript?

It should be obvious that , has another meaning already. How many elements do you expect the array

[0, 8,16666666666667]

to contain?

Community
  • 1
  • 1
Matt Ball
  • 332,322
  • 92
  • 617
  • 683
  • 2
    I've no idea. I'm not convinced that it does. Can you provide an [MCVE](http://stackoverflow.com/help/mcve) proving that it works locally? – Matt Ball Sep 29 '14 at 14:58
  • I'm not in the office right now, I can provide an MCVE tomorrow. But it does work, I assure you. – Burak Karakuş Sep 29 '14 at 15:00
  • 1
    @BurakKarakuş: I guarantee you that numeric literals in JavaScript code **never** use `,` as the decimal separator. Any engine that did would be violating the spec. You may be passing a *string* into some library or something that interprets it according to your locale, but you're not typing a numeric literal in JavaScript code with a `,` and getting a correct result. – T.J. Crowder Sep 29 '14 at 15:02
  • I do it with razor, maybe that's what you mean? I updated my whole code in question. – Burak Karakuş Sep 29 '14 at 15:07
  • @T.J.Crowder I updated my question with solution. Thanks for your time and effort. – Burak Karakuş Sep 30 '14 at 13:42
2

You shouldn't use commas in your numbers. Use a decimal place instead. Commas are special characters reserved for other uses, such as separators in arrays and function parameters.

For example:

8,16666666666667

should be

8.16666666666667

You have a few instances so here is the full code written correctly:

var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];
var ticks = [[0, 'Atmosfer'],[1, 'Servis'],[2, 'Yemeklerimiz']];
var labels = ['Atmosfer','Servis','Yemeklerimiz'];
var mainQuest_d1 = 8.16666666666667;
var mainQuest_d2 = 7.95833333333333;
var mainQuest_d3 = 8.125;
var d_main_quest_bar = [[0, 8.16666666666667],[1, 7.95833333333333],[2, 8.125]];

(there are 6 changes in total across the last 4 lines)

musefan
  • 45,726
  • 20
  • 123
  • 171
1

You shouldn't use commas for integers:

<script type="text/javascript">
var oneToTen = [0,1,2,3,4,5,6,7,8,9,10];
var ticks = [[0, 'Atmosfer'],[1, 'Servis'],[2, 'Yemeklerimiz']];
var labels = ['Atmosfer','Servis','Yemeklerimiz'];
var mainQuest_d1 = 8.16666666666667;
var mainQuest_d2 = 7.95833333333333;
var mainQuest_d3 = 8.125;
var d_main_quest_bar = [[0, 8.16666666666667],[1, 7.95833333333333],[2, 8.125]];
</script>

Source: http://en.wikipedia.org/wiki/JavaScript_syntax#Number

Wissam El-Kik
  • 2,371
  • 1
  • 13
  • 21
  • Keep in mind that JS provide an accuracy of 16 digits. The numbers you provided have an accuracy of 14 digits. If it reaches 16 digits, then you probably need to use `toFixed()`: http://www.w3schools.com/jsref/jsref_tofixed.asp – Wissam El-Kik Sep 29 '14 at 15:08
0

This is incorrect:

var mainQuest_d1 = 8,16666666666667;
var mainQuest_d2 = 7,95833333333333;
var mainQuest_d3 = 8,125;

You can't use commas there.

Brian
  • 2,923
  • 17
  • 27