简体   繁体   English

如何在不使用 .html 或脚本标记的情况下输出 .js 文件

[英]How to output a .js file without using .html or a script tag

1

Here is my javascript code with the file name .js I cannot seem to output my document.write using only this.这是我的 javascript 代码,文件名为 .js 我似乎无法仅使用这个来输出我的 document.write。 I can do it by turning it into a .html file and enclose the codes with <script> .我可以通过将其转换为 .html 文件并用<script>将代码括起来来实现。 I would like to have an output without changing the file and just keep it in .js, how do I do it?我想在不更改文件的情况下输出并将其保存在 .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);

You could have a paragraph element你可以有一个段落元素

    <p id = "highestsalesprofit"></p> 

element in your HTML and then use the following to get the element and add your text to ii元素,然后使用以下内容获取元素并将文本添加到 ii

    document.getElementById('highestsalesprofit').innerHTML = 'Highest sales profit is: ' + topProductValue + '<br>'

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

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