当我添加第二个输入时,它在警告框

时间:2017-04-03 09:37:03

标签: javascript ide

不确定错误是否在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);

1 个答案:

答案 0 :(得分:0)

您正在将字符串初始化为&#39;&#39;在每次调用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);