[英]How to output a .js file without using .html or a script tag
这是我的 javascript 代码,文件名为 .js 我似乎无法仅使用这个来输出我的 document.write。 我可以通过将其转换为 .html 文件并用<script>
将代码括起来来实现。 我想在不更改文件的情况下输出并将其保存在 .js 中,我该怎么做?
document.write("<b>A. Sales Problem</b><br><br>");
var productProfitArray = [
{"Product A": -75},
{"Product B": -70},
{"Product C": 98}, // Highest sales profit
{"Product D": 5}, // Profit nearest to 0
{"Product E": -88}, // Lowest sales profit
{"Product F": 29}
];
function topProduct(productProfitArray) {
if (toString.call(productProfitArray) !== "[object Array]")
return false;
return Math.max.apply(null, productProfitArray.map(o => Object.values(o)[0]));
}
function bottomProduct(productProfitArray) {
if (toString.call(productProfitArray) !== "[object Array]")
return false;
return Math.min.apply(null, productProfitArray.map(o => Object.values(o)[0]));
}
function zeroProfitProduct(productProfitArray) {
const zero = 0;
return productProfitArray.map(o => Object.values(o)[0]).reduce((a, b) => {
return Math.abs(b - zero) < Math.abs(a - zero) ? b : a;
});
}
var topProductValue = topProduct(productProfitArray);
var bottomProductValue = bottomProduct(productProfitArray);
var zeroProfitProductValue = zeroProfitProduct(productProfitArray);
document.write('Highest sales profit is: ' + topProductValue + '<br>');
document.write('Lowest sales profit is: ' + bottomProductValue + '<br>');
document.write('Profit nearest to 0 is: ' + zeroProfitProductValue);
你可以有一个段落元素
<p id = "highestsalesprofit"></p>
元素,然后使用以下内容获取元素并将文本添加到 ii
document.getElementById('highestsalesprofit').innerHTML = 'Highest sales profit is: ' + topProductValue + '<br>'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.