这是我第一次使用Arrays。我必须创建一个存储学生类型(州内,州外和国际)信用价格的数组,以及存储全日制,兼职和重载日程的学费的另一个数组。
我在创建函数时遇到问题。我以为我对它有一个很好的把握,但我的calcCost变量显示为未定义。任何建议都表示赞赏。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--Document Head-->
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<!--Title Element-->
<title>Greendale Community College</title>
<!--Style Element-->
<style type="text/css">
body {
background-color: white;
}
h1 {
text-align: center;
font-family: Impact;
color: green;
}
p {
font-size: 36px;
color: green;
}
</style>
<!--Script Element-->
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
// Created function to calculate tuition cost
var creditCostArray = new Array(3);
creditCost[0] = 302.00;
creditCost[1] = 452.00;
creditCost[2] = 588.00;
var tuitionCostArray = new Array(3);
tuitionCost[0] = 3619.00;
tuitionCost[1] = 5429.00;
tuitionCost[2] = 7057.00;
function calcTuition(studentRes) {
var creditTotal = document.getElementsByName("numCredits")[0].value;
console.log(creditTotal);
if (studentRes == "i")
if (creditTotal < 12)
calcCost = creditCost[0] * parseInt(creditTotal);
tuitionTotal = "Student is an Part-Time, In-State Residency taking " + creditTotal + "Credits, Tuition Total is " + calcCost;
return tuitionTotal;
}
</script>
</head>
<!--Body Element-->
<body>
<!--Heading Element-->
<h1>Greendale Community College</h1>
<center><img src="greendale.jpg" alt="greendale" width="512" height="256"/></center>
<h2 align="center">Tuition Calculator</h2>
<!--Form-->
<form name="tuitionCalculator" action="" method="get">
<h2>Semester</h2>
<h3>(choose a semester)</h3>
<!--Radio Buttons to Choose Semester-->
<input type="radio" name="semesterFall" /> Fall 2018 <br />
<input type="radio" name="semesterSpring" /> Spring 2018 <br />
<input type="radio" name="semesterSummer" /> Summer 2018 <br />
<h2>Residency</h2>
<h3>(choose your residency)</h3>
<!--Radio Buttons to Choose Residency-->
<input type="radio" name="residency" value="i" onclick="calcTuition('i')"/> In-State <br />
<input type="radio" name="residency" value="o" onclick="calcTuition('o')" /> Out-of-State <br />
<input type="radio" name="residency" value="x" onclick="calcTuition('x')"/> International <br />
<h2>Credits</h2>
<h3>(enter your number of credits)</h3>
<!--Text Box-->
<input type="text" size="2" maxlength="2" name="numCredits" /><br />
<!--Button to Calculate Tuition-->
<input type="button" onclick="window.alert(calcTuition())" value="Calculate Your Tuition" />
</form>
</body>
</html>
答案 0 :(得分:1)
当您致电window.alert(calcTuition())
时,calcTuition没有参数,因此您的studentRes
变量未定义,导致未定义的费用。
答案 1 :(得分:0)
您的代码中存在一些错误
1.-您正在创建creditCost
,但之后您将值分配给完全不同的对象tuitionCostArray
,同样适用于// Created function to calculate tuition cost
var creditCostArray = new Array(3);
creditCost[0] = 302.00;
creditCost[1] = 452.00;
creditCost[2] = 588.00;
function calcTuition(studentRes)
2.- window.alert(calcTuition())
需要一个参数,而您在没有参数calcCost
3.-在你的if条件之前声明<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
// Created function to calculate tuition cost
var creditCost = new Array(3);
creditCost[0] = 302.00;
creditCost[1] = 452.00;
creditCost[2] = 588.00;
var tuitionCost = new Array(3);
tuitionCost[0] = 3619.00;
tuitionCost[1] = 5429.00;
tuitionCost[2] = 7057.00;
function calcTuition(studentRes) {
var creditTotal = document.getElementsByName("numCredits")[0].value | 0;
var calcCost = 0;
console.log(creditTotal);
if (studentRes == "i")
if (creditTotal < 12)
calcCost = creditCost[0] * parseInt(creditTotal);
tuitionTotal = "Student is an Part-Time, In-State Residency taking " + creditTotal + "Credits, Tuition Total is " + calcCost;
return tuitionTotal;
}
</script>
</head>
<!--Body Element-->
<body>
<!--Heading Element-->
<h1>Greendale Community College</h1>
<center><img src="greendale.jpg" alt="greendale" width="512" height="256"/></center>
<h2 align="center">Tuition Calculator</h2>
<!--Form-->
<form name="tuitionCalculator" action="" method="get">
<h2>Semester</h2>
<h3>(choose a semester)</h3>
<!--Radio Buttons to Choose Semester-->
<input type="radio" id="semesterFall" name="semester" /> Fall 2018 <br />
<input type="radio" id="semesterSpring" name="semester" /> Spring 2018 <br />
<input type="radio" id="semesterSummer" name="semester" /> Summer 2018 <br />
<h2>Residency</h2>
<h3>(choose your residency)</h3>
<!--Radio Buttons to Choose Residency-->
<input type="radio" name="residency" value="i" onclick="calcTuition('i')"/> In-State <br />
<input type="radio" name="residency" value="o" onclick="calcTuition('o')" /> Out-of-State <br />
<input type="radio" name="residency" value="x" onclick="calcTuition('x')"/> International <br />
<h2>Credits</h2>
<h3>(enter your number of credits)</h3>
<!--Text Box-->
<input type="text" size="2" maxlength="2" name="numCredits" /><br />
<!--Button to Calculate Tuition-->
<input type="button" onclick="window.alert(calcTuition('i'))" value="Calculate Your Tuition" />
</form>
</body>
</html>
,这样你就不会得到未定义的
工作代码看起来像这样......
#include <stdio.h>
#include <stdlib.h>
/*Returns an even valued ascii uppercase letter. Random */
char genEven() {
int value = (rand() % 13);
char evens[13] =
{'B','D','F','H', 'J','L','N','P','R','T','V','X','Z'};
return evens[value];
}
/** Returns an odd ascii uppercase letter. Random */
char genOdd() {
int value = (rand() % 13);
char odds[13] =
{'A', 'C', 'E', 'G', 'I', 'K', 'M', 'O', 'Q', 'S', 'U', 'W', 'Y'};
return odds[value];
}
/* Given a decimal, returns 7 bit binary representation. */
int dToB(int n) {
int remainder;
int binary = 0;
int i = 1;
while(n != 0) {
remainder = n%2;
n = n/2;
binary= binary + (remainder*i);
i = i*10;
}
return binary;
}
/*Reverses binary number for string conversion. */
int revBin(int n) {
int remainder;
int reversedNumber = 0;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
return reversedNumber;
}
/* Takes binary and converts to randomized letter combos. */
void bToS(int n) {
int i = 7;
int r = n % 10;
char tmp;
//FIX SO THAT IF A BINARY HAS A 0 in the highest order spot, print an even
while(i > 0) {
if(r == 0) {
tmp = genEven();
printf("(%c-%d)", tmp, tmp);
} else {
tmp = genOdd();
printf("(%c-%d)",tmp,tmp);
}
n = n/10;
r = n%10;
i--;
}
}
/* Discards to EOL. */
void discardToEOL() {
char ch;
do {
scanf("%c", &ch);
} while(ch != '\n');
}
/* Encodes text.*/
int encode(void) {
char ch;
printf("? ");
discardToEOL(); /*Discards any newlines from previous. */
/*Get Input */
do {
scanf("%c", &ch);
if (ch >= 0 && ch <= 128 && ch != 10) {
printf("\nBinary rep of %c= %d\n", ch, dToB(ch));
bToS(revBin(dToB(ch))); //Decimal to bin-> binary to string
}
} while(ch != '\n');
printf("\n");
}
/** given a binary number, convert to decimal. */
int binToDec(int binary)
{
int decimal = 0, i = 1, remainder;
/* Iterate until number becomes zero */
while (binary != 0)
{
remainder = binary % 10;
decimal = decimal + remainder * i;
i = i * 2;
binary = binary / 10;
}
printf("%c", decimal);
}
/* Decodes text.*/
int decode(void) {
char ch;
printf("? ");
int i = 0;
int bin = 0;
discardToEOL(); /*Discards any newlines from previous. */
/*Get Input */
do {
scanf("%c", &ch);
if (ch >= 0 && ch <= 128 && ch != 10) {
if(i <= 7) {
if(ch % 2 == 0) {
//Add 0 to end
bin = bin * 10; //FIX
} else {
//Add 1 to end
bin = (bin*10) +1; //FIX
}
i++;
}
if(i == 7) {
//Send in for decimal conversion
binToDec(bin);
//Reset binary number
bin = 0;
i = 0;
}
}
} while(ch != '\n');
printf("\n");
}
/* Main */
int main(void) {
int c = 1;
printf("C/c : Encode\nD/d : Decode\nQ/q : Quit\n");
while(c) {
printf("> ");
char choice;
scanf(" %c", &choice);
switch(choice) {
case 'c':
encode();
break;
case 'C':
encode();
break;
case 'd':
decode();
break;
case 'D':
decode();
break;
case 'q':
c = 0;
break;
case 'Q':
c = 0;
break;
default:
printf("Invalid Input.\n");
break;
}
}
}