【问题标题】:What extra step am I missing when trying to pass a function as a parameter?尝试将函数作为参数传递时,我缺少什么额外的步骤?
【发布时间】:2020-06-07 02:44:51
【问题描述】:

我正在尝试将函数 dealIt() 插入到函数 dealFlop() 中,这样我就可以在 dealIt() 函数中访问 shuffle 函数。当我尝试在

的 dealFlop() 函数中使用 shuffle 时,我目前收到错误未定义

让 freshDeck_04 = shuffle(loadedDeck_03)

;任何帮助都会很棒,谢谢。

注意:点击:点击按钮发牌,然后翻牌

function begin() {
  document.querySelector(".introScreen").style = "transition: transform 3s;transform:matrix3d(1,0,0,0,-1.8,1,0,0,0,0,1,0,3000,3000,0,1)"
}
let p1Pot = 5+5;
let p2Pot = 6+6;
let p3Pot = 7+7;
let p4Pot = 8+8;


let playerone = document.querySelector(".dealItP1");
let playertwo = document.querySelector(".dealItP2");
let playerthree = document.querySelector(".dealItP3");
let playerfour = document.querySelector(".dealItP4");
let getP1Pot = document.querySelector(".P1").children[0];
let getP2Pot = document.querySelector(".P2").children[1];
let getP3Pot = document.querySelector(".P3").children[0];
let getP4Pot = document.querySelector(".P4").children[0];
let getFlopInterface = document.querySelector(".flopIt");
let getTurnInterface = document.querySelector(".turnIt");
let getRiverInterface = document.querySelector(".riverIt");


//for loop for bet loop 
//let a = for(i=0; thisFunction(); i++){};

let deck = ["2 Club", "2 Spade", "2 Diamond", "2 Heart", "3 Club", "3 Spade", "3 Diamond", "3 Heart", "4 Club", "4 Spade", "4 Diamond", "4 Heart", "5 Club", "5 Spade", "5 Diamond", "5 Heart", "6 Club", "6 Spade", "6 Diamond", "6 Heart", "7 Club", "7 Spade", "7 Diamond", "7 Heart", "8 Club", "8 Spade", "8 Diamond", "8 Heart", "9 Club", "9 Spade", "9 Diamond", "9 Heart", "10 Club", "10 Spade", "10 Diamond", "10 Heart", "Jack Club", "Jack Spade", "Jack Diamond", "Jack Heart", "Queen Club", "Queen Spade", "Queen Diamond", "Queen Heart", "King Club", "King Spade", "King Diamond", "King Heart", "Ace Club", "Ace Spade", "Ace Diamond", "Ace Heart"];


function dealIt() {
function shuffle(array) {
    var m = array.length,
      t, i;

    // While there remain elements to shuffle…
    while (m) {

      // Pick a remaining element…
      i = Math.floor(Math.random() * m--);

      // And swap it with the current element.
      t = array[m];
      array[m] = array[i];
      array[i] = t;
    }

    return array
  }

 
  //Player One	
  var arr = deck;
  let freshDeck_00 = shuffle(arr); //length = 52 *Working* shuffled//
  let p1Deal = freshDeck_00.filter(function (value, index, arr) {
    return index < 2;
  }); //length=2 *Working* PlayerOne delt cards//	
  let loadedDeck_00 = p1Deal.filter(x => !freshDeck_00.includes(x)).concat(freshDeck_00.filter(x => !p1Deal.includes(x)));
  playerone.innerHTML = p1Deal;

  //Player Two	
  let freshDeck_01 = shuffle(loadedDeck_00);
  let p2Deal = freshDeck_01.filter(function (value, index, arr) {
    return index < 2;
  });
  let loadedDeck_01 = p2Deal.filter(x => !freshDeck_01.includes(x)).concat(freshDeck_01.filter(x => !p2Deal.includes(x)));
  playertwo.innerHTML = p2Deal;

  //Player Three
  let freshDeck_02 = shuffle(loadedDeck_01);
  let p3Deal = freshDeck_02.filter(function (value, index, arr) {
    return index < 2;
  });
  let loadedDeck_02 = p3Deal.filter(x => !freshDeck_02.includes(x)).concat(freshDeck_02.filter(x => !p3Deal.includes(x)));
  playerthree.innerHTML = p3Deal;

  //Player Four
  let freshDeck_03 = shuffle(loadedDeck_02);
  let p4Deal = freshDeck_03.filter(function (value, index, arr) {
    return index < 2;
  });
  let loadedDeck_03 = p4Deal.filter(x => !freshDeck_03.includes(x)).concat(freshDeck_03.filter(x => !p4Deal.includes(x)));
  playerfour.innerHTML = p4Deal;
	

	



	

  //Deal Player One Through Four
  getP1Pot.innerHTML = 'User Pot:' + p1Pot;
  getP2Pot.innerHTML = 'P2 Pot:' + p2Pot;
  getP3Pot.innerHTML = 'P3 Pot:' + p3Pot;
  getP4Pot.innerHTML = 'P4 Pot:' + p4Pot;
	
	
	


}

