8000 Merge pull request #1 from TheAlgorithms/master · TheAlgorithms/JavaScript@1db7aa7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1db7aa7

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
Updating the Forked Repository
2 parents 9e6fe4b + 6c2f83b commit 1db7aa7

File tree

94 files changed

+1883
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+1883
-197
lines changed

.prettierrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSpacing": true,
4+
"endOfLine": "lf",
5+
"insertPragma": false,
6+
"printWidth": 80,
7+
"proseWrap": "preserve",
8+
"quoteProps": "as-needed",
9+
"requirePragma": false,
10+
"semi": false,
11+
"singleQuote": true,
12+
"tabWidth": 2,
13+
"trailingComma": "none",
14+
"useTabs": false
15+
}

Ciphers/ROT13.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Transcipher a ROT13 cipher
3+
* @param {String} text - string to be encrypted
4+
* @return {String} - decrypted string
5+
*/
6+
const transcipher = (text) => {
7+
const originalCharacterList = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
8+
const toBeMappedCharaterList = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
9+
const index = x => originalCharacterList.indexOf(x)
10+
const replace = x => index(x) > -1 ? toBeMappedCharaterList[index(x)] : x
11+
return text.split('').map(replace).join('')
12+
}
13+
14+
(() => {
15+
const messageToBeEncrypted = 'The quick brown fox jumps over the lazy dog'
16+
console.log(`Original Text = "${messageToBeEncrypted}"`)
17+
const rot13CipheredText = transcipher(messageToBeEncrypted)
18+
console.log(`Ciphered Text = "${rot13CipheredText}"`)
19+
const rot13DecipheredText = transcipher(rot13CipheredText)
20+
console.log(`Deciphered Text = "${rot13DecipheredText}"`)
21+
})()

