IndexOf bilan ikkita qatorda o'yinlarni topish

Men FreeCodeCamp mutatsion sinovlaridan o'tmoqchiman. Mana nima qilishim kerak:

qatorning birinchi elementidagi satr mavjud bo'lsa, rost rost   

qatoridagi ikkinchi elementdagi mag'lubiyatning barcha harflari      

Masalan, ["salom", "salom"], chunki hamma hammasi to'g'ri bo'lishi kerak   

Birinchi qatorda harflar ikkinchi satrda mavjud      

Argumentlar ["salom", "hey"] mag'lubiyatga qarab yolg'onni qaytarishi kerak   "Salom" "y" ni o'z ichiga olmaydi.

     

Nihoyat, ["Alien", "layn"], chunki hamma narsa to'g'ri bo'lishi kerak   "Chiziq" ichidagi harflar "xorijiy" da mavjud.

Bu mening halim. Afsuski, bu ishlamayapti, garchi bu kabi muammoni hal qilish mumkin deb o'ylayman. Xato qayerda?

Batafsil sharhlarim bilan kod bu erda:

function mutation(arr) {

  //indexOf is case sensitive, so first we make all the elements in the array lowerCase. After that we use the lowerCaseArray instead of our original array

var y = arr.join(" ");
var x = y.toLowerCase();
var lowerCaseArray = x.split(" ")

// This variable will contain the number of matches 

var matchCounter = 0;

//The for loop picks a letter from the second element
//(lowerCaseArray[1][i]) of an array and then we look
//if a match in the first element of an array(lowerCaseArray[0] is found).
//If there is a match, then the indexOf would return a number >=0.
//In this case we add 1 to our matchCounter.

for (i = 0; i < lowerCaseArray[1].length; i++) {
  if(lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > 0) {
    matchCounter+= 1;

  }

//Finally we compare the matchCounter length with the second
//element of our array.  If matchCounter >= the length of our
//array, it means every letter in the second element was found
//within the first element

}
return matchCounter >= arr[1].length;

}

mutation(["floor", "for"]);

pastkiCaseArray [1] [i] ga qaytsa, ikkinchi elementning oxirgi xarfi "r" bo'lsa-da, "u" ni qaytaradi. Va berilgan misolda matchCount 2ga teng, lekin u 3 bo'lishi kerak, chunki 3 ta o'yin mavjud. Ehtimol, bu xato bo'lgan qism.

3
Sizning kodingizni yugurdim va u to'g'ri 3 harfdan iborat, f , o va r . Qayta ishlash va tekshirish mumkinmi?
qo'shib qo'ydi muallif Daniel T., manba
U shunday qiladi, lekin u noto'g'ri qaytadi, chunki agar 3-chi bo'lsa, MatchCounter 2 ga teng.
qo'shib qo'ydi muallif Igor Schekotihin, manba
U shunday qiladi, lekin u noto'g'ri qaytadi, chunki agar 3-chi bo'lsa, MatchCounter 2 ga teng.
qo'shib qo'ydi muallif Igor Schekotihin, manba

9 javoblar

Nolinchi 0 - stringning joriy indeksidir, chunki -1 teng emasligini tekshirishingiz kerak.

if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) !== -1) {
//                                                  ^^^^^^

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function mutation(arr) {
    var y = arr.join(" "),
        x = y.toLowerCase(),
        lowerCaseArray = x.split(" "),
        matchCounter = 0,
        i;

    for (i = 0; i < lowerCaseArray[1].length; i++) {
        if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) !== -1) {
            matchCounter += 1;
        }
    }
    return matchCounter >= arr[1].length;
}

console.log(mutation(["floor", "for"]));
</div> </div>
2
qo'shib qo'ydi

Nolinchi 0 - stringning joriy indeksidir, chunki -1 teng emasligini tekshirishingiz kerak.

if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) !== -1) {
//                                                  ^^^^^^

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function mutation(arr) {
    var y = arr.join(" "),
        x = y.toLowerCase(),
        lowerCaseArray = x.split(" "),
        matchCounter = 0,
        i;

    for (i = 0; i < lowerCaseArray[1].length; i++) {
        if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) !== -1) {
            matchCounter += 1;
        }
    }
    return matchCounter >= arr[1].length;
}

console.log(mutation(["floor", "for"]));
</div> </div>
2
qo'shib qo'ydi

Kodingizni noto'g'ri natijaga qaytarishga sabab bo'lgan chiziq quyidagicha:

