Javascript-行总数
我试图创建这个程序来提示用户输入两个单词,然后在一行上打印出这两个单词。单词将被足够多的点隔开,因此总行长为30。我已经试过了,但似乎无法得到它Javascript-行总数,javascript,Javascript,我试图创建这个程序来提示用户输入两个单词,然后在一行上打印出这两个单词。单词将被足够多的点隔开,因此总行长为30。我已经试过了,但似乎无法得到它 <html> <head> <title>Lenth of 30</title> <script type="text/javascript"> //Program: Lenth of 30 //Purpose: The words will be separated by enough do
<html>
<head>
<title>Lenth of 30</title>
<script type="text/javascript">
//Program: Lenth of 30
//Purpose: The words will be separated by enough dots so that the total line length is 30:
//Date last modified: 4/11/12
var firstword = ""
var secondword = ""
firstword = prompt("Please enter the first word.")
secondword = prompt("Please enter the second word.")
document.write(firstword + secondword)
</script>
</head>
<body>
</form>
</body>
</html>
长度30
//节目:第30集
//目的:用足够多的点分隔单词,使总行长为30:
//上次修改日期:2012年11月4日
var firstword=“”
var secondword=“”
firstword=prompt(“请输入第一个单词”)
secondword=提示(“请输入第二个单词”)
写文档(第一个字+第二个字)
例如:
输入第一个单词:
乌龟
输入第二个单词
153
(程序将打印以下内容)
turtle从30中减去第一个单词的长度和第二个单词的长度,然后在for循环中打印出那么多点。从30中减去第一个单词的长度和第二个单词的长度,然后在for循环中打印出那么多点。您需要计算需要多少周期
var enteredLength = firstword.length + secondword.length;
var dotCount = 30 - enteredLength;
var dots = "";
for(var i = 0; i < dotCount; i++) dots += '.';
var enteredLength=firstword.length+secondword.length;
var dotCount=30-输入长度;
var dots=“”;
对于(变量i=0;i
您可以从那里获取if…您需要计算需要多少周期
var enteredLength = firstword.length + secondword.length;
var dotCount = 30 - enteredLength;
var dots = "";
for(var i = 0; i < dotCount; i++) dots += '.';
var enteredLength=firstword.length+secondword.length;
var dotCount=30-输入长度;
var dots=“”;
对于(变量i=0;i
您可以从那里获取if…您可以使用
length
属性来确定每个字符串的长度,然后计算需要添加的
的数量。您可以使用length
属性来确定每个字符串的长度,然后计算需要添加的
的数量。下面是一个通用解决方案,向您展示了如何执行此操作:
function dotPad(part1, part2, totalLength) {
// defaults to a total length of 30
var string = part1 + part2,
dots = Array((totalLength || 30) + 1).join('.');
return part1 + dots.slice(string.length) + part2;
}
按如下方式使用:
dotPad('foo', 'bar'); // 'foo........................bar'
就你而言:
dotPad(firstword, secondword);
这是一个非常简单的解决方案-如果需要,请验证输入字符串的串联形式是否短于
长度字符。以下是一个通用解决方案,向您展示了如何执行此操作:
function dotPad(part1, part2, totalLength) {
// defaults to a total length of 30
var string = part1 + part2,
dots = Array((totalLength || 30) + 1).join('.');
return part1 + dots.slice(string.length) + part2;
}
按如下方式使用:
dotPad('foo', 'bar'); // 'foo........................bar'
就你而言:
dotPad(firstword, secondword);
这是一个非常简单的解决方案-如果需要,请验证输入字符串的串联形式是否短于长度字符。您可以通过一些简单的数学计算得到点数。用30减去每个单词的长度
var dotLen = 30 - firstword.length - secondword.length;
document.write( firstword );
while ( dotLen-- ) {
document.write( "." );
}
document.write( secondword );
编辑:实际上我更喜欢Mathias的解决方案。但你可以让它更简单:
document.write( firstword + Array( 31 - firstword.length - secondword.length ).join( '.' ) + secondword );
你可以用一些简单的数学计算出点的数量。用30减去每个单词的长度
var dotLen = 30 - firstword.length - secondword.length;
document.write( firstword );
while ( dotLen-- ) {
document.write( "." );
}
document.write( secondword );
编辑:实际上我更喜欢Mathias的解决方案。但你可以让它更简单:
document.write( firstword + Array( 31 - firstword.length - secondword.length ).join( '.' ) + secondword );
你希望有人帮你解决这个问题吗?我看不出你的问题所在,请阅读有关字符串函数的手册你希望有人能帮你解决这个问题吗?我不认为你的问题有什么问题,请阅读有关字符串函数的手册。我不知道为什么我会被否决。这个网站的目的不是为人们编写完整的程序。我不知道为什么我会被否决。这个网站的重点不是为人们编写整个程序。你可以进一步简化:返回part1+Array((totalLength | 30)+1-part1.length-part2.length)。join('.')+part2代码>@WilliamVanRensselaer右。不过,我认为这种方式(个人偏好)更具可读性。您可以进一步简化:returnpart1+Array((totalLength | 30)+1-part1.length-part2.length)代码>@WilliamVanRensselaer右。不过,我认为这种方式更具可读性(个人偏好)。