Conversions/HexToRGB.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function hexStringToRGB (hexString) {
2-
var r = (hexString.substring(1, 3)).toUpperCase()
3-
var g = hexString.substring(3, 5).toUpperCase()
4-
var b = hexString.substring(5, 7).toUpperCase()
2+
var r = hexString.substring(0, 2)
3+
var g = hexString.substring(2, 4)
4+
var b = hexString.substring(4, 6)
55

66
r = parseInt(r, 16)
77
g = parseInt(g, 16)
@@ -11,4 +11,4 @@ function hexStringToRGB (hexString) {
1111
return obj
1212
}
1313

14-
console.log(hexStringToRGB('javascript rock !!'))
14+
console.log(hexStringToRGB('ffffff'))

Conversions/RGBToHex.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function RGBToHex (r, g, b) {
2+
if (
3+
typeof r !== 'number' ||
4+
typeof g !== 'number' ||
5+
typeof b !== 'number'
6+
) {
7+
throw new TypeError('argument is not a Number')
8+
}
9+
10+
const toHex = n => (n || '0').toString(16).padStart(2, '0')
11+
12+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`
13+
}
14+
15+
console.log(RGBToHex(255, 255, 255) === '#ffffff')
16+
console.log(RGBToHex(255, 99, 71) === '#ff6347')

DIRECTORY.md

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
## Ciphers
1414
* [CaesarsCipher](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/CaesarsCipher.js)
1515
* [KeyFinder](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/KeyFinder.js)
16+
* [ROT13](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/ROT13.js)
1617
* [VigenereCipher](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/VigenereCipher.js)
1718
* [XORCipher](https://github.com/TheAlgorithms/Javascript/blob/master/Ciphers/XORCipher.js)
1819

@@ -22,6 +23,7 @@
2223
* [DecimalToHex](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToHex.js)
2324
* [DecimalToOctal](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js)
2425
* [HexToRGB](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/HexToRGB.js)
26+
* [RGBToHex](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/RGBToHex.js)
2527
* [RomanToDecimal](https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/RomanToDecimal.js)
2628

2729
## Data-Structures
@@ -58,10 +60,12 @@
5860
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LevenshteinDistance.js)
5961
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestCommonSubsequence.js)
6062
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestIncreasingSubsequence.js)
63+
* [LongestPalindromicSubsequence](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/LongestPalindromicSubsequence.js)
6164
* [MaxNonAdjacentSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/MaxNonAdjacentSum.js)
6265
* [MinimumCostPath](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/MinimumCostPath.js)
6366
* [NumberOfSubsetEqualToGivenSum](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/NumberOfSubsetEqualToGivenSum.js)
6467
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/SieveOfEratosthenes.js)
68+
* [SudokuSolver](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/SudokuSolver.js)
6569
* [ZeroOneKnapsack](https://github.com/TheAlgorithms/Javascript/blob/master/Dynamic-Programming/ZeroOneKnapsack.js)
6670

6771
## Graphs
@@ -71,6 +75,7 @@
7175
* [Dijkstra](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/Dijkstra.js)
7276
* [DijkstraSmallestPath](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/DijkstraSmallestPath.js)
7377
* [KruskalMST](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/KruskalMST.js)
78+
* [NumberOfIslands](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/NumberOfIslands.js)
7479
* [PrimMST](https://github.com/TheAlgorithms/Javascript/blob/master/Graphs/PrimMST.js)
7580

7681
## Hashes
@@ -85,30 +90,71 @@
8590

8691
## Maths
8792
* [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Abs.js)
93+
* [Area](https://github.com/ 10000 TheAlgorithms/Javascript/blob/master/Maths/Area.js)
94+
* [ArmstrongNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ArmstrongNumber.js)
8895
* [AverageMean](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/AverageMean.js)
89-
* [digitSum](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/digitSum.js)
96+
* [DigitSum](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/DigitSum.js)
9097
* [Factorial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Factorial.js)
98+
* [Factors](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Factors.js)
9199
* [Fibonacci](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Fibonacci.js)
92100
* [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindHcf.js)
93101
* [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/FindLcm.js)
94102
* [GridGet](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/GridGet.js)
95103
* [isDivisible](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isDivisible.js)
104+
* [isOdd](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isOdd.js)
105+
* [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js)
96106
* [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MeanSquareError.js)
97107
* [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ModularBinaryExponentiationRecursive.js)
108+
* [NumberOfDigits](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/NumberOfDigits.js)
98109
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Palindrome.js)
99110
* [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PascalTriangle.js)
111+
* [PerfectCube](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PerfectCube.js)
112+
* [PerfectNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PerfectNumber.js)
113+
* [PerfectSquare](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PerfectSquare.js)
100114
* [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PiApproximationMonteCarlo.js)
101115
* [Polynomial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Polynomial.js)
102116
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/PrimeCheck.js)
103117
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/ReversePolishNotation.js)
104118
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/SieveOfEratosthenes.js)
119+
* test
120+
* [Abs](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Abs.test.js)
121+
* [Area](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Area.test.js)
122+
* [ArmstrongNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/ArmstrongNumber.test.js)
123+
* [AverageMean](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/AverageMean.test.js)
124+
* [DigitSum](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/DigitSum.test.js)
125+
* [Factorial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Factorial.test.js)
126+
* [Factors](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Factors.test.js)
127+
* [Fibonacci](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Fibonacci.test.js)
128+
* [FindHcf](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/FindHcf.test.js)
129+
* [FindLcm](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/FindLcm.test.js)
130+
* [GridGet](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/GridGet.test.js)
131+
* [MeanSquareError](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/MeanSquareError.test.js)
132+
* [ModularBinaryExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/ModularBinaryExponentiationRecursive.test.js)
133+
* [NumberOfDigits](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/NumberOfDigits.test.js)
134+
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Palindrome.test.js)
135+
* [PascalTriangle](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PascalTriangle.test.js)
136+
* [PerfectCube](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PerfectCube.test.js)
137+
* [PerfectNumber](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PerfectNumber.test.js)
138+
* [PerfectSquare](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PerfectSquare.test.js)
139+
* [PiApproximationMonteCarlo](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PiApproximationMonteCarlo.test.js)
140+
* [Polynomial](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/Polynomial.test.js)
141+
* [PrimeCheck](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/PrimeCheck.test.js)
142+
* [ReversePolishNotation](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/ReversePolishNotation.test.js)
143+
* [SieveOfEratosthenes](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/test/SieveOfEratosthenes.test.js)
105144

106145
## Project-Euler
107146
* [Problem1](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem1.js)
147+
* [Problem2](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem2.js)
148+
* [Problem3](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem3.js)
149+
* [Problem6](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem6.js)
150+
* [Problem7](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem7.js)
108151

109152
## Recursive
153+
* [BinarySearch](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/BinarySearch.js)
110154
* [EucledianGCD](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/EucledianGCD.js)
155+
* [factorial](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/factorial.js)
111156
* [FibonacciNumberRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/FibonacciNumberRecursive.js)
157+
* [Palindrome](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/Palindrome.js)
112158
* [TowerOfHanoi](https://github.com/TheAlgorithms/Javascript/blob/master/Recursive/TowerOfHanoi.js)
113159

114160
## Search
@@ -121,6 +167,7 @@
121167
* [StringSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Search/StringSearch.js)
122168

123169
## Sorts
170+
* [BeadSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BeadSort.js)
124171
* [BogoSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BogoSort.js)
125172
* [BubbleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BubbleSort.js)
126173
* [BucketSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/BucketSort.js)
@@ -146,25 +193,43 @@
146193

147194
## String
148195
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.js)
149-
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckAnagram.test.js)
150196
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.js)
151-
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPalindrome.test.js)
197+
* [CheckPangram](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckPangram.js)
152198
* [CheckRearrangePalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckRearrangePalindrome.js)
199+
* [CheckVowels](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckVowels.js)
200+
* [CheckVowels](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckVowels.test.js)
153201
* [CheckWordOccurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOccurrence.js)
154202
* [CheckWordOcurrence](https://github.com/TheAlgorithms/Javascript/blob/master/String/CheckWordOcurrence.test.js)
203+
* [createPurmutations](https://github.com/TheAlgorithms/Javascript/blob/master/String/createPurmutations.js)
204+
* [FormatPhoneNumber](https://github.com/TheAlgorithms/Javascript/blob/master/String/FormatPhoneNumber.js)
205+
* [FormatPhoneNumber](https://github.com/TheAlgorithms/Javascript/blob/master/String/FormatPhoneNumber.test.js)
206+
* [GenerateGUID](https://github.com/TheAlgorithms/Javascript/blob/master/String/GenerateGUID.js)
155207
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/String/LevenshteinDistance.js)
156208
* [LevenshteinDistance](https://github.com/TheAlgorithms/Javascript/blob/master/String/LevenshteinDistance.test.js)
209+
* [MaxCharacter](https://github.com/TheAlgorithms/Javascript/blob/master/String/MaxCharacter.js)
210+
* [MaxCharacter](https://github.com/TheAlgorithms/Javascript/blob/master/String/MaxCharacter.test.js)
157211
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.js)
158-
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/PatternMatching.test.js)
212+
* [PermutateString](https://github.com/TheAlgorithms/Javascript/blob/master/String/PermutateString.js)
213+
* [PermutateString](https://github.com/TheAlgorithms/Javascript/blob/master/String/PermutateString.test.js)
159214
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.js)
160-
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseString.test.js)
161215
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.js)
162-
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/ReverseWords.test.js)
216+
* test
217+
* [CheckAnagram](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CheckAnagram.test.js)
218+
* [CheckPalindrome](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CheckPalindrome.test.js)
219+
* [CheckPangram](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/CheckPangram.test.js)
220+
* [PatternMatching](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/PatternMatching.test.js)
221+
* [ReverseString](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/ReverseString.test.js)
222+
* [ReverseWords](https://github.com/TheAlgorithms/Javascript/blob/master/String/test/ReverseWords.test.js)
223+
* [ValidateEmail](https://github.com/TheAlgorithms/Javascript/blob/master/String/ValidateEmail.js)
224+
* [ValidateEmail](https://github.com/TheAlgorithms/Javascript/blob/master/String/ValidateEmail.test.js)
163225

164226
## Timing-Functions
227+
* [GetMonthDays](https://github.com/TheAlgorithms/Javascript/blob/master/Timing-Functions/GetMonthDays.js)
228+
* [GetMonthDays](https://github.com/TheAlgorithms/Javascript/blob/master/Timing-Functions/GetMonthDays.test.js)
165229
* [IntervalTimer](https://github.com/TheAlgorithms/Javascript/blob/master/Timing-Functions/IntervalTimer.js)
166230

167231
## Trees
232+
* [BreadthFirstTreeTraversal](https://github.com/TheAlgorithms/Javascript/blob/master/Trees/BreadthFirstTreeTraversal.js)
168233
* [DepthFirstSearch](https://github.com/TheAlgorithms/Javascript/blob/master/Trees/DepthFirstSearch.js)
169234

170235
## Web-Programming
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
LeetCode -> https://leetcode.com/problems/longest-palindromic-subsequence/
3+
4+
Given a string s, find the longest palindromic subsequence's length in s.
5+
You may assume that the maximum length of s is 1000.
6+
7+
*/
8+
9+
const longestPalindromeSubsequence = function (s) {
10+
const n = s.length
11+
12+
const dp = new Array(n).fill(0).map(item => new Array(n).fill(0).map(item => 0))
13+
14+
// fill predefined for single character
15+
for (let i = 0; i < n; i++) {
16+
dp[i][i] = 1
17+
}
18+
19+
for (let i = 1; i < n; i++) {
20+
for (let j = 0; j < n - i; j++) {
21+
const col = j + i
22+
if (s[j] === s[col]) {
23+
dp[j][col] = 2 + dp[j + 1][col - 1]
24+
} else {
25+
dp[j][col] = Math.max(dp[j][col - 1], dp[j + 1][col])
26+
}
27+
}
28+
}
29+
30+
return dp[0][n - 1]
31+
}
32+
33+
const main = () => {
34+
console.log(longestPalindromeSubsequence('bbbab')) // 4
35+
console.log(longestPalindromeSubsequence('axbya')) // 3
36+
console.log(longestPalindromeSubsequence('racexyzcxar')) // 7
37+
}
38+
39+
main()

Dynamic-Programming/SudokuSolver.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const _board = [
2+
['.', '9', '.', '.', '4', '2', '1', '3', '6'],
3+
['.', '.', '.', '9', '6', '.', '4', '8', '5'],
4+
['.', '.', '.', '5', '8', '1', '.', '.', '.'],
5+
['.', '.', '4', '.', '.', '.', '.', '.', '.'],
6+
['5', '1', '7', '2', '.', '.', '9', '.', '.'],
7+
['6', '.', '2', '.', '.', '.', '3', '7', '.'],
8+
['1', '.', '.', '8', '.', '4', '.', '2', '.'],
9+
['7', '.', '6', '.', '.', '.', '8', '1', '.'],
10+
['3', '.', '.', '.', '9', '.', '.', '.', '.']
11+
]
12+
13+
const isValid = (board, row, col, k) => {
14+
for (let i = 0; i < 9; i++) {
15+
const m = 3 * Math.floor(row / 3) + Math.floor(i / 3)
16+
const n = 3 * Math.floor(col / 3) + i % 3
17+
if (board[row][i] === k || board[i][col] === k || board[m][n] === k) {
18+
return false
19+
}
20+
}
21+
return true
22+
}
23+
24+
const sodokoSolver = (data) => {
25+
for (let i = 0; i < 9; i++) {
26+
for (let j = 0; j < 9; j++) {
27+
if (data[i][j] === '.') {
28+
for (let k = 1; k <= 9; k++) {
29+
if (isValid(data, i, j, k)) {
30+
data[i][j] = `${k}`
31+
if (sodokoSolver(data)) {
32+
return true
33+
} else {
34+
data[i][j] = '.'
35+
}
36+
}
37+
}
38+
return false
39+
}
40+
}
41+
}
42+
return true
43+
}
44+
45+
// testing
46+
(() => {
47+
if (sodokoSolver(_board)) {
48+
console.log(_board)
49+
}
50+
})()

0 commit comments

Comments
 (0)
0