2016-03-29 6 views
0

これで、ユーザーがボタンを押したときにランダムな自己害を生成するWebサイトを作っています。私は、配列要素を表示するためにJQueryの.load()メソッドを使用していますが、選択された後に配列から要素を削除する方法が見つからないようです。私はまた、表示する代替手段がなくなったらユーザーに知らせるメッセージを表示したい。ランダム配列要素を選んで削除し、利用可能な要素がなくなったらメッセージを表示する

私は.splice()と.shift()メソッドを試しましたが、それでもまだ繰り返しが表示されているようです。私はおそらくちょうどこれらを正しく使用していないでしょう。

ご協力いただければ幸いです。

var skillList = new Array; 
 
skillList[0] = "Math.floor(Math.random() * skillListAnxious.length)"; 
 
skillList[1] = "Math.floor(Math.random() * skillListAngry.length)"; 
 
skillList[3] = "Math.floor(Math.random() * skillListSad.length)"; 
 
skillList[3] = "Math.floor(Math.random() * skillListDistract.length)"; 
 
skillList[4] = "Math.floor(Math.random() * skillListThink.length)"; 
 
skillList[5] = "Math.floor(Math.random() * skillListSensation.length)"; 
 
skillList[6] = "Math.floor(Math.random() * skillListIllusion.length)"; 
 
skillList[7] = "Math.floor(Math.random() * skillListBizzare.length)"; 
 

 
var skillListAnxious = new Array; 
 

 
var skillListSad = new Array; 
 

 
var skillListAngry = new Array; 
 
skillListAngry[0] = "skills.html #goForRun"; 
 
skillListAngry[1] = "skills.html #pillowFightWall"; 
 
skillListAngry[2] = "skills.html #breakSticks"; 
 
skillListAngry[3] = "skills.html #popBalloon"; 
 
skillListAngry[4] = "skills.html #calmJar"; 
 
skillListAngry[5] = "skills.html #scribbleonPeople"; 
 
skillListAngry[6] = "skills.html #stabFruit"; 
 
skillListAngry[7] = "skills.html #appleAgainstWall"; 
 
skillListAngry[8] = "skills.html #screamLoudly"; 
 
skillListAngry[9] = "skills.html #tearPaperObjects"; 
 
skillListAngry[10] = "skills.html #gym"; 
 
skillListAngry[11] = "skills.html #singtomusic"; 
 
skillListAngry[12] = "skills.html #pictureofangryness"; 
 
skillListAngry[13] = "skills.html #beatUpaStuffedBear"; 
 
skillListAngry[14] = "skills.html #bubblewrap"; 
 
skillListAngry[15] = "skills.html #popMultipleBalloons"; 
 
skillListAngry[16] = "skills.html #paperFillCrosshatches"; 
 
skillListAngry[17] = "skills.html #goForRun"; 
 
skillListAngry[18] = "skills.html #colourWholePaper"; 
 
skillListAngry[19] = "skills.html #throwIce"; 
 
skillListAngry[20] = "skills.html #cutSomethingElse"; 
 
skillListAngry[20] = "skills.html #cutSomethingElse"; 
 
skillListAngry[21] = "skills.html #writeDownandRip"; 
 
skillListAngry[22] = "skills.html #flattenCans"; 
 
skillListAngry[23] = "skills.html #heavyShoes"; 
 
skillListAngry[24] = "skills.html #tennis"; 
 
skillListAngry[25] = "skills.html #cutFruit"; 
 
skillListAngry[26] = "skills.html #markPicture"; 
 
skillListAngry[27] = "skills.html #slashBottle"; 
 
skillListAngry[28] = "skills.html #destroyPillows"; 
 
skillListAngry[29] = "skills.html #splatterPaint"; 
 
skillListAngry[30] = "skills.html #brushTeddyVicious"; 
 
skillListAngry[31] = "skills.html #watchFilm"; 
 

 
var skillListDistract = new Array; 
 
skillListDistract[0] = "skills.html #cupTea"; 
 
skillListDistract[1] = "skills.html #hotBath"; 
 
skillListDistract[2] = "skills.html #mopBlood"; 
 

 
var skillListThink = new Array; 
 
