简体   繁体   English

无法让一些 JS 数学工作

[英]Having trouble getting some JS Math to work

I'm working on a small project for tuning a car in a game.我正在做一个在游戏中调整汽车的小项目。 Allowing users to input the own numbers and have them calculated and then displayed on the screen.允许用户输入自己的数字并进行计算,然后显示在屏幕上。 The user would input a max number(65) a min number(1) and a percentage(55).用户将输入最大数字(65)、最小数字(1)和百分比(55)。 And here is the equation I'm trying to complete: (65-1) *.55 +1.这是我要完成的方程式:(65-1) *.55 +1。

I've linked all of the inputs in the.js file, and I can update the text on the screen as well, but can't get the equation to work properly.我已经链接了 .js 文件中的所有输入,我也可以更新屏幕上的文本,但无法让方程式正常工作。 The problem starts when I try to multiple by the percentage(.55).当我尝试按百分比(0.55)倍增时,问题就开始了。 And I haven't even added the + 1 yet either.而且我什至还没有添加+ 1。 I thought maybe I was having a problem converting from a string to a number as I was receiving 'NaN' in the console.我想当我在控制台中收到“NaN”时,我可能在从字符串转换为数字时遇到问题。 But I'm really not sure when the problem is.但我真的不确定问题出在什么时候。 Any help or suggestions on how to do this better would be greatly appreciated.任何有关如何更好地做到这一点的帮助或建议将不胜感激。 Let me know if you need more information如果您需要更多信息,请与我们联系

 // ========== Main Variables ========== const weight = document.querySelector('#fWeightUserInput').value; const frontWeight = parseInt(weight); const antiRollMin = document.querySelector('#antiRollMin'); const antiRollMax = document.querySelector('#antiRollMax'); const mainForm = document.querySelector('#mainForm'); // ===== Final Tune Text ===== const antiRollFrontTxt = document.querySelector('#calcAntiRollFrontNum'); const antiRollRearTxt = document.querySelector('#calcAntiRollRearNum'); // ========== Tune Math Calculations =========== mainForm.addEventListener('submit', function (e) { e.preventDefault(); const antiRollFront = (antiRollMax.value - antiRollMin.value) * (frontWeight / 100); console.log(frontWeight); antiRollFrontTxt.innerText = antiRollFront; });

I think this is just a data type issue, I would try this:我认为这只是一个数据类型问题,我会试试这个:

const frontWeight = parseFloat(weight); const frontWeight = parseFloat(weight);

I would also change the entire code to this, to make sure you are not getting String instead of numbers:我还将整个代码更改为此,以确保您没有得到字符串而不是数字:



const mainForm = document.querySelector('#mainForm');

// ===== Final Tune Text =====
const antiRollFrontTxt = document.querySelector('#calcAntiRollFrontNum');
const antiRollRearTxt = document.querySelector('#calcAntiRollRearNum');
// ========== Tune Math Calculations ===========

mainForm.addEventListener('submit', function (e) {
  e.preventDefault();

// ========== Main Variables ==========
const weight = parseInt(document.querySelector('#fWeightUserInput').value);
const frontWeight = parseFloat(weight);
const antiRollMin = parseInt(document.querySelector('#antiRollMin').value);
const antiRollMax = parseInt(document.querySelector('#antiRollMax').value);

  const antiRollFront =
    (antiRollMax - antiRollMin) * (frontWeight / 100);
  console.log(frontWeight);
  antiRollFrontTxt.innerText = antiRollFront;
});


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

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