Does anyone know how to code the mastermind in javascript??

Posted by AdVisible6484@reddit | learnprogramming | View on Reddit | 8 comments

I have coded till this.

console.log("Welcome!!!")

const prompt = require ('prompt-sync')();
const size = prompt('Enter the size.');

let arr = new Array(size);
for(let i =
0
;i<size;i++)
{
  arr[i] = Math.floor(Math.random()*
10
);
}


//console.log(arr);
let state = true;
while(true)
{
     let val = prompt('Enter the array:');
     if(val=='break') {
     console.log(`The answer was ${arr}`);
     break
     };
     if(val.length>size)
     {
      console.log("wrong length..");
      continue;
     }
     let correct = 
0
;

     let includedInTheFinal = 
0
;
     let temp = [...arr];
     for(let i=
0
;i<size;i++)
     {
       if(Number(val[i])==arr[i])
       {
        correct++;
       }

       let index = temp.indexOf(Number(val[i]));
       if(temp[index]!==-
1
)
       {
        includedInTheFinal++;
        temp[index]=-
1
;
        }
     }
     if(correct==size)
     {
      console.log("Correct!!!!");
      break;
     }
     else
     {
      console.log(`${correct} are in correct position || ${includedInTheFinal} are included in the final array`);

     }

}
/*
1. says how mmany positions have the correct value in the them.
2. How many digits actuallt present in the code
*/

But I am not able to get the 2 thing right.. That is outputting the number the digits that are in the wrong place but in the code.. I am super confused..

I really appreciate any directions and suggestions.