gpt4 book ai didi

javascript - 当我添加第二个输入时,它在警报框中显示两者均未定义

转载 作者:行者123 更新时间:2023-11-28 04:48:10 24 4
gpt4 key购买 nike

不确定错误是否出在 viewData() 函数中。

  var list = {
firstName: [],
lastName: [],
maximumHeartRate: [],
restingHeartRate: [],
age: [],
gender: []
}

alert("Welcome to the VO2 Max Categorisation App.");

function validate(string) {
var stringToVal = prompt(string);
var range = new RegExp("^[a-zA-Z]{2,}$");
while (range.test(stringToVal) == false || stringToVal == "" || stringToVal == null ) {
stringToVal = prompt(string);
}
return stringToVal
}

function validateNumber(numToVal, min, max){
var askUser = Number(prompt(numToVal))
while (isNaN(askUser) || askUser < min || askUser > max || askUser == '' || askUser == null) {
askUser = Number(prompt("Please enter a number between " + min + " & " + max + "." ));
}
return askUser
}

function addData(list) {
var userFirstName = validate("First name of user?");
var userLastName = validate("Last name of user?");
var userGender = validate("Gender of user?");
var userAge = validateNumber("Please enter an age between 20 - 39!",20,39);
var MHR = validateNumber("Enter maximum heart rate of user",30,200);
var RHR = validateNumber("Enter resting heart rate of user",30,200 );

list.firstName.push(userFirstName);
list.lastName.push(userLastName);
list.gender.push(userGender);
list.age.push(userAge);
list.maximumHeartRate.push(MHR);
list.restingHeartRate.push(RHR);

}

此函数询问用户并将所有内容推送到列表中

function calculateVO2Max(list) {
var VO2Max = 15.3 * (list.maximumHeartRate / list.restingHeartRate);
return VO2Max;
}

这会计算 VO2Max

function VO2MaxCategorisation(Gender,Age,VO2Max) {
if (Gender == "Female" && Age >= 20 && Age <= 29 && VO2Max < 31) {
return("Low");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && (VO2Max >= 31 && VO2Max <=34) ) {
return("Fair");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && (VO2Max >= 35 && VO2Max <= 37)) {
return("Average");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && (VO2Max >= 38 && VO2Max <= 41)) {
return("Good");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && VO2Max > 41) {
return("High");
}


if (Gender == "Female" && Age >= 30 && Age <= 39 && VO2Max < 29) {
return("Low");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && (VO2Max >= 29 && VO2Max <= 32)) {
return("Fair");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && (VO2Max >= 33 && VO2Max <= 35)) {
return("Average");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && (VO2Max >= 36 && VO2Max <= 39)) {
return("Good");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && VO2Max > 39) {
return("High");
}


if (Gender == "Male" && Age >= 20 && Age <= 29 && VO2Max < 37) {
return("Low");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && (VO2Max >= 37 && VO2Max <= 41)) {
return("Fair");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && (VO2Max >= 42 && VO2Max <= 44)) {
return("Average");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && (VO2Max >= 45 && VO2Max <= 48)) {
return("Good");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && VO2Max > 48) {
return("High");
}


if (Gender == "Male" && Age >= 30 && Age <= 39 && VO2Max < 35) {
return("Low");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && (VO2Max >= 35 && VO2Max <= 39)) {
return("Fair");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && (VO2Max >= 40 && VO2Max <= 42)) {
return("Average");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && (VO2Max >= 43 && VO2Max <= 47)) {
return("Good");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && VO2Max > 47) {
return("High");
}

}

此功能分类

function viewData(list) {
var string = ""
for(var i = 0; i < list.firstName.length; i++) {
string += (list.firstName[i] + " " + list.lastName[i] + ": " + VO2MaxCategorisation(list.gender[i],list.age[i],calculateVO2Max(list)) + "\n")
}
alert(string)
}

警告框中显示2个输入时显示未定义,但仅显示1个输入时显示用户的类别

function optionMenu(list) {
while(true) {
var optionChoice = Number(prompt("What do you want to do: \n 1)Enter data \n 2)View data \n 3)Exit"));
if (optionChoice == 1) {
addData(list);
} else if (optionChoice == 2) {
viewData(list);
} else if (optionChoice == 3) {
break;
} else {
alert("Sorry,choose from the options.");
}
}
}

这是可供选择的菜单

optionMenu(list);

最佳答案

您在每次调用 viewData 时将字符串初始化为 ''。

