简体   繁体   English

根据任务编号将单词动态排列成数组

[英]Dynamically arrange words into array based on task number

I'm making a language learning RPG. 我正在制作语言学习RPG。 A task will be for a user to select words from a word bank that get placed into a text box. 用户的任务是从单词库中选择放置在文本框中的单词。 Once they submit the words, I want to parse the string to check the word order to make sure they have constructed the sentence in a grammatical way. 他们提交单词后,我想解析字符串以检查单词顺序,以确保他们以语法方式构造了该句子。

在此处输入图片说明

Originally, I was going to hardcode each sentence per task. 最初,我将对每个任务的每个句子进行硬编码。 But I would prefer to have a database full of single words... and based on the current task, the task would dictate how the words should be loaded into the array to be compared against the user's input. 但是我更希望有一个充满单词的数据库...并且根据当前任务,该任务将决定如何将单词加载到数组中以与用户输入进行比较。

For example: 例如:

Task 1: User needs to type in "Ni Hao Wo Shi." 任务1:用户需要输入“您好我要”。 The task # will do something like wordOrder.push("Ni", "Hao", "Wo", "Shi") from the word bank (which is an object words that stores all words in an object). 任务#将像做wordOrder.push("Ni", "Hao", "Wo", "Shi")从词库(这是一个对象words ,存储所有单词的对象)。

Task 2: User types in "Wo Shi Dan." 任务2:用户输入“ Wo Shi Dan”。 Again, the task # this time will tell it to clear the array, and load it as wordOrder.push("Wo", "Shi", "Dan") ... 再次,任务#这次将告诉它清除数组,并将其加载为wordOrder.push("Wo", "Shi", "Dan") ...

But I also don't want to have a bunch of if else blocks that hardcode which words to push into the array, because then I might as well hardcode the sentences. 但是我也不想让一堆if else块硬编码将哪些单词推入数组,因为那样的话我也可以硬编码这些句子。 I would rather they dynamically be pulled from the words object and put in order like 我宁愿将它们从words object中动态地拉出并按顺序排列

Task 1: wordOrder(0, 2, 1, 3) 任务1: wordOrder(0,2,1,3)

Task 2: wordOrder(1, 2, 3) 任务2: wordOrder(1、2、3)

Javascript: Javascript:

var words = {
    "task1" :
    {
        'Ni'    : 'you',
        'Wo'    : 'I',
        'Hao'   : 'good',
        'Shi'   : 'am'  
    },
    "task2" :
    {
        'Xie Xie' : 'Thanks',
        'Bu' : 'No',
        'Ke Qi' : 'Manners'
    }
}

/*Check player sentence input to see if grammar is correct*/
function submitMe() {
    var input = document.getElementById('textBox').value;
    //if quest = 1, the words in the word bank should be checked in the order specified by quest
    //compare input against word order of that quest
   if ($.trim(input) == getWordOrder(currentTask)) {
        alert("You are correct");
    }
    else {
        alert("Please try again");
    }
}

HTML: HTML:

<input type="text" id="textBox"  value="" />
<br/>
<button onclick="submitMe()" id="testButton" >Submit Response </button>
<br/>

Eventually these words will be loaded into a database. 最终,这些单词将被加载到数据库中。 I'm just testing a concept now. 我现在正在测试一个概念。 Any guidance on this would be helpful. 关于此的任何指导将是有帮助的。

Thanks! 谢谢!

Here are my changes / updates that hopefully helped: 这是希望对我有所帮助的更改/更新:

html: added this div for the word bank below the submit button html:在“提交”按钮下方为单词库添加了该div

<div id="words"></div>

javascript: javascript:

var currentTask = 1,
    numberOfTasks = 0;

// assuming "Ni", "Hao", "Wo", "Shi" is the correct phrasing for task 1:
var words = {
    "task1" :
    {
        'Ni'    : 'you',
        'Hao'   : 'good',
        'Wo'    : 'I',
        'Shi'   : 'am'  
    },
    "task2" :
    {
        'Xie Xie' : 'Thanks',
        'Bu' : 'No',
        'Ke Qi' : 'Manners'
    }
}
// get number of tasks in words object
for (var key in words) {
    numberOfTasks++;
}

/*Check player sentence input to see if grammar is correct*/
submitMe = function() {
    var input = document.getElementById('textBox').value;
    var correctOrder = getWordOrder().join(' ').toLowerCase();
    if ($.trim(input.toLowerCase()) == correctOrder) {
        alert("You are correct");
        $('#textBox').val(''); // from http://stackoverflow.com/questions/7174265/reset-textbox-value-in-javascript
        currentTask++;
        loadNextLevel();
    }
    else {
        alert("Please try again");
    }
}

loadNextLevel = function() {
    $('#words').empty();
    if (currentTask <= numberOfTasks) {
        var taskNum = "task" + currentTask,
            wordList = [];
        for (var word in words[taskNum]) {
            var temp = {};
            temp[word] = words[taskNum][word];
            wordList.push(temp);
        }
        wordList = shuffle(wordList);
        for (var i = 0; i < wordList.length; i++) {
            for (var w in wordList[i]) {
                $('#words').append("<div class='word' onclick='addText(this)'>"+w+" : "+wordList[i][w]+"</div>");
            }
        }
    } else {
        alert('you win! a.k.a. there are no more tasks to complete');
    }
}

addText = function(me) {
    var chineseWord = $.trim(me.innerHTML.split(':')[0]);
    var textElement = document.getElementById('textBox');
    if (textElement.value.length > 0) {
        textElement.value += " ";
    }
    textElement.value += chineseWord;
}

getWordOrder = function() {
    var taskNum = "task" + currentTask,
        order = [];
    for (var word in words[taskNum]) {
        order.push(word);
    }
    return order;
}

shuffle = function(o){ //v1.0
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

loadNextLevel();

css I added to make the word bank look better: 我添加的CSS使单词bank看起来更好:

.word {
    border-style: solid;
    border-width:2px;
    margin: 5px;
    padding: 2px;
    float:left;
}

.word:active {
    background-color: green;
}

All put together here! 全部放到这里! Let me know whatcha think! 让我知道您的想法!

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

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