Javascript 具有对象的最小和最大函数

Javascript 具有对象的最小和最大函数,javascript,processing,p5.js,Javascript,Processing,P5.js,亲爱的每个人这是我在这里的第一篇文章,我只是最近才开始尝试编码,所以请友好 我目前正在学习一门课程,在这门课程上,他们给了我们一些任务。我当前的任务有问题 我将在下面发布我当前尝试的全部代码。我觉得我已经尝试了各种合乎逻辑的方法,但它总是以失败告终 任何帮助都会非常非常感激 /* Officer: 1585754 CaseNum: 303-2-49385492-1585754 Case 303 - The Case of the Crooked Attorney Stage 3 - The

亲爱的每个人这是我在这里的第一篇文章,我只是最近才开始尝试编码,所以请友好

我目前正在学习一门课程,在这门课程上,他们给了我们一些任务。我当前的任务有问题

我将在下面发布我当前尝试的全部代码。我觉得我已经尝试了各种合乎逻辑的方法,但它总是以失败告终

任何帮助都会非常非常感激

/*

Officer: 1585754
CaseNum: 303-2-49385492-1585754

Case 303 - The Case of the Crooked Attorney
Stage 3 - The Gates Bank

I’ve made an appointment for you at the Gates Bank to retrieve your safe deposit box from the vault.
Actually you will break into Torvalds’ one.

Crack the safe by doing the following:

    Whilst the mouse is moving:
    - Make CrypticSafeCombination_0 equal to the value of mouseY
    - Use the 'min' function to prevent CrypticSafeCombination_0 from going above 11

    Whilst the mouse is moving:
    - Decrement CrypticSafeCombination_1 by 2
    - Use the 'max' function to prevent CrypticSafeCombination_1 from falling below 4

    Whilst the mouse is being dragged:
    - Make CrypticSafeCombination_2 equal to the value of mouseX
    - Use the 'max' function to prevent CrypticSafeCombination_2 from falling below 2

    Whilst the mouse is moving:
    - Decrement CrypticSafeCombination_3 by 1
    - Use the 'max' function to prevent CrypticSafeCombination_3 from falling below 3

    Whilst the mouse is moving:
    - Make CrypticSafeCombination_4 equal to the value of mouseX
    - Use the 'max' function to prevent CrypticSafeCombination_4 from falling below 13



This time you'll need to create the relevant event handlers yourself.

There are many possible ways of investigating this case, but you
should use ONLY the following commands:

    - The assignment operator aka. the equals sign !
    - mouseX, mouseY
    - Incrementing +=
    - Decrementing -=
    - min, max
    - constrain

*/

//declare the variables

var CrypticSafeCombination_0;
var CrypticSafeCombination_1;
var CrypticSafeCombination_2;
var CrypticSafeCombination_3;
var CrypticSafeCombination_4;


function preload()
{
    //IMAGES WILL BE LOADED HERE

}

function setup()
{
    createCanvas(512,512);

    //initialise the variables
    CrypticSafeCombination_0 = 0;
    CrypticSafeCombination_1 = 0;
    CrypticSafeCombination_2 = 0;
    CrypticSafeCombination_3 = 0;
    CrypticSafeCombination_4 = 0;

}

///////////////////EVENT HANDLERS///////////////////

//Create event handlers here to open the safe ...

**This is my current attempt**

function mouseMoved()
{

    CrypticSafeCombination_0 = mouseY;
    min = (mouseY, 11);

    CrypticSafeCombination_1 -= 2;
    max = (2, 4);

    CrypticSafeCombination_3 -=1;
    max = (1, 3);

    CrypticSafeCombination_4 = mouseX;
    max = (mouseX, 13);
}

function mouseDragged()
{

    CrypticSafeCombination_2 = mouseX;
    max = (mouseX, 2);
}


///////////////DO NOT CHANGE CODE BELOW THIS POINT///////////////////

function draw()
{

    //Draw the safe door
    background(70);
    noStroke();
    fill(29,110,6);
    rect(26,26,width-52,width-52);

    //Draw the combination dials
    push();
    translate(120,170);
    drawDial(140,CrypticSafeCombination_0, 16);
    pop();

    push();
    translate(120,380);
    drawDial(140,CrypticSafeCombination_1, 23);
    pop();

    push();
    translate(280,170);
    drawDial(140,CrypticSafeCombination_2, 19);
    pop();

    push();
    translate(280,380);
    drawDial(140,CrypticSafeCombination_3, 15);
    pop();

    //Draw the lever
    push();
    translate(width - 125,256);
    drawLever(CrypticSafeCombination_4);
    pop();


}

function drawDial(diameter,num,maxNum)
{
    //the combination lock

    var r = diameter * 0.5;
    var p = r * 0.6;

    stroke(0);
    fill(255,255,200);
    ellipse(0,0,diameter,diameter);
    fill(100);
    noStroke();
    ellipse(0,0,diameter*0.66,diameter*0.66);
    fill(150,0,0);
    triangle(
        -p * 0.4,-r-p,
        p * 0.4,-r-p,
        0,-r-p/5
    );

    noStroke();

    push();
    var inc = 360/maxNum;

    rotate(radians(-num * inc));
    for(var i = 0; i < maxNum; i++)
    {
        push();
        rotate(radians(i * inc));
        stroke(0);
        line(0,-r*0.66,0,-(r-10));
        noStroke();
        fill(0);
        text(i,0,-(r-10));
        pop();
    }

    pop();
}

