Skip to content

Commit 5a6d75c

Browse files
committed
Replace nested conditional with Gaurd Clause
1 parent a8c9006 commit 5a6d75c

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

JavaScript/chapter01/1.5 - OneAway/rroque98_sol.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,17 @@ const isOneAway = (str1, str2) => {
1919
}
2020
}
2121
}
22-
} else {
23-
const longStr = str1.length > str2.length ? str1 : str2;
24-
const shortStr = str1.length < str2.length ? str1 : str2;
25-
for (
26-
let i = 0, x = 0;
27-
i < longStr.length && x < shortStr.length;
28-
i++, x++
29-
) {
30-
if (longStr[i] !== shortStr[x]) {
31-
errorCount++;
32-
if (errorCount > 1) {
33-
return false;
34-
}
35-
i++;
22+
return true;
23+
}
24+
const longStr = str1.length > str2.length ? str1 : str2;
25+
const shortStr = str1.length < str2.length ? str1 : str2;
26+
for (let i = 0, x = 0; i < longStr.length && x < shortStr.length; i++, x++) {
27+
if (longStr[i] !== shortStr[x]) {
28+
errorCount++;
29+
if (errorCount > 1) {
30+
return false;
3631
}
32+
i++;
3733
}
3834
}
3935
return true;

0 commit comments

Comments
 (0)