简体   繁体   English

矛盾的Javascript变量定义

[英]Paradoxical Javascript Variable Defining

I am trying to calculate a maths question. 我正在尝试计算一个数学问题。 It requires the use of variables within variables that loop around. 它要求在循环的变量中使用变量。

Here is my JavaScript Code: 这是我的JavaScript代码:

var a = m+1;
var b = n+1;
var c = o+1;
var d = p+1;
var e = q+1;
var f = r+1;
var g = s+1;
var h = t+1;
var i = u+1;
var j = v+1;
var l = w+1;

var m = (1+b)/2;
var n = (a+c)/2;
var o = (b+d)/2;
var p = (c+e)/2;
var q = (d+f)/2;
var r = (e+g)/2;
var s = (f+h)/2;
var t = (g+i)/2;
var u = (h+j)/2;
var v = (i+l)/2;
var w = (j+12)/2;

function CalculateA() {
    alert(a);
}

The HTML is just a button calling CalculateA() Function. HTML只是调用CalculateA()函数的按钮。

This results with undefined. 结果未定义。 Is there a way to properly calculate this or is it not possible with javascript or coding. 有没有一种方法可以正确地计算出这一点,或者使用JavaScript或编码是不可能的。

Edit I relize now that this isn't working very well. 编辑我现在认为这不能很好地工作。 Also I ran it through excel with looping calculations and found the answer. 我也通过循环计算在excel中运行它,并找到了答案。 Thanks for all your help anyway. 谢谢您的帮助。 https://drive.google.com/open?id=0B9I2lCue4HpLZEh3X0NGTUE4YXM The Excel Calculation ^ https://drive.google.com/open?id=0B9I2lCue4HpLZEh3X0NGTUE4YXM Excel计算^

You could give all variables a start value of zero to prevent NaN and then call the operations again until a is converging to 12.916666666666651 . 您可以将所有变量的起始值设为零以防止出现NaN ,然后再次调用操作,直到a收敛至12.916666666666651

 function calc() { a = m + 1; b = n + 1; c = o + 1; d = p + 1; e = q + 1; f = r + 1; g = s + 1; h = t + 1; i = u + 1; j = v + 1; l = w + 1; m = (1 + b) / 2; n = (a + c) / 2; o = (b + d) / 2; p = (c + e) / 2; q = (d + f) / 2; r = (e + g) / 2; s = (f + h) / 2; t = (g + i) / 2; u = (h + j) / 2; v = (i + l) / 2; w = (j + 12) / 2; console.log(a); } var a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0, i = 0, j = 0, l = 0, m = 0, n = 0, o = 0, p = 0, q = 0, r = 0, s = 0, t = 0, u = 0, v = 0, w = 0, ii, letter; for (ii = 0; ii < 1000; ii++) { calc(); } for (ii = 1; ii <= 23; ii++) { letter = (ii + 9).toString(36); letter === 'k' || console.log(letter, window[letter]); // k is missing ... } 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Nothing is impossible in coding. 编码中没有什么是不可能的。 it is returning undefined because 'a' is declared on top where m is not defined and it is "NaN" error -- Not a Number. 由于未在其中定义m的顶部声明了“ a”,并且返回“ NaN”错误-不是数字,因此返回undefined。

The variables end up depending onto each other, which means that your result will always be NaN. 变量最终彼此依赖,这意味着您的结果将始终为NaN。

Javascript tries to give you an exact result for the formula, which it can't. JavaScript会尝试为您提供公式的确切结果,而事实并非如此。 What you need for this math issue, is to replace the variables in each term with each other, to get to a result with only 1 variable - I don't think there's a way for you to do that in Javascript with less effort than doing it manually. 这个数学问题的需要是,将每个术语中的变量彼此替换,仅用1个变量得出结果-我认为您没有比在Javascript中更轻松地完成此任务的方法了手动。

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

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