添加公式的javascript代码不起作用

添加公式的javascript代码不起作用,javascript,jquery,html,Javascript,Jquery,Html,我用html和javascript编写了这段代码。它所做的是使用提示符生成正数和负数,然后将使用公式n*(n+1)/2生成的所有数字相加。代码似乎不太好用,因为在使用公式和用户输入求和时,我得到了一个错误的值。我的代码: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> <script l

我用html和javascript编写了这段代码。它所做的是使用提示符生成正数和负数,然后将使用公式n*(n+1)/2生成的所有数字相加。代码似乎不太好用,因为在使用公式和用户输入求和时,我得到了一个错误的值。我的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>

    <script language="javascript">

        var countpos = 1;
        var countneg = 0;       

        function pos(){
            var positive = prompt("enter limit for positive numbers");
            document.write(positive*(positive+1)/2 +"<br>");            

            while(countpos <= positive){
                document.write(" " + countpos);
                document.write("<br>");
                countpos++;
            }                                       
        }

        function neg(){
            var negative = prompt("enter limit for negative numbers");
            while(countneg >= negative){
                document.write(" " + countneg);
                document.write("<br>");
                countneg--;
            }
        }

    </script>
</head>

<body>

    What do you want to output? Positive or Negative numbers? <br>
    <input type="button" value="positive numbers" onClick="pos()"> <br>
    <input type="button" value="negative numbers" onClick="neg()">

</body>
</html>

无标题文件
var countpos=1;
var countneg=0;
函数pos(){
var正值=提示(“输入正数的限制”);
文件。写(正数*(正数+1)/2+“
”; while(countpos=负){ 文件。填写(“+countneg”); 文件。写(“
”); countneg--; } } 您想要输出什么?正数还是负数

这是一个字符串,不是一个数字,您需要用parseInt或parseFloat转换它

var positive = parseInt(prompt("enter limit for positive numbers"), 10);


它将它们视为弦。将您的代码更改为此将告诉您为什么

    var positive = prompt("enter limit for positive numbers");
    alert(positive);
    alert(positive+1);
试试这个

 document.write(positive*(Number(positive)+1)/2 +"<br>"); 
文件。写(正数*(正数)+1)/2+“
”;
正如adeneo所说,document.write将覆盖文档上的文本。 试试这个:

    var countpos = 1;
    var countneg = 0;       

    function pos(){
        var positive = prompt("enter limit for positive numbers");
        var str = '';
        str += (positive*(positive+1)/2 +"<br>");            

        while(countpos <= positive){
            str += (" " + countpos);
            str += ("<br>");
            countpos++;
        }                        

        document.write(str);
    }

    function neg(){
        var negative = prompt("enter limit for negative numbers");
        while(countneg >= negative){
            str += (" " + countneg);
            str += ("<br>");
            countneg--;
        }
        document.write(str);
    }
var countpos=1;
var countneg=0;
函数pos(){
var正值=提示(“输入正数的限制”);
var-str='';
str+=(正*(正+1)/2+“
”; while(countpos=负){ str+=(“”+countneg); str+=(“
”); countneg--; } 文件编写(str); }

这将一次使用一个功能,但我现在已经告诉您下一步是如何使用它的。

document.write将在单击按钮时覆盖整个文档?仍然不起作用。我就是这么做的:var positive=prompt(“输入正数的限制”);parseInt(阳性);文件。写(正数*(正数+1)/2+“
”;他不需要转换,因为他正在使用
*
执行一些操作。检查此项:
var strA='2';var strB='3';strA*strB;//6
@DaniloValente作战命令<代码>“2*(“2”+1)=“2”*“21”=42@epascarello哦,对了,对不起。没有注意到
 document.write(positive*(Number(positive)+1)/2 +"<br>"); 
    var countpos = 1;
    var countneg = 0;       

    function pos(){
        var positive = prompt("enter limit for positive numbers");
        var str = '';
        str += (positive*(positive+1)/2 +"<br>");            

        while(countpos <= positive){
            str += (" " + countpos);
            str += ("<br>");
            countpos++;
        }                        

        document.write(str);
    }

    function neg(){
        var negative = prompt("enter limit for negative numbers");
        while(countneg >= negative){
            str += (" " + countneg);
            str += ("<br>");
            countneg--;
        }
        document.write(str);
    }