简体   繁体   English

Javascript-如何在一个6个字母长的单词中混合字母并将每个字母放入特定的按钮

[英]Javascript-how to mix letters in a 6 letter-long word and put every letter to a specific button

I am a very beginner in Javascript / Jquery and my task is to make a "guess the word game" 我是Javascript / Jquery的初学者,我的任务是制作一个“猜词游戏”

I have no idea how to mix letters in a word and then how to put one letter of a word per button. 我不知道如何在一个单词中混合字母,然后如何在每个按钮中放入一个单词。

I have an array of words which length is 6 letters. 我有一个单词数组,长度为6个字母。 When user clicks "start" button a random word of the whole array is being taken. 当用户单击“开始”按钮时,整个数组中的一个随机单词将被使用。 This is the code of it : 这是它的代码:

var words = ["abacus", ".....", "zygote"]
$( "#start" ).on('click', function() {
var rand = Math.floor( Math.random() * words.length );
alert(words[rand]);
});

This is what I've got so far. 这就是到目前为止。 Alert is just to check if the code is right. 警报只是检查代码是否正确。 So the actual task for me is to mix characters in the word I've got and then spread it for 6 buttons. 因此,对我来说,实际的任务是在我所得到的单词中混合字符,然后将其扩展为6个按钮。

I am not asking for a code, just for possible solutions but anything would be appreciated Thanks in advance, 我不是在要求代码,只是在寻求可能的解决方案,但我们将不胜感激谢谢,

Shuffle the letters is described in this post: How do I shuffle the characters in a string in JavaScript? 这篇文章中介绍了随机排列字母: 如何在JavaScript中随机排列字符串中的字符?

Then iterate through all the shuffled letters with a for loop and create a button for each letter. 然后使用for循环遍历所有混排的字母,并为每个字母创建一个按钮。

I have created an example for you at http://jsfiddle.net/Mz39e/ 我在http://jsfiddle.net/Mz39e/为您创建了一个示例

var word = "myword";
var shuffledWord = word.shuffle();

for (var i = 0; i < shuffledWord.length; i++ ) {

  createButton(shuffledWord[i]);

}


function createButton(letter) {

    $("body").append($("<button/>").html(letter).on("click", function() {

         // Here your button handling code
         alert($(this).html());

    }))    

}

You can look into the String.prototype.split() function, which you can use to explode that word into an array of 6 letters, and then you can look into the Array.prototype.sort() function, which you can use a random operation inside of to randomize those letters, and then you can just iterate over the array of randomized letters to assign each letter to a different button. 您可以查看String.prototype.split()函数,该函数可用于将该单词分解为6个字母的数组,然后可以查看Array.prototype.sort()函数,您可以使用进行随机操作以随机化这些字母,然后您可以遍历随机字母数组以将每个字母分配给不同的按钮。 Something like this: 像这样:

var scrambledWord = words[rand].split('').sort(function () {
    var random = Math.random() * 2;
    if (random < 1) {
        return 1;
    } else if (random > 1) {
        return -1;
    } else {
        return 0;
    }
});

To assign each letter to a different button, you can use a library like jQuery to just create a new button as you loop over the array items, and inject them into a container. 要将每个字母分配给一个不同的按钮,您可以使用jQuery之类的库,只需在遍历数组项并将它们注入到容器中时创建一个新按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM