Reputation: 41
As of right now, I'm trying to develop the logic behind a sudoku project. My function includes1to9
takes in any row or column of the puzzle and should return true
if that row includes every number from 1 to 9 and that those numbers do not repeat themselves. I guess with only defining what it must include, it should be okay. How can I make the include method check multiple parameters? As I have it right now it always returns true even if the section has repeated numbers and does not include all the numbers from 1 to 9. I'll leave the sudoku grid I made and the function. I have two attempts, one is commented out. None of them work.
let puzzle = [[ 8,9,5, 7,4,2, 1,3,6 ],
[ 2,7,1, 9,6,3, 4,8,5 ],
[ 4,6,3, 5,8,1, 7,9,2 ],
[ 9,3,4, 6,1,7, 2,5,8 ],
[ 5,1,7, 2,3,8, 9,6,4 ],
[ 6,8,2, 4,5,9, 3,7,1 ],
[ 1,5,9, 8,7,4, 6,2,3 ],
[ 7,4,6, 3,2,5, 8,1,9 ],
[ 3,2,8, 1,9,6, 5,4,7 ]];
function includes1to9 (subSection) {
for (let i = 1; i < 10; i++) {
if (subSection.includes(i)) {
return true;
} else {
return false;
}
}
// if (subSection.includes(1 && 2 && 3 && 4 && 5 && 6 && 7 && 8 && 9)) {
// return true;
// } else {
// return false;
// }
}
Upvotes: 1
Views: 1736
Reputation: 4912
UPDATE:
Here's an even more idiomatic solution:
function includes(arr, ...args) {
return !args.some(arg => !arr.includes(arg));
}
Example of usage:
console.log(includes(your_array, 1, 2, 3, 4, 5, 6, 7, 8, 9));
Since Array.some
returns true
as soon as any element is found to match the specified condition, we can have the condition be whether an element is not included in the array, in which case the check will be short-circuited upon the first non-included element (if any) and fail early without wasting time checking more elements.
Then of course we return the opposite of what .some
returned, by flipping it with the !
operator, since we would rather know directly whether all elements are included, as opposed to whether any element was not included.
Here's an idiomatic solution:
function includes(arr, ...args) {
return args.map(arg => arr.includes(arg))
.filter(Boolean).length == args.length;
}
Here's an example or two of how to use it:
function includes(arr, ...args) {
return args.map(arg => arr.includes(arg))
.filter(Boolean).length == args.length;
}
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(
includes(array, 1, 2, 3, 4, 5, 6, 7, 8, 9), // true
includes(array, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) // false
);
What it does is creates a new array whose elements are either true
or false
based on whether the parameter at their corresponding index exists in the array in question. Then it filters out all the false
elements. If the remaining array is still the same length as the original array, then all the elements were true and every parameter must be included in the original array.
Upvotes: 1
Reputation: 827
function includes1to9(section) {
return [...new Set(section)]
.filter(num => num > 0 && num < 10).length === 9;
}
console.log(includes1to9([8, 9, 5, 7, 4, 2, 8, 3, 6])); // F
console.log(includes1to9([-1, 9, 5, 7, 4, 2, 1, 3, 6])); // F
console.log(includes1to9([10, 9, 5, 7, 4, 2, 1, 3, 6])); // F
console.log(includes1to9([8, 9, 5, 7, 4, 2, 1, 3, 6])); // T
Upvotes: 0
Reputation: 481
The way your current logic works is like an OR gate. You return true if any of the numbers are included. I can see in your commented out attempt that you tried to make this an AND gate, but unfortunately you can't just string together parameters like that for the include function. You'll have to write your own AND gate. You can do that by setting an initial variable to true and then only setting it to false if any of the digits do not appear:
function includes1to9 (subSection) {
let andGate = true;
for (let i = 1; i < 10; i++) {
if (!subSection.includes(i)) {
andGate = false;
}
}
return andGate;
}
This assumes that the subsection is 9 elements long, in which case if every number appears once there must be no duplicates, so there is no need to check for duplicates.
Upvotes: 1
Reputation: 89224
Loop over the array and create an object representing whether or not each element exists. Such an object is easier to verify.
function includes1to9 (subSection) {
if(subSection.length !== 9) return false;
const vis = subSection.reduce((vis,cur)=>(vis[cur]=true,vis), {});
const keys = Object.keys(vis);
const allowed = [1,2,3,4,5,6,7,8,9];
return keys.length === 9 && keys.every(key=>allowed.includes(+key));
}
console.log(includes1to9([1,9,8,7,6,5,4,3,2]));
Upvotes: 1