javascript - 你能帮忙看看控制台日志吗?
问题描述
我需要计算 X 和 Y,但我的控制台日志有效,如果匹配 [i].includes(something) == "true" 可能有问题
var input = "10W5N2S6E";
var matches = input.split(/(?<=[A-Z])(?=\d)/);
for (let i = 0; i < matches.length; i++) {
let x = 0;
let y = 0;
if (matches[i].includes("w") == "true") {
x = x - matches[i];
console.log(x);
}
if (matches[i].includes("e") == "true") {
x = x + matches[i];
console.log(x);
}
if (matches[i].includes("n") == "true") {
y = y + matches[i];
console.log(y);
}
if (matches[i].includes("s") == "true") {
y = y - matches[i];
console.log(y);
}
}
解决方案
您需要使用,toLowerCase()
因为includes()
它区分大小写。
此外,无需比较,includes() == "true"
因为它返回一个布尔值并且对于 if 条件(它将根据该布尔条件执行下一个块语句)就足够了
var input = "10W5N2S6E";
var matches = input.split(/(?<=[A-Z])(?=\d)/);
for (let i = 0; i < matches.length; i++) {
const match = matches[i].toLowerCase();
let x = 0;
let y = 0;
if (match.includes("w")) {
x = x - matches[i];
console.log(x);
}
if (match.includes("e")) {
x = x + matches[i];
console.log(x);
}
if (match.includes("n")) {
y = y + matches[i];
console.log(y);
}
if (match.includes("s")) {
y = y - matches[i];
console.log(y);
}
}
推荐阅读
- math - C#/Unity:在 3D 空间中旋转三角形以形成 2D 多边形
- c# - 在 2.1 中使用 .Net Core 2.2 类
- azure-ad-b2c - Azure AD B2C:向应用程序添加 api 访问时出现内部错误
- visual-c++ - 将文本附加到 CEdit 控件后出现奇怪的字体效果
- dapper - 使用参数时在“=@”处或附近出现语法错误
- java - 在 SpringData 中持久化实体而不需要获取关联
- android - 覆盖cordova项目中的colors.xml或strings.xml
- json - 如何在 Sqlite 中加入多个 JSON 列
- amcharts - x轴上的Amchart水平滚动条
- c# - 'else if' 不能使用 TryParse 中的变量