比较两个字符串并返回它们之间的共同字符?

时间:2018-03-05 04:08:34

标签: javascript string function variables dom

我从这开始:

var goalWord = ""; // this and userWord are populated with words from an array I haven't listed here

var userWord = "";

function countCorrectCharacters(userWord, goalWord){
  var compareNum = 0;
    var common = document.createElement('common');
    div.innerHTML = "testing";
    common.style.color = 'red';
    common.setAttribute('class', 'myclass');
    document.bottom-screen.appendChild(common);
    document.body.appendChild(common);
  var check = Math.min(userWord.length, goalWord.length);
  for (var i = 0; i < check; i++) {
    if (userWord.charAt(i) == goalWord.charAt(i))
    compareNum++;
 }
 return compareNum;
}

countCorrectCharacters(userWord, goalWord);

但是对于我的生活,我不知道它是否有效。我很喜欢操纵DOM元素,所以这可能是问题,但我不确定。

1 个答案:

答案 0 :(得分:1)

计算两个字符串之间的公共字符的函数有效。你的DOM操作不起作用。你应该将这些事情分开,因为它们是不同的问题。

<?php
/* Set e-mail recipient */
$myemail  = "review@studios.com";

/* Check all form inputs using check_input function */
$yourname = check_input($_POST['yourname'], "Enter your name");
$email    = check_input($_POST['email']);
$comments = check_input($_POST['comments'], "Write your comments");
$subject  = check_input($_POST['subject'], "Write a subject");
$how_find = check_input($_POST['how']);
$comments = check_input($_POST['comments'], "Write your comments");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("Please enter valid email address");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

if(isset($_POST['submit'] && $human == '15')

else if(isset($_POST['submit'] && $human != '15')   

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your request has been submitted to the 9haus graphics team:

Name: $yourname
E-mail: $email
URL: $website

How did he/she find it? $how_find

Comments:
$comments

End of message
";

/* Send the message using mail() function */
mail($myemail, $subject, $message);


/* Functions we used */
function check_input($data, $problem='')
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    if ($problem && strlen($data) == 0)
    {
        show_error($problem);
    }
    return $data;
}

function show_error($myError)
{
?>
    <html>
    <body>

    <b>Please correct the following error:</b><br />
    <?php echo $myError; ?>

    </body>
    </html>
<?php
exit();
}
?>