JavaScript:使用&&与&处理数字时的结果差异
Hey there! Let's work through this together. First off, I see your code snippet cuts off mid-line at Mark = markHeight + 5 * mark...—that's likely a key piece we need to fix first. Let me start by formatting your existing code clearly:
var johnAge = 25; var markAge = 30; var steveAge = 40; var johnHeight = 170; var markHeight = 175; var steveHeight = 150; var John, Mark, Steve; John = johnHeight + 5 * johnAge; Mark = markHeight + 5 * mark... // 这里代码截断了!
From what's here, I can spot a couple of things that might be causing issues, plus some quick improvements:
- Truncated code: The line for calculating
Markis incomplete. I'm guessing you meant to usemarkAgehere, so it should beMark = markHeight + 5 * markAge;—and similarly, Steve's calculation would follow the same pattern:Steve = steveHeight + 5 * steveAge;. - Inconsistent variable naming: You're using uppercase starting letters for
John,Mark,Stevebut lowercase forjohnAge,markHeight, etc. JavaScript is case-sensitive, so this won't break your code outright, but it's easy to mix up names accidentally. Sticking to a consistent style (like camelCase, where all variables start with lowercase and use caps for subsequent words) makes your code cleaner and easier to debug. - Variable declaration grouping: You can group all your
vardeclarations together at the top to keep things organized, instead of spreading them out across multiple lines.
Here's a cleaned-up version of what your code might look like once completed:
// 统一用驼峰式命名,分组声明变量 var johnAge = 25, markAge = 30, steveAge = 40, johnHeight = 170, markHeight = 175, steveHeight = 150, johnScore, markScore, steveScore; // 完成计算逻辑 johnScore = johnHeight + 5 * johnAge; markScore = markHeight + 5 * markAge; steveScore = steveHeight + 5 * steveAge;
Since you mentioned the problem isn't super complex, could you share a bit more detail? Are you getting a specific error message? Is the output not what you expected? Once you fill in that missing code snippet and tell us what's going wrong, we can nail this down quickly!
内容的提问来源于stack exchange,提问作者Anmar Jihad