将字符串放在 viewData 上下文之外并使其全局。

https://jsbin.com/sukuyeguzu/edit?html,js,output

 var list = {
firstName: [],
lastName: [],
maximumHeartRate: [],
restingHeartRate: [],
age: [],
gender: []
}
var string = "";



alert("Welcome to the VO2 Max Categorisation App.");

function validate(string) {
var stringToVal = prompt(string);
var range = new RegExp("^[a-zA-Z]{2,}$");
while (range.test(stringToVal) == false || stringToVal == "" || stringToVal == null ) {
stringToVal = prompt(string);
}
return stringToVal
}

function validateNumber(numToVal, min, max){
var askUser = Number(prompt(numToVal))
while (isNaN(askUser) || askUser < min || askUser > max || askUser == '' || askUser == null) {
askUser = Number(prompt("Please enter a number between " + min + " & " + max + "." ));
}
return askUser
}

function addData(list) {
var userFirstName = validate("First name of user?");
var userLastName = validate("Last name of user?");
var userGender = validate("Gender of user?");
var userAge = validateNumber("Please enter an age between 20 - 39!",20,39);
var MHR = validateNumber("Enter maximum heart rate of user",30,200);
var RHR = validateNumber("Enter resting heart rate of user",30,200 );

list.firstName.push(userFirstName);
list.lastName.push(userLastName);
list.gender.push(userGender);
list.age.push(userAge);
list.maximumHeartRate.push(MHR);
list.restingHeartRate.push(RHR);

}

function calculateVO2Max(list) {
var VO2Max = 15.3 * (list.maximumHeartRate / list.restingHeartRate);
return VO2Max;
}

function VO2MaxCategorisation(Gender,Age,VO2Max) {
if (Gender == "Female" && Age >= 20 && Age <= 29 && VO2Max < 31) {
return("Low");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && (VO2Max >= 31 && VO2Max <=34) ) {
return("Fair");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && (VO2Max >= 35 && VO2Max <= 37)) {
return("Average");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && (VO2Max >= 38 && VO2Max <= 41)) {
return("Good");
} else if (Gender == "Female" && Age >= 20 && Age <= 29 && VO2Max > 41) {
return("High");
}


if (Gender == "Female" && Age >= 30 && Age <= 39 && VO2Max < 29) {
return("Low");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && (VO2Max >= 29 && VO2Max <= 32)) {
return("Fair");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && (VO2Max >= 33 && VO2Max <= 35)) {
return("Average");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && (VO2Max >= 36 && VO2Max <= 39)) {
return("Good");
} else if (Gender == "Female" && Age >= 30 && Age <= 39 && VO2Max > 39) {
return("High");
}


if (Gender == "Male" && Age >= 20 && Age <= 29 && VO2Max < 37) {
return("Low");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && (VO2Max >= 37 && VO2Max <= 41)) {
return("Fair");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && (VO2Max >= 42 && VO2Max <= 44)) {
return("Average");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && (VO2Max >= 45 && VO2Max <= 48)) {
return("Good");
} else if (Gender == "Male" && Age >= 20 && Age <= 29 && VO2Max > 48) {
return("High");
}


if (Gender == "Male" && Age >= 30 && Age <= 39 && VO2Max < 35) {
return("Low");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && (VO2Max >= 35 && VO2Max <= 39)) {
return("Fair");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && (VO2Max >= 40 && VO2Max <= 42)) {
return("Average");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && (VO2Max >= 43 && VO2Max <= 47)) {
return("Good");
} else if (Gender == "Male" && Age >= 30 && Age <= 39 && VO2Max > 47) {
return("High");
}

}

function viewData(list) {
for(var i = 0; i < list.firstName.length; i++) {
string += (list.firstName[i] + " " + list.lastName[i] + ": " + VO2MaxCategorisation(list.gender[i],list.age[i],calculateVO2Max(list)) + "\n")
}
alert(string)
}

function optionMenu(list) {
while(true) {
var optionChoice = Number(prompt("What do you want to do: \n 1)Enter data \n 2)View data \n 3)Exit"));
if (optionChoice == 1) {
addData(list);
viewData(list);
} else if (optionChoice == 2) {
viewData(list);
} else if (optionChoice == 3) {
break;
} else {
alert("Sorry,choose from the options.");
}
}
}

optionMenu(list);

关于javascript - 当我添加第二个输入时,它在警报框中显示两者均未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43181204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com