简体   繁体   English

JavaScript 中不匹配的相同字符串

[英]Identitical Strings that do not match in JavaScript

I'm building a sortable word quiz where the user has to put the words of a sentence in the correct order.我正在构建一个可排序的单词测验,用户必须将句子中的单词按正确的顺序排列。 The idea is that the user drags the words from below the sentence and then the word blocks light up green or red depending on if their solution matches the answer.这个想法是用户从句子下方拖动单词,然后单词块亮起绿色或红色,这取决于他们的解决方案是否与答案匹配。

I'm using jQuery and the jQuery UI sortable method.我正在使用 jQuery 和 jQuery UI 排序方法。 The jQuery sortable and HTML & CSS all works fine, but in my JavaScript if statement comparing the answer string with the users solution string is always false no matter what. jQuery sortable 和 HTML & CSS 都工作正常,但在我的 JavaScript if 语句中,将答案字符串与用户解决方案字符串进行比较,无论如何总是错误的 The really odd thing is console.log shows the strings as being identical in every way as far as I can tell.真正奇怪的是,就我所知,console.log 显示的字符串在各个方面都是相同的。 ie typeof show they are both of type "string" and length of both strings shows 31. I even added some code to chop white space off the ends of the strings to sanitize them.即 typeof 显示它们都是“字符串”类型,并且两个字符串的长度都显示为 31。我什至添加了一些代码来切掉字符串末端的空白以清理它们。 The answer is being stored in a variable called answer and the users solution is stored in a variable called solution答案存储在名为answer的变量中,用户解决方案存储在名为solution的变量

The demo code is live on codepen here:演示代码位于 codepen 上:

https://codepen.io/codepajamas/pen/zYGMveN?editors=1111 https://codepen.io/codepajamas/pen/zYGMveN?editors=1111

You can open the console to see the result.您可以打开控制台查看结果。

Following is the HTML, CSS, and JavaScript Code:以下是 HTML、CSS 和 JavaScript 代码:

<div class="sentence" data-answer="Henry wants to go to the store!">
  <span class="sentence-start">Henry</span>
  <ul class="words place-words"></ul>
  <span class="sentence-end">to the store!</span>
</div>
<ul class="words supply-words"><li class="word">wants </li><li class="word">go </li><li class="word">to </li></ul>
$(function () {

var answer = $('.sentence').data('answer'); // "Henry wants to go to the store!"

$('.words').sortable({
  connectWith: '.place-words, .supply-words',
  beforeStop: function () {

    if ($('.supply-words').text() === '') {
      var sentence_start = $('.sentence-start').text(),
          placed_words = $('.place-words').text(),
          sentence_end = $('.sentence-end').text(),
          combined = sentence_start+placed_words+sentence_end,
          // get rid of line returns and white space at beginning and end of sentence
          solution = combined.replace(/(\r\n|\n|\r)/gm,'').trim();

      if (solution === answer) {
        console.log('correct');
        $('.place-words').removeClass('wrong').addClass('correct');
      } else {
        console.log('wrong');
        $('.place-words').removeClass('correct').addClass('wrong');
      }

    }// outer if
  }// beforeStop function
 });

$('.words, .answer').disableSelection();

}; //end document ready
.sentence {
  margin: 0 0 10px 0;
}

.words {
  display: inline-block;
  margin: 0 5px 0 5px;
  padding: 0;
  width: auto;
  min-width: 135px;
  height: 30px;
  border: 1px dashed #ccc;
  vertical-align: bottom;
}

.word {
  display: inline-block;
  margin: 0;
  border: 1px solid #000000;
  padding: 5px 10px;
  cursor: pointer;
}

.correct {
  background: lime;
}

.wrong {
  background: pink;
}

Let me know if anyone has any questions.如果有人有任何问题,请告诉我。 Any help is appreciated.任何帮助表示赞赏。 I think I need a fresh set of eyes on this.我想我需要重新审视这个问题。 Thanks in advance!提前致谢!

Classic edge case of white spaces from what I can tell.据我所知,白色空间的经典边缘案例。 I was able to get your code to work by handling white space in both the provided answer and solution.通过在提供的答案和解决方案中处理空格,我能够让您的代码工作。

// add this just before you if statement.
solution = solution.replace(/ /g, "");
answer = answer.replace(/ /g,"").trim();

But it looks like you are missing the space on your first word.但看起来您缺少第一个单词上的空格。 "wants " “想要”

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

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