javascript - 我收到了很多错误,例如“在第 4 列,而不是第 2 列预期'使用严格'”。这是什么意思?
问题描述
我假设因为它提到了列,它是基于打开和关闭标签的空间存在差异而关闭的。我该如何解决这些问题?好像我解决的越少,弹出的越多。自从我开始并且它工作以来,我一直尝试重做我的代码,但是它只是在我的 change() 函数中丢失了 alert(...Popup) 并且没有变量tickbag。我只是惊讶于它可能会很好,然后只需添加最少的代码,“不应该”(据我所知)对整个程序有任何影响。
这是我的代码:
// Travel Calculator JavaScript
// don't forget to validate at https://jslint.com
/*jslint devel: true, browser: true */
// self-executing "global" anonymous function
// it's here to keep variable and function scope
// contained within our script
(function () {
// use strict enforces more rules
// rules make us better programmers
"use strict";
// === === === === === === === === === === === === ===
// === === === === global variables === === === ===
// === === === === begin here === === === ===
// === === === === === === === === === === === === ===
var mileage = 100;
var mpg = 20;
var ppg = 2.50;
var payrate = 15;
var travelers = 2;
var ticketprice = 250;
var baggage = 25;
var vehicletotal = 0;
var persontotal = 0;
var airtotal = 0;
var vehicleperson = 0;
var output = 0;
var hotel = 300;
var tickbag = 0;
// this is how ouputting the console log works. will miss up code
// if not here
console.log("-- travel-calculator.js starting --");
console.log("-----------------------------\n\n\n");
// === === === === === === === === === === === === ===
// === === === === global functions === === === ===
// === === === === begin here === === === ===
// === === === === === === === === === === === === ===
function byID(element) {
return document.getElementById(element);
}
function calculate() {
// grab the current column1 column and save it!
mileage = Number(byID("mileage").value);
mpg = Number(byID("mpg").value);
ppg = Number(byID("ppg").value);
travelers = Number(byID("travelers").value);
payrate = Number(byID("payrate").value);
ticketprice = Number(byID("ticketprice").value);
baggage = Number(byID("baggage").value);
hotel = Number(byID("hotel").value);
vehicletotal = (mileage / mpg * ppg);
persontotal = (travelers * (8 * payrate));
tickbag = (ticketprice + baggage);
airtotal = (ticketprice + baggage) * travelers;
vehicleperson = vehicletotal + persontotal + hotel;
output = "Vehicle Costs: $" + (vehicletotal).toFixed(2) + "\n";
output += "Traveler Costs: $" + persontotal.toFixed(2) + "\n";
output += "Air Cost per Person: $" + tickbag.toFixed(2) + "\n";
output += "Total Cost by Car: $" + vehicleperson.toFixed(2) + "\n";
output += "Total Cost by Plane: $" + airtotal.toFixed(2) + "\n";
alert(output);
}
function change() {
var image = byID("testing");
var carPopup = ("It is cheaper to take a car!");
var airPopup = ("It is cheaper to take a plane!");
if (vehicleperson < airtotal) {
image.src = "images/green_mini_cooper.jpg";
alert(carPopup);
} else {
image.src = "images/airplane.jpg";
alert(airPopup);
}
}
console.log("-- updated by click --");
console.log("----------------------\n\n\n");
console.log("-- updated by \"calculate\" --");
console.log("-------------------------\n\n\n");
byID("calculate-button").addEventListener("click", calculate);
byID("calculate-button").addEventListener("click", change);
}());
解决方案
jslint 希望您的缩进以 4 个空格为一组,而不是 2 个。
如果您想继续缩进 2 个空格,您可以添加white: true
到jslint
文件顶部的注释中。这将忽略空格警告。
改变:/*jslint devel: true, browser: true */
至:/*jslint devel: true, browser: true, white: true */
您可以从 jslint 站点获得帮助(查找有关空格的部分):https ://www.jslint.com/help.html
如果你愿意,可以测试你的代码:https ://www.jslint.com/
推荐阅读
- angular - 如何将component.ts中的变量发送到service.ts以进行身份验证Angular
- windows - DXGI_PRESENT_ALLOW_TEARING 标志的行为是什么?
- machine-learning - 如何对来自 websocket url 的视频流进行对象检测
- git - 在 GitHub 中更改仓库名称后如何推送到 GitHub
- python - 如何根据分值对列表列表进行排序
- excel - 如何使用excel公式搜索包含关键字的单元格,并返回相应的值
- sql - 如果选择的类别级别低于配置的级别,则显示最低级别
- python - 为什么我会收到 AmbiguousForeignKeysError?
- java - 在java中创建一个shell控制台(类似于windows中的cmd,unix中的bash等)
- java - 您可以在不导入或使用任何 Java 标准库包或组件的情况下读取 .txt 文件吗?