Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
                    @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)
                    @Html.Raw("'"+item.QuestionText+"'")
                    if (m != Model.MainQuestionsRatingList.Count) { @Html.Raw(","); }
            @Html.Raw("];");
            if (Model.MainQuestionsRatingList != null)
                foreach (var item in Model.MainQuestionsRatingList)
                  @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)
                    @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?

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, 2014 at 14:55 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, 2014 at 14:58 @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, 2014 at 15:00

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?

I've no idea. I'm not convinced that it does. Can you provide an MCVE proving that it works locally? – Matt Ball Sep 29, 2014 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, 2014 at 15:00 @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, 2014 at 15:02

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)

<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

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(): w3schools.com/jsref/jsref_tofixed.asp – Wissam El-Kik Sep 29, 2014 at 15:08
var mainQuest_d1 = 8,16666666666667;
var mainQuest_d2 = 7,95833333333333;
var mainQuest_d3 = 8,125;

You can't use commas there.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.