选项的JS值没有定义?

时间:2018-06-13 23:27:38

标签: javascript

我最近刚开始学习JS,就像几天前一样...... 现在我正在尝试做最大卡路里计算器的练习,这取决于年龄,工作档案和体重(千克) 在哪里可以获得工作档案和年龄的值 所以maxcalorie就像=工作*年龄*质量 对于工作因素,它说它没有定义(当我把工作因素排除在工作*年龄工作正常) 这是我的代码:

var activityFactor = '';
var ageFactor = '';
var maxCalorie = '';

function izracunaj() {
  var age = document.getElementById('age').value;
  var mass = document.getElementById('mass').value;
  var job = document.getElementById('job').value;

  maxCalorie = mass * ageFactor * activityFactor;
  if (age <= 3) {
    ageFactor = 1.9;
  } else if (age >= 4 && age <= 10) {
    ageFactor = 1.5;
  } else if (age >= 11 && age <= 18) {
    ageFactor = 1.2;
  } else if (age >= 19 && age <= 26) {
    ageFactor = 1;
  } else if (age >= 27 && age <= 30 || age >= 50 && age <= 60) {
    ageFactor = 0.8;
  } else if (age >= 31 && age <= 35 || age >= 45 && age <= 49) {
    ageFactor = 0.7;
  } else if (age >= 36 && age <= 44 || age > 60) {
    ageFactor = 0.6;
  }

  if (job === manager || job === programer || job === admin) {
    activityFactor = 100;
  } else if (job === soldier) {
    activityFactor === 200;
  } else if (job === sportsmen) {
    activityFactor === 300;
  } else if (job === lawyer || job === econom) {
    factorActivnost === 150;
  }

  document.getElementById('calories').innerText = maxCalorie;
}
<div class="container">
  <label>mass</label>
  <br>
  <input type="number" name="mass" value="" id="mass">
  <br>
  <label>age</label>
  <br>
  <input type="text" name="age" value="" id="age"><br>
  <label></label>
  <br>
  <select id="job">
    <option value="programer">Programer</option>
    <option value="admin">Administrativni radnik</option>
    <option value="manager">manager</option>
    <option value="soldier">soldier</option>
    <option value="sportsmen">sportsmen</option>
    <option value="econom">econom</option>
    <option value="lawyer">lawyer</option>
  </select>

  <p>Maximal calorie intake: <span id="calories"></span></p>

  <button onclick="izracunaj()">Get maximal calorie intake</button>
</div>

2 个答案:

答案 0 :(得分:0)

您在ageFactor区块中设置activityFactor之前使用ifmanager

您还需要引用字符串programmeradminage >= XX

您不需要在第一个if/else if块中进行所有if次测试,因为任何低于此值的值都会由之前的var activityFactor = ''; var ageFactor = ''; var maxCalorie = ''; function izracunaj() { var age = document.getElementById('age').value; var mass = document.getElementById('mass').value; var job = document.getElementById('job').value; if (age <= 3) { ageFactor = 1.9; } else if (age <= 10) { ageFactor = 1.5; } else if (age <= 18) { ageFactor = 1.2; } else if (age <= 26) { ageFactor = 1; } else if (age >= 27 && age <= 30 || age >= 50 && age <= 60) { ageFactor = 0.8; } else if (age >= 31 && age <= 35 || age >= 45 && age <= 49) { ageFactor = 0.7; } else if (age >= 36 && age <= 44 || age > 60) { ageFactor = 0.6; } if (job === 'manager' || job === 'programer' || job === 'admin') { activityFactor = 100; } else if (job === 'soldier') { activityFactor === 200; } else if (job === 'sportsmen') { activityFactor === 300; } else if (job === 'lawyer' || job === 'econom') { factorActivnost === 150; } maxCalorie = mass * ageFactor * activityFactor; document.getElementById('calories').innerText = maxCalorie; }处理。

<div class="container">
  <label>mass</label>
  <br>
  <input type="number" name="mass" value="" id="mass">
  <br>
  <label>age</label>
  <br>
  <input type="text" name="age" value="" id="age"><br>
  <label></label>
  <br>
  <select id="job">
    <option value="programer">Programer</option>
    <option value="admin">Administrativni radnik</option>
    <option value="manager">manager</option>
    <option value="soldier">soldier</option>
    <option value="sportsmen">sportsmen</option>
    <option value="econom">econom</option>
    <option value="lawyer">lawyer</option>
  </select>

  <p>Maximal calorie intake: <span id="calories"></span></p>

  <button onclick="izracunaj()">Get maximal calorie intake</button>
</div>
<img src="uploads/user_pic/<?php echo $first_name.'_'.$last_name.'/'.'profile_pic'.'/'.'profile_pic.'.'png'; ?>" class="img-thumbnail" />

答案 1 :(得分:0)

我建议每当事情不适合你时使用print语句。在这种情况下,我会做job.value来找出我正在使用的内容。我认为在进行逻辑比较时使用job代替if(job === 'manager')可以解决问题,但我可能错了。如果我不对,我会在回家后(6小时内)改变我的回复。祝你好运!

编辑经理未定义。同样,其他作业名称也未定义。确保如果您要将作业与经理进行比较,那么您需要比较字符串。例如,执行job.value()

第二次修改:我发现他确实使用了job,但正在将{{1}}与变量名称进行比较而不是字符串。