if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > 0) {

Bu qidirilayotgan belgi 0 holatida bo'lishi mumkinligini hisobga olmaydi.

Changing the > to >= gets it to work. Your comments actually indicate that it should be >=, but your code uses >.

Kodni biroz kamroq konvertatsiya qilish mumkin bo'lgan bir necha boshqa joylar mavjud. Pastga qarang:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function mutation(arr) {

  //indexOf is case sensitive, so first we make all the elements in the array lowerCase. After that we use the lowerCaseArray instead of our original array

  var lowerCaseArray = arr.map(function (str) {
      return str.toLowerCase();
  });

 //the for loop checks each letter in the second string
 //if any of its letters is not present in the first one,
 //return false immediately
  for (i = 0; i < lowerCaseArray[1].length; i++) {
    if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) === -1) {
      return false;
    }
  }
  
 //if the for loop completed without returning, then the strings pass the test.
  return true;
}

console.log(mutation(["floor", "for"]));
</div> </div>
1
qo'shib qo'ydi

Kodingizni noto'g'ri natijaga qaytarishga sabab bo'lgan chiziq quyidagicha:

if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > 0) {

Bu qidirilayotgan belgi 0 holatida bo'lishi mumkinligini hisobga olmaydi.

Changing the > to >= gets it to work. Your comments actually indicate that it should be >=, but your code uses >.

Kodni biroz kamroq konvertatsiya qilish mumkin bo'lgan bir necha boshqa joylar mavjud. Pastga qarang:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function mutation(arr) {

  //indexOf is case sensitive, so first we make all the elements in the array lowerCase. After that we use the lowerCaseArray instead of our original array

  var lowerCaseArray = arr.map(function (str) {
      return str.toLowerCase();
  });

 //the for loop checks each letter in the second string
 //if any of its letters is not present in the first one,
 //return false immediately
  for (i = 0; i < lowerCaseArray[1].length; i++) {
    if (lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) === -1) {
      return false;
    }
  }
  
 //if the for loop completed without returning, then the strings pass the test.
  return true;
}

console.log(mutation(["floor", "for"]));
</div> </div>
1
qo'shib qo'ydi

Bir marta ushbu o'yinni sinab ko'ring ('sngng', 'Singer').

var match=function(a,b)
{
    var nomatch=0;
    var w1=a.toLowerCase(); 
    var word1=w1.split('');
    var string2=b.toLowerCase();
    console.log('word1 len : '+word1.length+' '+string2+' length : '+string2.length);
        for(var i=0;i0)
    {
        console.log(b+' does not have all  characters of '+a);
    }
    else
    {
        console.log(b+' do have all  characters of '+a);
    }

}
match('hello','Hell');
0
qo'shib qo'ydi

Shu bilan bir qatorda regex dan ham foydalanishingiz mumkin. Bu sizning ishingiz bo'yicha shikoyatni saqlab qoladi.

Sample

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function validate(arr){
  var regex_str = arr[0].replace(/[^a-z0-9 ]/gi, function(m){ return "\\" + m }) 
  var regex = new RegExp("^[" + regex_str + "]*$", "i");
  var valid = regex.test(arr[1]);
  console.log(regex, "|", arr[1], "|", valid)
  return valid
}

validate(["floor", "for"])
validate(["floor", "fora"])
validate(["heworld", "hello World "])
validate(["heworld", "hello World "])
validate(["heworldts(*) ", "hello World (test *)"])
</div> </div>
0
qo'shib qo'ydi
Birinchi satrda `yoki ]` kabi belgilar bo'lsa, bu buziladi.
qo'shib qo'ydi muallif JLRishe, manba
@JLRishe Ko'rsatganingiz uchun tashakkur. Iltimos, yangilanishni tekshiring.
qo'shib qo'ydi muallif Rajesh, manba

Shu bilan bir qatorda regex dan ham foydalanishingiz mumkin. Bu sizning ishingiz bo'yicha shikoyatni saqlab qoladi.

Sample

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function validate(arr){
  var regex_str = arr[0].replace(/[^a-z0-9 ]/gi, function(m){ return "\\" + m }) 
  var regex = new RegExp("^[" + regex_str + "]*$", "i");
  var valid = regex.test(arr[1]);
  console.log(regex, "|", arr[1], "|", valid)
  return valid
}

validate(["floor", "for"])
validate(["floor", "fora"])
validate(["heworld", "hello World "])
validate(["heworld", "hello World "])
validate(["heworldts(*) ", "hello World (test *)"])
</div> </div>
0
qo'shib qo'ydi
Birinchi satrda `yoki ]` kabi belgilar bo'lsa, bu buziladi.
qo'shib qo'ydi muallif JLRishe, manba
@JLRishe Ko'rsatganingiz uchun tashakkur. Iltimos, yangilanishni tekshiring.
qo'shib qo'ydi muallif Rajesh, manba
if(lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > -1)?
0
qo'shib qo'ydi
if(lowerCaseArray[0].indexOf(lowerCaseArray[1][i]) > -1)?
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb