2016-10-23 7 views
0

wordという文字列とbookと呼ばれる文字列が となる関数offOne(word, book)を作成します。 同じ の長さのbookwordの配列がすべて1文字異なる配列を返します。単語と配列の単語を比較する

例:

offOne("cat", ["cat", "fat", "flat", "tar"]) => ["fat", "tar"] 
offOne("will", ["wilt", "willow", "wail"]) => ["wilt", "wail"] 

My機能は、現在、次のとおりです。

function offOne(word, book) { 
    var array = []; 
    var count = 0; 

    for (var i = 0; i < book.length; i++) { 
     if (book.length === word.length) { 
      if (word.indexOf(book[i]) !== -1) { 
       count += 1; 

       if (count === (book[i].length - 1)) { 
        array.push(book[i]); 
       } 
      } 
     } 
    } 
    return array; 
} 

誰もがこの問題を解決する方法を知っていますか?私はしばらくここで立ち往生した。

+1

それとも事前面接のスクリーニング質問だろうか? –

+0

私の意見では、それが本当にSOに合致していないという意味での質問はよく述べられていません。あなたの問題は何ですか?私はあなたの仕事を解決するためのアルゴリズムを提供する答えを得るだろうと思う。関数の定義はあまり意味がありません。あなたはJavaScriptをあまりよく理解していないと感じています。そこから始めよう:本の長さと単語の長さ - なぜ文字列の長さを配列の長さと比較するのでしょうか? – Elyasin

+0

これは私の50の準備問題の1つです。 – DoeDoeDoe

答えて

1

スニペットはよくコメントされています。それはあなたを助けるはずです。それをチェックしてください!

ポイントは、あなたの準備のために覚えておく:

  1. は、不必要な変数を宣言しないでください。それは記憶を消費しますが、それは悪いです。
  2. 不要なループを使用しないでください。ループを使用する前に、使用可能な言語APIを確認してください。同様に、foreachの代わりにfilterを使用しました。これらはあなたの仕事を減らすでしょう。
  3. 常に考慮するLogical operators
  4. コードをシンプルにする。

あなたのコースに最適な運勢!やっての

マイウェイそれ

var word = "cat"; 
 
var book = ["car", "far", "mars", "call", "bat"] 
 

 
function compare(elm, word) { 
 
    var i = 0 
 
    elm.split('').forEach(c => { //tokenize elm of book into array 
 
    if (word.indexOf(c) > -1) //check if charecter in present in the word 
 
     i += 1 //if yes, increment 
 
    }) 
 
    return i === word.length - 1 ? true : false //return true if length of i is (length of word - 1), 
 
} 
 

 
function offOne(word, book) { 
 
    return book.filter(elm => 
 
    // check, if the length of both strings are not same and 
 
    // both strings are not same and 
 
    // compare strings, true will be returned if the condition is satisfied in compare() 
 
    elm.length === word.length && elm !== word && compare(elm, word) 
 
) 
 
} 
 

 
console.log(offOne(word, book))

やって私の高度な方法それ

あなたが表示された場合、これは内部で宣言された変数を持っていません関数。

var word = "cat"; 
 
var book = ["car", "far", "mars", "call", "bat"] 
 

 
function compare(elm, word) { 
 
    return elm.split('').filter(c => //tokenize elm of book into array 
 
    word.indexOf(c) > -1 //check if charecter in present in the word, if yes, return true 
 
).join('').length === word.length - 1 ? true : false //join and check the length of the array is one less than length of the word, if yes, return true 
 
} 
 

 
function offOne(word, book) { 
 
    return book.filter(elm => 
 
    // check, if the length of both strings are not same and 
 
    // both strings are not same and 
 
    // compare strings, true will be returned if the condition is satisfied in compare() 
 
    elm.length === word.length && elm !== word && compare(elm, word) 
 
) 
 
} 
 

 
console.log(offOne(word, book))

0

単語を文字の配列に変換し、この配列を一意にします。それぞれのブック配列アイテムについて、同じものを実行し、それらの間の異なる文字の数を計算します。差異が1つだけ見つかった場合は、その項目を返し、それぞれを繰り返します。

+0

だから私は単語を分割し、それらを繰り返し、indexOfの本をチェックしなければならないが、ただ一つの違いが見つかったらどうやってチェックするのだろうか? – DoeDoeDoe

+0

1つの配列の要素を反復処理し、2つ目の配列に含まれていない各項目のカウンタを増やすことができます。それらはプログラミングの基礎です。 –

関連する問題