Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 加减逻辑_Javascript_Jquery_Css_Jquery Ui_Jquery Mobile - Fatal编程技术网

Javascript 加减逻辑

Javascript 加减逻辑,javascript,jquery,css,jquery-ui,jquery-mobile,Javascript,Jquery,Css,Jquery Ui,Jquery Mobile,这是我的密码 我想不出如何把逻辑改为减法 var slices = $("#slices"); var options = $("#options"); var area = $("#area"); var selected; var result; //---Array of images var pizzas = [ {image: "http://s23.postimg.org/6yojml8vb/Pizza_One.png", value: 1}, {image:

这是我的密码 我想不出如何把逻辑改为减法

   var slices = $("#slices");
var options = $("#options");
var area = $("#area");

var selected;
var result;

//---Array of images
var pizzas = [
    {image: "http://s23.postimg.org/6yojml8vb/Pizza_One.png", value: 1},
  {image: "http://s13.postimg.org/5d8zxnb2b/pizzatwo.png", value: 2},
  {image: "http://s12.postimg.org/xfsxldqyx/pizzathree.png", value: 3},
  {image: "http://s14.postimg.org/d6tdq0865/pizzafour.png", value: 4}
];
var total = pizzas.length;

//---Make boxes dragables
options.find("div").draggable();

//---When the boxes are dropped
area.droppable({

    drop: function(event, ui){

    if( Number( ui.draggable.attr("data-index") ) == result ){

        alert("correct");

    }else{

        alert("incorrect");

    }

  }

});

//---Insert random pizza slices
function insertPizzas(){

    selected = [];
  result = 0;

  //---Generate aleatory pieces
  var rand

  while(selected.length < 2){

        //---Random value
    rand = Math.floor( Math.random() * total );

    //---Sum result
    result += pizzas[rand].value;

    selected.push( rand );

  }

    //---Clear the slices
    slices.html("");

  //---Add the new slices
  selected.forEach(function(number){

    var img = $("<img/>");

    img.attr("src", pizzas[number].image);

    slices.append(img);

  });

}

insertPizzas();
var切片=$(“#切片”);
var期权=$(“#期权”);
风险值面积=$(“#面积”);
选择var;
var结果;
//---图像数组
var pizzas=[
{图像:http://s23.postimg.org/6yojml8vb/Pizza_One.png,值:1},
{图像:http://s13.postimg.org/5d8zxnb2b/pizzatwo.png,值:2},
{图像:http://s12.postimg.org/xfsxldqyx/pizzathree.png,值:3},
{图像:http://s14.postimg.org/d6tdq0865/pizzafour.png,值:4}
];
var total=比萨饼长度;
//---把箱子做成三角墙
options.find(“div”).draggable();
//---当盒子掉下来的时候
可下降面积({
drop:函数(事件、用户界面){
if(数字(ui.draggable.attr(“数据索引”))==结果){
警惕(“正确”);
}否则{
警告(“不正确”);
}
}
});
//---随机插入比萨饼片
函数insertPizzas(){
选定=[];
结果=0;
//---生成任意片段
瓦兰
while(已选择。长度<2){
//---随机值
rand=Math.floor(Math.random()*总计);
//---求和结果
结果+=比萨饼[rand]。值;
选择。推送(兰德);
}
//---清理切片
html(“”);
//---添加新的切片
已选择。forEach(函数(编号){
变量img=$(“

我如何将逻辑从加法改为减法


我的html页面的一部分也有这段代码,我正在处理新的变量名

下面是处理减法的JSFIDLE更新:

新提琴的功能:

//---Insert random pizza slices
function insertPizzas() {

    selected = [];
    result = 0;

    //---Generate aleatory pieces
    var rand = 0;

    while (selected.length < 2) {

        //---Making sure first number is greater than 0 (0 is the index so it is actually has a value of 1)
        while (selected.length === 0 && rand === 0) {
            rand = Math.floor(Math.random() * total);
            console.log('first: ' + rand);
        }

        //---Making sure second number is greater than first number
        while (selected.length === 1 && rand >= result) {
            rand = Math.floor(Math.random() * total);
            console.log('second: ' + rand);
        }

        //---Because of the number values, we can simply use the index difference as the values
        //---Of course, we have to update our code if the values are different
        if (selected.length === 0) {
            result = rand;
        } else {
            result -= rand;
        }

        selected.push(rand);

    }

    //---Clear the slices
    slices.html("");

    //---Add the new slices
    selected.forEach(function(number) {

        var img = $("<img/>");

        img.attr("src", pizzas[number].image);

        slices.append(img);

    });

}
/---插入随机比萨饼片
函数insertPizzas(){
选定=[];
结果=0;
//---生成任意片段
var-rand=0;
while(已选择。长度<2){
//---确保第一个数字大于0(0是索引,因此它的实际值为1)
while(selected.length==0&&rand==0){
rand=Math.floor(Math.random()*总计);
console.log('first:'+rand);
}
//---确保第二个数字大于第一个数字
while(selected.length==1&&rand>=result){
rand=Math.floor(Math.random()*总计);
console.log('second:'+rand);
}
//---由于是数值,我们可以简单地使用索引差作为值
//---当然,如果值不同,我们必须更新代码
如果(选定。长度===0){
结果=兰特;
}否则{
结果-=兰特;
}
选择。推送(兰德);
}
//---清理切片
html(“”);
//---添加新的切片
已选择。forEach(函数(编号){
变量img=$(“
需要注意的几件事:

  • 我们必须在第一次迭代中循环以确保第一个索引不是0,因为第一个数字减去第二个数字必须大于0,因为我们的选择是1到8
  • 我们必须在第二次迭代中循环,以确保第二个数字小于第一个数字,原因与#1相同
  • 因为我们的值都是差1,所以我们可以简单地使用索引作为差
  • 此外,我们必须将rand值初始化为0,第一个循环才能工作

  • 你的意思是要显示两幅图像,用户将图像1减去图像2,然后将数字拖到下拉区域作为答案吗?是的,先生!这就是我想要的我链接了小提琴,我的加法代码正是这样工作的我只想让减法部分工作,而你将不得不减去比萨。这很好!谢谢你,快问我,我刚刚把这个插入我的代码,看起来脚本互相干扰(加法+减法)为了让脚本独立工作,我需要更改哪些变量名?@artbarzz-更新您的小提琴,以便我们可以查看并提出建议。我希望在一个html文件中同时使用加法脚本和减法脚本,并且我希望对javascript使用相同的html调用code@artbarzz-您的意思是用一个函数来生成imag吗是的,先生,我正在使用一个名为加法的页面制作加法游戏,另一个页面制作减法游戏,两个页面都将使用此代码,但我需要更改每个页面的代码,以便逻辑正常工作