我试图在每次运行difficultyEasy函数时创建一个新单词,函数randomisePosition主要是在我的HTML中选择一个p标签,它在不同的地方显示单词。 我需要它每次在难度运行时选择一个不同的p标签
问题是,当我调用函数时,如何让randomisePosition函数不在数组中选择相同的索引
HTML ```
<div class="ag-tetris">
<p id="anagram1"></p>
<p id="anagram2"></p>
<p id="anagram3"></p>
<p id="anagram4"></p>
<p id="anagram5"></p>
<p id="anagram6"></p>
<p id="anagram7"></p>
<p id="anagram8"></p>
<p id="anagram9"></p>
<p id="anagram10"></p>
</div>``
Javascript(jQuery) -
console.log('JS Loaded');
$(() => {
let interval;
const $playGame = $('#play');
const $inputByUser = $('#answer');
const $subjectOptions = $('#subject-options');
const $difficultyOptions = $('#difficulty-options');
const $anagram1 = $('#anagram1');
const $anagram2 = $('#anagram2');
const $anagram3 = $('#anagram3');
const $anagram4 = $('#anagram4');
const $anagram5 = $('#anagram5');
const $anagram6 = $('#anagram6');
const $anagram7 = $('#anagram7');
const $anagram8 = $('#anagram8');
const $anagram9 = $('#anagram9');
const $anagram10 = $('#anagram10');
const $anagrams = [$anagram1, $anagram2,$anagram3,$anagram4,$anagram5,$anagram6,$anagram7,$anagram8,$anagram9,$anagram10];
const categories = {
BBT: ['sheldon','leanord','spock','cheesecakefactory','howard','startrek','penny','koothrapoli','bernadette','physics','laundry','halonight','dumplings', 'brisket','nasa','stringtheory','darkmatter', 'comiccon','comicbook'],
LOTR: ['arragon', 'gimli']
}
////////////////////Selectors///////////////////////////
let selectedDifficulty = '';
let selectedSubject = '';
$subjectOptions.on('change', () => {
const value = $subjectOptions.val();
selectedSubject = categories[value];
});
$difficultyOptions.on('change', () => {
selectedDifficulty = $difficultyOptions.val();
});
////////////////////Selectors///////////////////////////
/// Selects a random position (p -tag from the DOM) and stores it inside selectedAnagram
function generateWord() {
const selectedWord = selectedSubject[Math.floor(Math.random() * selectedSubject.length)];
return selectedWord;
}
function generateAnagram(selectedWord) {
return selectedWord.split('').sort(function(){
return 0.5 - Math.random();
} ).join('');
}
function randomisePosition () {
$anagrams[Math.floor((Math.random() * 10) + 1)];
}
function difficultyEasy (selectedAnagram){
const selectedAnagram = randomisePosition();
const selectedWord = generateWord();
const scrambledWord = generateAnagram(selectedWord);
selectedAnagram.text(scrambledWord);
selectedAnagram.attr('data-word', selectedWord);
interval = setInterval(function() {
selectedAnagram.css('top', '+=10px');
},2000);
}
//current score of the player
let lifeScore = 5;
let currentScore = 0;
//code validation of anagram
/// function that stops and animates a correctly guesses answer
//////////////////////////WORKING CODE//////////////////////////////////////
$('#submit').on('click', function () {
const selectedWord = selectedAnagram.attr('data-word');
if($('#answer').val() === selectedWord){
selectedAnagram.text(selectedWord).css('display','hidden');
console.log(currentScore = currentScore + selectedWord.length);
selectedAnagram.addClass('animated fadeOutLeft').css('color','green');
} else {
selectedAnagram.addClass('animated wobble').css('color','red');
//This needs to be applied when a word gets to a certain point// console.log( lifeScore = lifeScore -1);
}
}
);
$playGame.on('click', function(){
setInterval(function(){
difficultyEasy(selectedAnagram);
},2000);
});