4
u/Cheesybread4ever Jul 02 '21
So I want this function to accept an array of nine numbers, and I want it to return true if all the numbers are unique. I’m assuming all the numbers will be 1-9. For some reason this keeps returning false when I pass an array that should return true. You can also ignore the console log I was trying to debug with it.
1
u/Roguewind Jul 02 '21
Under the assumption that the myArray will always be exactly 9 single digit numbers, just pass it into a set and check the size of the set
const includes1to9(myArray) => { return (new Set(myArray).size === 9) }Set takes an array and creates a Set object with unique values.
1
u/Mollyarty Jul 03 '21
You should check the incoming array is the right length. I see other people already answered your question but I didn't see anyone mention that. The way you have it now the array [1,2,3,4,5,6,7,8,9,1] would pass
0
Jul 02 '21 edited Jul 02 '21
So you are wanting the array to search itself and make sure that there are no duplicates within the array? I would use a nested for loop within the for loop array instead like this:
function includes1to9(myArray){ let unique = true; for(let i=0; i< myArray.length; i++) { for(let index = 0; index < myArray.length; index++) { if(index != i) { if(myArray[i] == myArray[index]) { unique = false } } } } return unique; }Instead of adding another array into the mix, you can compare the array against itself and skip the current index you are comparing (because of course a number compared to itself would be equal haha!). If you find a match, it isn't unique and the bool trips to false. I'm sure there are better ways to do it, but this is how I would do it (ps, sorry if my syntax is off with let vs var; I am more comfortable with Java/C#. I did run the logic of this in C# though, and it checks out!). I hope his help!
4
u/kd4444 Jul 02 '21
Maybe try using a set to check the length against the original array, to see if anything was removed (meaning that there was a duplicate)?
1
u/Cheesybread4ever Jul 02 '21
I’m not sure I follow. What length do you want me to check against the original array?
4
u/kd4444 Jul 02 '21 edited Jul 02 '21
function includes1to9(myArray) {
const myCleanedArray = myArray.filter(num m => num !== 0); const mySet = new Set(myCleanedArray); return mySet.size === myCleanedArray.length; }Can’t get the formatting quite right but something like this should work - the set will remove any duplicated values, so if its size doesn’t match the length of the input array, it will return false.
3
u/NoAd7524 Jul 02 '21
Looks like trouble with the logic of it to me. Why don't you try instead "if ... -> return true", take the else away and change the "return true" at the end for "return false" and then take it from there?
1
u/Cheesybread4ever Jul 02 '21
But I don’t want it to return true until it’s check every element of myArray. If I put return true inside the if statement it will always return true.
1
u/NoAd7524 Jul 02 '21
Ah wait, missed that part but the logic is bad anyway. It helps if you paste the code in text form. Could you please do this?
0
u/Cheesybread4ever Jul 02 '21
function includes1to9(myArray) { let array = [1,2,3,4,5,6,7,8,9]; for (let i = 0; i < myArray.length; i++) { if (array.includes(myArray[i])) { array.splice((myArray[i] - 1), 1); } else return false } return true }
Idk why it isn’t spaced right
1
u/NoAd7524 Jul 02 '21 edited Jul 02 '21
Spacing is funny that way here. Let me try it. Try this one:
function ix129(arrx){ for (let i=0;i<arrx.length; i++){ let a = arrx[i]; for (let j=i+1; j<arrx.length;j++){ let b = arrx[j]; if(a==b) {document.write("False"); return;} } } document.write("True"); }1
1
u/NoAd7524 Jul 02 '21
For uniqueness it seems like you need to check each number against the rest. Double loops, perhaps? Let's wait for the text code.
3
u/chrimack Jul 02 '21
It's not clever js, but it you want to make sure the array contains every number from 1 to 9 you can add them all together. Then return whether the sum is 45. Also make sure that the numbers aren't all 5s.
1
u/beatschill Jul 02 '21
Are you able to list an array call with multiple numbers? "[1,2,3,4,5,6,7,8,9]" any time I've tried this, the interpreter doesn't seemtorun it properly
10
u/LeaveTheWolf Jul 02 '21
.splice() mutates the array. So your logic for removing the encountered element is only guaranteed to be correct on the first pass. After that it is pretty likely to fail. For what it is worth, I do think this might work for the very specific case of a valid array passed in descending order.
Anyway you need to change your .splice() call to use .indexOf() to find the array index instead of your attempt at indexing myArray[].
Let me know if you need more than that, but hopefully it helps.