简体   繁体   English

我这里的代码有什么问题

[英]what's wrong with my code here

Can somebody help me with this.有人可以帮我解决这个问题。 Here I have a set of 3 different numbers as strings that I get from a user and I also have another 3 randomly generated numbers that I want to compare with the set of numbers that I get from a users but since I'm not gonna do any calculations with the number I didn't see any reason to convert them so I'm trying to compare them as strings在这里,我有一组 3 个不同的数字作为我从用户那里获得的字符串,我还有另外 3 个随机生成的数字,我想将它们与我从用户那里获得的一组数字进行比较,但因为我不会这样做任何带有数字的计算我没有看到任何转换它们的理由,所以我试图将它们作为字符串进行比较

function radGenerator(userno1,userno2,userno3,wnum1, wnum2, wnum3,dvresult) {
  $(wnum1).text("" + Math.floor((Math.random() * 10) + 1));
  $(wnum2).text("" + Math.floor((Math.random() * 10) + 1));
  $(wnum3).text("" + Math.floor((Math.random() * 10) + 1));

  var num1 = num2 = num3 = "";
  if($(userno1).text()== $(wnum1).text()||
     $(userno1).text()== $(wnum2).text()||
     $(userno1).text()== $(wnum3).text()){
    num1 = $(userno1).text();
  }
  if($(userno2).text()== $(wnum1).text()||
     $(userno2).text()== $(wnum2).text()||
     $(userno2).text()== $(wnum3).text()){
    num2 = $(userno2).text();
  }

  if($(userno3).text()== $(wnum1).text()||
     $(userno3).text()== $(wnum2).text()||
     $(userno3).text()== $(wnum3).text()){
    num3 = $(userno3).text();
  }

  $(dvresult).text("Winning Numbers: " + num1 +" "+ " "+ num2 + " "+ num3);

}
radGenerator("#uans1","#uans2","#uans3","#wno1", "#wno2", "#wno3","#divresult");

In order to compare strings in JavaScript, you should the localeCompare() function.为了在 JavaScript 中比较字符串,您应该使用 localeCompare() 函数。 "string".localeCompare("otherString"); will return 1 because "string" comes after "otherString".将返回 1,因为“string”在“otherString”之后。 The function would return 0 if they are equal and -1 if the first string comes before the string you are comparing it to.如果它们相等,该函数将返回 0,如果第一个字符串出现在您要与之比较的字符串之前,则该函数将返回 -1。

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

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