function dealFlop(dealIt){
//The Flop
  let freshDeck_04 = shuffle(loadedDeck_03);
  let theFlop = freshDeck_04.filter(function (value, index, arr) {
    return index < 3;
  });
  let loadedDeck_04 = theFlop.filter(x => !freshDeck_04.includes(x)).concat(freshDeck_04.filter(x => !theFlop.includes(x)));
  getFlopInterface.innerHTML = theFlop;

}







function pingThisIn01() {
 let fold = [false];
  let call = [true];
  let raise_01 = [true, 20];
  let raise_02 = [true, 50];
  let dealBets = {
    fold: fold.valueOf(),
    call: call.valueOf(),
    raiseFifty: raise_02.valueOf(),
    raiseTwenty: raise_01.valueOf()
  };
  document.querySelector(".P2").innerHTML = dealBets.call + dealBets.raiseTwenty;
  document.querySelector(".P3").innerHTML = dealBets.fold;
  document.querySelector(".P4").innerHTML = dealBets.raiseFifty;

 
	

}
.elTorro{
	box-sizing: border-box; border: 3px solid green; background-image: url("../images(1)/bullface.png");height: 45%;width: 45%;position: absolute; top: 45%;left: 45%;background-repeat: no-repeat;
	
}
.main{
	z-index: -1;
	box-sizing: border-box;
	border: 3px solid green;
	height: 1000px;
	width: 1000px;
	position: absolute;
	background-color: black;
	opacity: 90%;
	
	
}
.introScreen{box-sizing: border-box;
					border: 3px solid green;
					height: 200px;
					width: 200px;
					background-color: yellow;}
	
.title{
	box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 200px;
	position: absolute;
	top: 10%;
	left: 50%;
	background-color: green;
	opacity:10%0;
	font-family: coniferous, sans-serif;
	font-style: normal;
	font-weight: 300;
}
.P1{
	box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 30%;
	left: 45%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;
	z-index: 1;
}

.p1BetForm{
	box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 200px;
	position: absolute;
	top: 30%;
	left: 65%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;
}
.playersBetForm{box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 200px;
	position: absolute;
	top: 45%;
	left: 45%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}


.P2{
	box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 45%;
	left: 10%;
	background-color: green;
	opacity:100%;
	font-size: 1.5em;
	color: red;
	z-index: 1;
}
.P3{
	box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 60%;
	left: 45%;
	background-color: green;
	opacity: 100%;
	font-size:1.5em;
	color: red;
	z-index: 1;
}
.P4{
	box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 45%;
	left: 80%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;
	z-index: 1;
}

.dealBet{box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 75%;
	left: 5%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}

.flopIt{box-sizing: border-box;
	border: 3px green solid;
	height: 300px;
	width: 100px;
	position: absolute;
	top: 55%;
	left: 20%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}

.flopBet{box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 75%;
	left: 35%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}

.turnIt{box-sizing: border-box;
	border: 3px green solid;
	height: 300px;
	width: 100px;
	position: absolute;
	top: 55%;
	left: 35%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}

.turnBet{box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 75%;
	left: 50%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}


.riverIt{box-sizing: border-box;
	border: 3px green solid;
	height: 300px;
	width: 100px;
	position: absolute;
	top: 55%;
	left: 65%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}

.riverBet{box-sizing: border-box;
	border: 3px green solid;
	height: 100px;
	width: 100px;
	position: absolute;
	top: 75%;
	left: 80%;
	background-color: green;
	opacity: 100%;
	font-size: 1.5em;
	color: red;}
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<link href="../CSS/pokerTryOne.css" rel="stylesheet" type="text/css">
</head>

