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.
Ormek_II@reddit
Are you sure about the expected output? Often not understanding the challenge is main problem.
Are= 115511 Val= 111555
The printed program outputs: 3 and 6?
Well each 5 can be found in arr, each 1 as well.
joranstark018@reddit
Yeah, one thing that may help you is to have separte methods for the things you want to find (ie calculating how many numbers are in correct place may go into one method) that way it becomes easier to make sure that each part behaves correctly on its own (and you can easier focus on each sub-task).
Check your if-statements, are they comparing the right thing (running your code with a debugger and checking what values you actually are using/comparing is a usefull skill to rapidly find bad behaving code).
AdVisible6484@reddit (OP)
Yeah thanks.. that was really helpful. I did separate them into different modules.
AdVisible6484@reddit (OP)
Thank you all for your suggestions.. it was very helpful.
can you suggest some edge cases where this might break. Thanks
johnpeters42@reddit
Throw in some more console.log statements whenever the loop counter increases, and whenever 'correct' or 'includedInTheFinal' increases. Then compare that to the input, find a specific step where it didn't do what it should have, and look more closely there to work out why.
AdVisible6484@reddit (OP)
Yeah I did that.. then ended up creating separate loops first to separate correct places and check of presence.
cipheron@reddit
You need to do a second scan after marking all the ones that are in the right spot.
But you'll need a nested for loop.
For each arr element, check if it's not -1 in temp, then scan the other array to see if that element is there. You probably need two temp arrays however so you can mark used ones off in both, not just one.
AdVisible6484@reddit (OP)
Hi Thanks.... I didn't exactly understand the working but yeah I tried a similar approach I think its working fine now..
I am eliminating all the correct positions and then checking the presence of the other elements.