基本的Javascript程序,帮助需要

时间:2013-11-12 00:39:44

标签: javascript

我的程序需要包含一个for循环,使用已经创建的输入函数(名称和数字)和需要获取的总数。我还需要能够取消并转到我的doc.write,我会在其中输入姓名和号码。我还需要给用户另一个机会输入他们的姓名或号码,如果他们不小心输入了字母应该是的数字,反之亦然。我认为我拥有大部分结构,任何帮助都将不胜感激!

function inputName() {

var nameIn = prompt("Enter your Name");
while(!isNaN(nameIn)) {
nameIn = prompt("Must contain only letters");


}
return nameIn;

}
/* INPUT NUMBER */

 function inputNum(){

var numIn=parseFloat(prompt("Enter the number of hours worked \n 0-60 hours"));

var min=0;
var max=60;

while(numIn>min && numIn>max ){
numIn=prompt("Enter a valid number between 0-60");
return numIn;
}
</script>
<body>


<script type="text/javascript">

//DECLARATIONS
var wage=10.00;
var earned; // hrsWrked*wage
var totHrsWrked=0;
var totEarning=0;
var BR= "
";
var howMany;
var loopControl;

//INPUT & PROCESSING

howMany=parseFloat(prompt("How many employees are you inputing?"));

for(loopControl=1 ; loopControl <= howMany; ++loopControl){

var inpNam=inputName();

var inpNumber=inputNum();


earned= inpNumber*wage;
totEarning+=earned;
totHrsWrked+=inpNumber;

//OUTPUT
document.write("Name: "+ inpNam+ BR);
document.write("Hours Worked: " + inpNumber + BR);
document.write("Money Earned: $ " + earned + BR +BR);

}
document.write("Total Hours Worked: " + totHrsWrked.toFixed(2) + BR);
document.write("Total Earnings: " + "$"+totEarning.toFixed(2)+ BR+BR);

</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这是您编辑的代码版本。 :)

<! doctype html>
<html>
<body>
<script>

//DECLARATIONS
var wage=10.00, earned, totHrsWrked=0, totEarning=0, howMany;

//INPUT & PROCESSING
howMany=parseFloat(prompt("No of employees"));
for( var loopControl=1 ; loopControl <= howMany; ++loopControl)
{

    var inpNam=inputName();
    var inpNumber=inputNum();   
    earned= inpNumber*wage;
    totEarning+= (+earned);
    totHrsWrked+= (+inpNumber);
    //OUTPUT
    document.write("Name: "+ inpNam+ "<br>");       
    document.write("\n Hours Worked: " + inpNumber + "<br>");
    document.write("Money Earned: $ " + earned + "<br><br>");
}
document.write("Total Hours Worked: " + totHrsWrked.toFixed(2)+ "<br>");
document.write("Total Earnings: " + "$"+totEarning.toFixed(2)+ "<br>");

//INPUT NAME 
function inputName() {

var nameIn = prompt("Enter your Name");
while(!isNaN(nameIn)) {
nameIn = prompt("Must contain only letters");
}
return nameIn;
}

//INPUT NUMBER 
function inputNum(){
var numIn=parseFloat(prompt("Enter the number of hours worked \n 0-60 hours"));
var min=0;
var max=60;

while(numIn<=min || numIn>=max ){
numIn=prompt("Enter a valid number between 0-60");
}
return numIn;
}
</script>
</body>
</html>
相关问题