<body>
	
	<div class="main"><div class="elTorro"></div>
		<!--<div class="output02" style="z-index: 1; box-sizing: border-box; border: 7px dashed orange;height: 200px; width: 225px; position: absolute; top: 35%; left: 35%;font-size: 5em;color:darkred;background-color: white;"></div>
	<div class="output" style="z-index: 1; box-sizing: border-box; border: 7px dashed orange;height: 200px; width: 225px; position: absolute; top: 75%; left: 35%;font-size: 5em;color:darkred;background-color: white;"></div>
	<input style="box-sizing: border-box; 4px solid green;height: 50px; width: 600px; position: absolute; top:10%; left:10%; z-index: 1; background-color: white; color: darkred;" type="range" min="1" max="100" value="50" id="slider01">-->

	<div class="introScreen" >
		
	  <button onClick="begin()"> Hello Please Begin</button><p>Here is my video:</p>
		
    <div style="position: absolute; box-sizing: border-box; border: 7px solid green; height: 2000px; width: 2000px; top: 10%; left: 90%;"><img SRC="../images(1)/PortalHuman01Gif.gif"></div>

    </div>
	
	
	<header><h1 class="title">Buttons:<button onClick="dealIt()">Click Button to Deal</button></h1>
			<h1> <button onClick="dealFlop()">Flop</button></h1>
			<h1> <button onClick="dealTurn()">Turn</button></h1>
			<h1> <button onClick="dealRiver()">River</button></h1>
		</header>
	
			<div class="P1"><p>Pot:</p><div class="dealItP1">User</div>
		
			<form id="playerOneForm" action="pokerTryOne.php" method="post">
				
				<label>label:01</label>
				<input id="label01" name="label01" type="range" value="#" max="#" min="#">
				
				</form>
		

		
			</div>
		
				
		
		
		
		
		
			<div class="P2"><button onClick="pingThisIn01()">onclick</button><p>Pot:</p><div class="dealItP2">P2</div></div><div class="playersBetForm">Plaayers Bet form</div>
		
			<div class="P3"><p>Pot:</p><div class="dealItP3">P3</div></div>
		
		
			<div class="P4"><p>Pot:</p><div class="dealItP4">P4</div></div>
		
		
		<div class="dealBet">deal Bet</div>
		<div class="flopIt">flop It</div>
		<div class="flopBet">flop bet</div>
		<div class="turnIt">turn It</div>
		<div class="turnBet">turn Bet</div>
		<div class="riverIt">river It</div>
		<div class="riverBet">river Bet</div>	
	</div>
<script type="text/javascript" src="../JavaScript/pokerTryOne.js"></script>
	</script>

</body>
</html>

【问题讨论】:

    标签: javascript parameters


    【解决方案1】:

    你可以试试这个

    function shuffle() {
        // the shuffle function body
    }
    
    function dealIt() {
        ...
    
        let freshDeck_03 = shuffle(loadedDeck_02);
    
        ...
    
        return freshDeck_03;
    }
    
    function dealFlop(){
        //The Flop
          let freshDeck_04 = shuffle(dealIt());
          let theFlop = freshDeck_04.filter(function (value, index, arr) {
            return index < 3;
          });
          let loadedDeck_04 = theFlop.filter(x => !freshDeck_04.includes(x)).concat(freshDeck_04.filter(x => !theFlop.includes(x)));
          getFlopInterface.innerHTML = theFlop;
    
    }
    

    注意:为了简单起见,我避免了大部分函数代码,你必须把其余的。

    【讨论】:

    • 我正在尝试访问 shuffle 函数,我不想回忆整个 dealIt() 函数,我只想访问内容。那有意义吗?我按照您的建议做了,但我仍然收到未定义 shuffle 的错误。
    • 让 dealItFunction = function() { function shuffle(array) {...
    • 函数 dealFlop(dealItFunction){ dealItFunction; //Flop let freshDeck_04 = shuffle(loadedDeck_03);...
    • 但是,你为什么不把功能分开呢?只需在 dealIt 之外定义 shuffle 函数,然后您可以在定义后从任何地方调用它。
    • 因为我仍然需要访问该 dealIt 函数中的其他内容。像:loadedDeck_03。如果我以正确的方式传递它,你知道为什么我仍然会收到错误吗?
    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    相关资源
    最近更新 更多