我需要帮助来解决一个小问题

时间:2016-04-20 01:32:41

标签: javascript

基本上我想要问题这个词,Johnny有1个镍,4个角钱和3个季度。约翰尼有多少钱? (回答:1.20美元) 但每次我改变它我的代码它都不起作用。

这是我编写的代码,需要帮助来更改单词问题。

<script>
 var person1, person2;

 function getPeople() {
     var people = ['Kritarth', 'Sharujan', 'Anveer', 'Krishna', 'Husnain', 'Iser', 'Ishan', 'Harman', 'Hemant', 'Harjot'];

     person1 = people[Math.floor(Math.random() * people.length)];
     person2 = people[Math.floor(Math.random() * people.length)];

     while (person1 === person2) {
         person2 = people[Math.floor(Math.random() * people.length)];
     }
 }

 function getrandomnumber(min, max, notin) {
     return min + Math.floor((max - min + 1) * Math.random());
 }

 function getoption(s, ch, num) {
     var a = s.split(ch);
     return a[num - 1];
 }

 var marymoney = getrandomnumber(50, 100, "");
 var johnmoney = getrandomnumber(50, 100, "");
 var maryitem = getrandomnumber(5, 20, "");
 var johnitem = getrandomnumber(5, 20, "");
 var marystuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));

 var johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));

 while (marystuff === johnstuff) {
     johnstuff = getoption("notebook,pencil,ruler,pen,eraser,binder,backpack", ",", getrandomnumber(1, 7));
 }
 getPeople();
 var totalleft = marymoney + johnmoney - maryitem - johnitem;
 var str = person1 + " has $" + marymoney + " and " + person2 + " had $" + johnmoney + ". " + person1 + " buys a " + marystuff + " for $" + maryitem + " and " + person2 + " buys a " + johnstuff + " for $" + johnitem + ". They have $" + totalleft + ".";

 document.write(str);
</script>

1 个答案:

答案 0 :(得分:1)

//定义人员和项目列表       var people = ['Kritarth','Sharujan','Anveer','Krishna','Husnain','Iser','Ishan','Harman','Hemant','Harjot'];      var items = ['notebook','pencil','ruler','pen','eraser','binder','backpack'];       var cost = [20.00,8.00,11.00,15.00,5.00,35.00,100.00];

//获取唯一且随机的名称      function getList(list,amount){       var index = 0,selected = [];

 while (amount-- > 0) {
         index = getRandom(0, list.length - 1);
     selected.push (list[index]);

     // Remove the selected member from the list
     list = list.slice(0, index).concat(list.slice(index + 1));
 }

 return selected;

}

//返回min和max之间的整数      function getRandom(min,max){      返回Math.floor(min + Math.random()*(max - min + 1));  }

//显示结果  function showResults(){    var population = 2;

var everyone = getList(人,人口);       var everything = getList(items,population);

 var person = [];

//设置每个人的姓名,项目和金钱        for(var i = 0; i&lt; population; i ++){        person [i] = {            姓名:大家[i],            item:一切[i],            成本:成本[items.indexOf(everything [i])],            钱:getRandom(50,100)        }    }

var totalleft = person [1] .money + person [0] .money - person [1] .cost - person [0] .cost;

  var str = person[1].name + " has $" + person[1].money + " and " +     person[0].name + " had $" + person[0].money + ". " +
         person[1].name + " buys a " + person[1].item + " for $" +  person[1].cost + " and " + person[0].name + " buys a " + person[0].item + " for $" + person[0].cost + ". " +
         "They have $" + totalleft + " left.";

document.getElementById('output')。innerHTML = str;   }

//最初由Misa Chan编码,由David.Refoua.me改进   showResults();