skillListThink[0] = "skills.html #butterflyProject"; 
 
skillListThink[1] = "skills.html #sample" 
 

 
var skillListSensation = new Array; 
 

 
var skillListIllusion = new Array; 
 

 
var skillListBizzare = new Array; 
 

 
function pick() { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillList.length); 
 
\t \t $(".resultContent").load(skillList[skillChoice]); 
 
\t if (document.filter.anxious.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListAnxious.length); 
 
\t \t $(".resultContent").load(skillListAnxious[skillChoice]); 
 
\t } 
 
\t if (document.filter.sad.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListSad.length); 
 
\t \t $(".resultContent").load(skillListSad[skillChoice]); 
 
\t } 
 
\t if (document.filter.angry.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListAngry.length); 
 
\t \t $(".resultContent").load(skillListAngry[skillChoice]); 
 
\t } 
 
\t if (document.filter.distract.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListDistract.length); 
 
\t \t $(".resultContent").load(skillListDistract[skillChoice]); 
 
\t } \t 
 
\t if (document.filter.think.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListThink.length); 
 
\t \t $(".resultContent").load(skillListThink[skillChoice]); 
 
\t } 
 
\t if (document.filter.sensation.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListSensation.length); 
 
\t \t $(".resultContent").load(skillListSensation[skillChoice]); 
 
\t } 
 
\t if (document.filter.bizzare.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListBizzare.length); 
 
\t \t $(".resultContent").load(skillListBizzare[skillChoice]); 
 
\t } 
 
\t if (document.filter.illusion.checked == true) { 
 
\t \t var skillChoice = Math.floor(Math.random() * skillListIllusion.length); 
 
\t \t $(".resultContent").load(skillListIllusion[skillChoice]); 
 
\t } 
 

 
}

+2

あなたのコードを追加してください。 –

+0

はちょうど私がちょうど要素を除去するための –

答えて

0

JavaScriptの.splice()メソッドが必要です。これは、選択した要素を返すと、すぐに元の配列から削除されます

skillListBizzare.splice(skillChoice, 1) 

:これまで

skillListBizzare[skillChoice] 

:ちょうどこのように要素を取得し、すべての通話を交換してください。

と左のいくつかの要素があるかどうかをチェックすることができ、次の行に:

if (skillListBizzare.length === 0) { 
    alert('Sorry! No skills left'); 
} 
+0

(document.filter.angry場合。真==)を{チェック \t \t VAR skillChoice = Math.floor(Math.random()* skillListAngry.length)。 \t \t $( "。resultContent")。load(skillListAngry.splice(skillChoice)、1); \t} –

+0

あなたが意味することは、今何も起こりませんか?私が間違っていると申し訳ありません –

+0

@ J.Bates。あなたは大括弧を台無しにしました。 'skillListAngry.splice(skillChoice)、1)の代わりに'それを 'skillListAngry.splice(skillChoice、1);'にしてください。注意してください –

0

あなたのコードを見なくても、私は、スプライスは、おそらくあなたが探している方法であることを知っているが、私は実際にそれがどのように助言するようにセットアップだかを確認する必要があると思いますコードを設定します。

+0

それを入れて、メッセージのヘルプのためにそれらが、感謝を入れているので、確認されませんでした:) –

0
//remove the 20th index of skillListAngry array. 
skillListAngry.splice(20,1); 

if(skillListAngry.length === 0){ 
alert('there are no more items to display'); 
} 
+0

ラインを、それをアップロードしたに:) –

0
var array = [1,2,3,4,5,6,7,8,9,10,11,12]; 

// Function to generate random numbers 
function getRandomInt(min, max) { 
    return Math.floor(Math.random() * (max - min + 1)) + min; 
} 


// Loop through the array printing and splicing random index 
while(array.length > 0) { 
    var index = getRandomInt(0, array.length-1); 
    console.log(array[index]); 
    array.splice(index, 1); 
} 

ここでうまくいけば、これはあなたにいくつかの助けであり、ランダムに要素を選択して削除する配列をループへの単純な機能ですか?

関連する問題