需要删除+ =

时间:2017-05-09 21:11:50

标签: javascript

程序通常会显示类似1 + 3 + 5 + = 9的内容,但我希望在5之后摆脱+。请帮我解决此问题。

var userNum = prompt("Pick a number and every odd number between 1 and that 
number will be added");

var  increase = 0;

 var totalSum = 0;

 var expression = "+";

document.write("the sum of the odd numbers are:");
// while the increase is less than the userNum , the increase will 
increase by one

while(increase < userNum){
  increase++
  // if the increase is odd , it will be added to the totalSum
  if(increase % 2 === 0){

  } else {
    document.write(increase+ expression)
    totalSum = totalSum + increase
  }
}

document.write( 0+ "=" + totalSum);

6 个答案:

答案 0 :(得分:2)

您可以将+符号放在前面,在第一次迭代中,不要添加它:

var userNum = prompt("Pick a number and every odd number between 1 and that number will be added");

var increase = 1,
    totalSum = 0,
    expression = "+",
    str = "The sum of the odd numbers is: ";

while(increase < userNum) {
    if(increase !== 1) { str += expression; }
    str += increase;
    totalSum += increase;
    increase += 2;
}

document.write( str + "=" + totalSum );

答案 1 :(得分:1)

不是在迭代时创建输出,而是将数字放入数组中,只需.join("+")MDN)最后一个数组,以便在结尾创建字符串1+3+5。< / p>

我将实施作为练习留下。

答案 2 :(得分:0)

尝试检查while循环的最后一次迭代 - (你可以用下面的循环替换你的while循环)

while(increase < userNum){
  increase++
  if(increase % 2 !== 0){
    if(increase === userNum){
      document.write(increase)
    }else{
      document.write(increase+ expression)
      totalSum = totalSum + increase
    }
  }
}

希望这有帮助。

答案 3 :(得分:0)

为数组添加值并使用+符号连接。

HTML:

<div class="sum">
</div>

JS:

 var userNum=10;

var  increase = 0;

 var totalSum = 0;

 var expression = "+";

   $('.sum').text("the sum of the odd numbers are:");
  // while the increase is less than the userNum , the increase will 
   //increase by one
   var txt=[];
   while(increase < userNum){
  increase++
   // if the increase is odd , it will be added to the totalSum
    if(increase % 2 === 0){}
    else{
         txt.push(increase);
            totalSum = totalSum + increase
    }    
}
$(".sum").text(txt.join("+") + "=" + totalSum);

https://jsfiddle.net/u5o9qb0c/

答案 4 :(得分:0)

首先,不要使用String title = 'testing abcdefgh.abc.cde.fgh test testing issue' String[] test = title.split(" "); String[] newTest = test[1]; println newTest 。它的用例非常有限,通常会覆盖您现有的文档。相反,设置一个空的HTML元素作为“输出”区域,然后只写入该区域。

接下来,您将获得额外的document.write()符号,因为您正在撰写:

+

document.write(increase + expression); 被硬编码为expression符号。

只需添加另一个操作数即可添加。

应该是这样的:

+
/* 
   Place all of this code in "script" tags and place those tags
   just before the closing body tag (</body>) so that this code won't run
   until all of the HTML is parsed
*/

// Get a reference to the output element
var output = document.getElementById("output");

var endNum = prompt("Pick a number and every odd number between 1 and that number will be added");

// Since we know you want to start at 1, initialze the variable to 1
var increase = 1;
var totalSum = 0;

// This will hold the result that will be injected just once into the document
var result = "";

// while the increase is less than the userNum , the increase will increase by one

while(increase < endNum){
  // Just check to see if we've already started writing the result
  // and prepend the + sign if so.
  if(result !== ""){
    result += "+";
  }
  result += increase;
  totalSum += increase;

  // Just increment by 2 to stay with odd numbers
  increase += 2;
}

output.innerHTML += result + "=" + totalSum;

答案 5 :(得分:-1)

你正在做的事情可能更简洁。见下文。

将此increase变量更改为初始化为1,因为根据提示,这始终是您的第一个值。

expression作为主要文本,因为a)我希望在其他任何内容之前打印,并且b)我不希望在第一次迭代时使用加号

关于循环中的expression赋值,没有必要像我一样分配每次迭代,只是分配它比检查我是否每次都需要这样做更简洁。

我将increase的增量移动到循环中的后面,以便打印的值是循环开始时的值。这是一个偏好的问题,但如果我希望在document.write之前增加它,我将不得不将其初始化为-1,我不喜欢从传达明确意图的角度来看。

除了没有必要之外,我也毫无理由地摆脱了分号。 (附录:在本次讨论的上下文中,我没有规定进行此更改。这是我的代码样式,但在语句之间添加分号对代码片段没有任何相关影响。)

  var userNum = prompt("Pick a number and every odd number between 1 and that number will be added")
  var  increase = 1 
  var totalSum = 0
  var expression = 'the sum of the odd numbers are:'
  
  while (increase <= userNum) {
    document.write(expression + increase)
    totalSum += increase
    increase += 2
    expression = '+'
  }

 document.write("=" + totalSum)