2017-12-13 2 views
-5

どのような配列や文字列をターゲットにするのが最適なのでしょうか?ユーザーが文字を入力すると、一致する文字とそのインデックスが配列(または文字列)に記録されますか? :私は戻って私の元の質問(OP)に行くのチェックを(行う予定NEXTと呼ばれるファイルcheckGuess.js ONユーザー入力による配列または文字列をターゲットにして、それぞれのインデックスを記録する最適な方法は何ですか?

GUESS THIS MOVIE: 

how to train your dragon 
___ __ _____ ____ ______ 

Type a letter to guess, you have 10 TRIES: 
User Typed: o 

Result: _o_ _o _____ _o__ ____o_ 

HERES MY CODE: 
    var fs = require('fs'); 
    var inquirer = require('inquirer'); 
    var displayProgress = require('./checkGuess'); 

// var checkGuess = require('./checkGuess'); 


var PlayFunc = function() { 
    var blanksArr = []; 
    var currentWord = []; 

this.getData = function() { 
    var stackOv = ""; 
    fs.readFile("words.txt", "utf8", function(error, data){ 
     if (error) throw error; 
     dataType = data.toLowerCase(); 
     //data in array 
     var wordArr = dataType.split(','); 
     //select random from word from data 
     var compWord = wordArr[Math.floor(Math.random() * wordArr.length)];//random 
     //split chosen word 
     currentWord = compWord.split(''); 
     console.log("========================\n\n\n"); 

     //Looping through the word   
     for (var i = 0; i <= currentWord.length - 1; i++) { 
      // pushing blanks 
      var gArr = blanksArr.push("_"); 

      //HYPHENS, COLONS, SPACES SHOULD BE PASSED 
      stackOv = currentWord.join("").replace(/[^- :'.]/g, "_"); 
      wordString = currentWord.join(""); 
     } 
     console.log("GUESS THIS MOVIE: "); 
     fs.writeFile("blanks.txt", stackOv, (err) => { 
      if (err) throw err; 
      console.log(wordString); 
      fs.readFile('blanks.txt', "utf8",(err, word) => { 
       if (err) throw err; 
       // console.log("GUESS THIS MOVIE: " + compWord); 
       blanksTxt = word.split(''); //console.log(string.join('').replace(/[^-: '.]/g, "_")); 

       displayProgress = new displayProgress(); 
       displayProgress.checkGuess(); 
      }); 
     });   
    }); 
} 

} 
module.exports = PlayFunc; 

このため

var fs = require('fs'); 
var inquirer = require('inquirer'); 
var PlayFunc = require('./PlayFunc'); 

var displayProgress = function(){ 
    // console.log("WORKING CONNECTED CHECKGUESS MODULE"); 
    // PlayFunc = new PlayFunc(); 
    // PlayFunc.getData(); 


    var a = blanksTxt.join(''); console.log(a); //string a 
    var manipulateThisArray = blanksTxt;//reading from blanks.txt 

    // console.log(manipulateThisArray); 


    this.checkGuess = function(){ 
     inquirer.prompt([ 
     { 
     type: "input", 
     name: "letter", 
     message: "Type a letter to guess, you have 10 TRIES:" 
     } 

    ]).then(function(userInput) { 
     var correctArray = []; 
     // console.log(userInput.letter); 
     letterTyped = userInput.letter; 
     //logic 
     //test if we can parse through the array 
     for (var i = 0; i <= manipulateThisArray.length - 1; i++) { 
      x = manipulateThisArray[i]; console.log(x); 
      // if userinput letter-value matches chosen words letter value 
      // replace this chosen worsa letter with userinput value 
      // if(letterTyped == x.charAt(i)) { 

       console.log("THERES A MATCH " + x.charAt(i)); 
      // }else { 
       // console.log("NO MATCH"); 
      // } 

     } 
    }); 
} 
} 
// checkGuess(); 

module.exports = displayProgress; 
+0

場合は、文字列または配列 –

+1

そして、あなたが行き詰まったのでしょうか?あなたの試みはどこですか?どこが間違っていたのですか?期待するインプットを事前に知っているのはなぜですか? –

+0

これまでに試したことはありません。配列や文字列で 'indexOf'メソッドのパフォーマンスを実際に検索することができます。私はそれがあなたが探しているものであることを願っています。 [ここをクリック](https://stackoverflow.com/questions/33049813/javascript-difference-in-efficiency-of-indexof-method-on-string-and-array) –

答えて

-2

私は'foo'.replace('bar', 'bar')を使用するとなるだろうStringに入力'foo'.includes('input')が含まれていることを確認してください。RegExpを使用することもできます。 インデックスを取得するには、文字列の長さにわたりOPと戻ります'foo'.indexOf('input', number)を使用-1私たちは「あなたのドラゴンを訓練する方法」と仮定することができていない偶然の一致

+0

そして、あなたはどのように「それぞれのインデックスを記録するか」という質問について質問に答えます。 –

+0

文字列の長さをループして '' foo'.indexOf( 'input'、number) 'を使用すると、インデックスが繰り返されてもそれを伝えることができます –

+0

私は知っていますそれを他の人に説明するための答え。 –

0
//declaration of the variables we need 
//the original string: 
const string = "how to train your dragon"; 
//a string of characters we want to show 
const show = "o"; 
//A resulting string we can work with 
var result = ""; 
//Now we go over every character of our string and 
for(const character of string){ 
    //check if the character is inside the characters we want to show 
    if(show.includes(character)){ 
    //if so we show it by adding it to the result 
    result += character; 
    }else{ 
    //If not we add an _ instead 
    result += "_"; 
    } 
} 
//At the end we can show the result 
alert(result); 
関連する問題