我们如何更改代码以选择表格中以字母开头的单词" O"

时间:2015-12-21 17:54:38

标签: javascript html

您能帮助我以下列方式更改下一个代码吗?我们需要选择表中以字母" O"开头的单词。现在我们已经选择了长度为3的单词。

var array_word = ['рама','облако','Рим','орел','крик','олень','титр','тигр','олово'];
var array_symbol = ['т','и','р'];

function getRandom(min,max){
    return Math.floor(Math.random()*(max-min+1))+min;
}

function showTable(count_rows, count_cols) {
    var htmlcode = '';
    ROWS_SIZE = count_rows;
    COLS_SIZE = count_cols;
    for (var rows = 0; rows < count_rows; rows++) {
        htmlcode += '<tr>\n';
        for (var cols = 0; cols < count_cols; cols++) {
            htmlcode += '<td id="td' + rows + cols + '">' + array_word[getRandom(0,array_word.length-1)] + '</td>'
        }
        htmlcode += '</tr>\n';
    }
    document.getElementById("main_table").innerHTML = htmlcode;
}

function toMark() {                    
    for (var rows = 0; rows < ROWS_SIZE; rows++) {
        for (var cols = 0; cols < COLS_SIZE; cols++) {
            if (checkWord(document.getElementById("main_table").rows[rows].cells[cols].innerHTML)) {
                var elem = document.getElementById("td" + rows + cols);
                elem.setAttribute("class","selected");
            }
        }
    }
}

function checkWord(word) {
    if (word.length == 3) {
         return true;
            } else {
        return false; 
    }
}
</script>
</head>
<body onload="showTable(10,10)">
<div align="center">
    <table id="main_table">
    </table>
    <p>
    <input type="button" value="Выделить" onclick="toMark()">
    <input type="button" value="Генерировать" onclick="showTable(10,10)">
</div>

2 个答案:

答案 0 :(得分:0)

更改checkWord功能如下...

function checkWord(word) {
    if (word[0].toLowerCase() == 'o') {
       return true;
    } else {
       return false; 
    }
}

答案 1 :(得分:0)

我对您的代码的主要问题是为该语言设置的不同类型。您的о与拉丁语o不同。

var array_word = ['рама','облако','Рим','орел','крик','олень','титр','тигр','олово'];
var array_symbol = ['т','и','р'];
 
function getRandom(min,max){
    return Math.floor(Math.random()*(max-min+1))+min;
}
 
function showTable(count_rows, count_cols) {
    var htmlcode = '';
    ROWS_SIZE = count_rows;
    COLS_SIZE = count_cols;
    for (var rows = 0; rows < count_rows; rows++) {
        htmlcode += '<tr>\n';
        for (var cols = 0; cols < count_cols; cols++) {
            htmlcode += '<td id="td' + rows + cols + '">' + array_word[getRandom(0,array_word.length-1)] + '</td>'
        }
        htmlcode += '</tr>\n';
    }
    document.getElementById("main_table").innerHTML = htmlcode;
}
 
function toMark() {                    
    for (var rows = 0; rows < ROWS_SIZE; rows++) {
        for (var cols = 0; cols < COLS_SIZE; cols++) {
            if (checkWord(document.getElementById("main_table").rows[rows].cells[cols].innerHTML)) {
                var elem = document.getElementById("td" + rows + cols);
                elem.setAttribute("class","selected");
            }
        }
    }
}
 
function checkWord(word) {
    return word[0].toLowerCase() === 'о';
}
<style>
    .selected { background-color: #ffff00; }
</style>
<body onload="showTable(10,10)">
<div align="center">
    <table id="main_table">
    </table>
    <p>
    <input type="button" value="Выделить" onclick="toMark()">
    <input type="button" value="Генерировать" onclick="showTable(10,10)">
</div>