function drawLever(rot)
{
    push();
    rotate(radians(-rot))
    stroke(0);
    fill(100);
    rect(-10,0,20,100);
    ellipse(0,0,50,50);
    ellipse(0,100,35,35);
    pop();
}
/*
警官:1585754
案例编号:303-2-49385492-1585754
案例303——欺诈律师案
第三阶段-盖茨银行
我已经为你在盖茨银行预约了从保险库取回你的保险箱。
事实上,你会闯入托瓦尔兹的一家。
通过执行以下操作打开保险箱:
当鼠标移动时:
-使CrypticSafeCombination_0等于mouseY的值
-使用“min”函数防止CrypticSafeCombination_0超过11
当鼠标移动时:
-将CrypticSafeCombination_1减量2
-使用“max”函数防止CrypticSafeCombination_1降到4以下
拖动鼠标时:
-使CrypticSafeCombination_2等于mouseX的值
-使用“max”函数防止CrypticSafeCombination_2降到2以下
当鼠标移动时:
-将CrypticSafeCombination_3减量1
-使用“max”函数防止CrypticSafeCombination_3降到3以下
当鼠标移动时:
-使CrypticSafeCombination_4等于mouseX的值
-使用“max”函数防止CrypticSafeCombination_4降至13以下
这次您需要自己创建相关的事件处理程序。
调查这个案子有很多可能的方法,但是你
应仅使用以下命令:
-赋值运算符aka。等号!
-摩丝,摩丝
-递增+=
-递减-=
-最小值,最大值
-约束
*/
//声明变量
var-CrypticSafeCombination_0;
var-CrypticSafeCombination_1;
var-CrypticSafeCombination_2;
var-CrypticSafeCombination_3;
var-CrypticSafeCombination_4;
函数预加载()
{
//图像将在此处加载
}
函数设置()
{
createCanvas(512512);
//初始化变量
密码安全组合_0=0;
密码安全组合_1=0;
密码安全组合_2=0;
密码安全组合_3=0;
密码安全组合_4=0;
}
///////////////////事件处理程序///////////////////
//在此处创建事件处理程序以打开保险柜。。。
**这是我目前的尝试**
函数mouseMoved()
{
CrypticSafeCombination_0=鼠标;
min=(mouseY,11);
密码安全组合_1-=2;
最大值=(2,4);
密码安全组合_3-=1;
最大值=(1,3);
CrypticSafeCombination_4=鼠标;
max=(mouseX,13);
}
函数mouseDragged()
{
CrypticSafeCombination_2=鼠标;
max=(mouseX,2);
}
///////////////请勿在此点以下更改代码///////////////////
函数绘图()
{
//打开保险箱门
背景(70);
仰泳();
填充(29110,6);
rect(26,26,width-52,width-52);
//画组合刻度盘
推();
翻译(120170);
drawDial(140,CrypticSafeCombination_0,16);
pop();
推();
翻译(120380);
drawDial(140,CrypticSafeCombination_1,23);
pop();
推();
翻译(280170);
drawDial(140,CrypticSafeCombination_2,19);
pop();
推();
翻译(280380);
drawDial(140,CrypticSafeCombination_3,15);
pop();
//拉杠杆
推();
平移(宽度-125256);
牵引杆(CrypticSafeCombination_4);
pop();
}
函数绘图刻度盘(直径、数量、最大数量)
{
//组合锁
var r=直径*0.5;
var p=r*0.6;
冲程(0);
填充(255255200);
椭圆(0,0,直径,直径);
填充(100);
仰泳();
椭圆(0,0,直径*0.66,直径*0.66);
填充(150,0,0);
三角(
-p*0.4,-r-p,
p*0.4,-r-p,
0,-r-p/5
);
仰泳();
推();
var inc=360/maxNum;
旋转(弧度(-num*inc));
对于(变量i=0;i
您误解了如何使用s。和是接收2个参数并返回1个参数的函数

e、 g.
x=min(a,b)
将参数
a
b
传递给函数
min()
min()
查找
a
b
中的最小值并返回它。返回值被分配给
x

e、 g.
y=max(y,c)
查找
y
c
中的最大值,并将其分配给
y

将其应用于您的代码:

函数mouseMoved()
{ 
//使CrypticSafeCombination_0等于mouseY的值
CrypticSafeCombination_0=鼠标;
//使用“min”函数防止CrypticSafeCombination_0超过11
CrypticSafeCombination_0=min(CrypticSafeCombination_0,11);
//将CrypticSafeCombination_1减量2
密码安全组合_1-=2;
//使用“max”函数防止CrypticSafeCombination_1降到4以下
CrypticSafeCombination_1=最大值(CrypticSafeCombination_1,4);
//将CrypticSafeCombination_3减量1
密码安全组合_3-=1;
//使用“max”函数防止CrypticSafeCombination_3降到3以下
CrypticSafeCombination_3=最大值(CrypticSafeCombination_3,3);
//使CrypticSafeCombination_4等于mouseX的值
CrypticSafeCombination_4=鼠标;