diff --git a/js/cyclic-rotation.js b/js/cyclic-rotation.js index 13c7dfd..19acb86 100644 --- a/js/cyclic-rotation.js +++ b/js/cyclic-rotation.js @@ -10,7 +10,7 @@ console.log('// CYCLIC ROTATION //'); function solution(A, K) { - K = (A.length > K) ? K : K % A.length; + K = K % A.length; var d = A.slice(0, A.length - K); var e = A.splice(A.length - K); @@ -33,4 +33,4 @@ test([5, -1000], 1); function test(...params) { console.log('\n(', ...params, ')\n'); console.log('\n=>', solution(...params), '\n\n'); -} \ No newline at end of file +} diff --git a/js/decimal-representation.js b/js/decimal-representation.js index 27d7e9b..b3c9fc8 100644 --- a/js/decimal-representation.js +++ b/js/decimal-representation.js @@ -15,15 +15,19 @@ console.log('// DECIMAL REPRESENTATION //'); function solution(A) { - - let newNumber = 17 * Number(A.reverse().toString().replace(/,/g, '')); - - let total = 0; - newNumber.toString().split('').map(e => total += Number(e)); - return total; + let sum = 0, overflow = 0, result = 0; + + for (let i = 0; i < A.length; i ++) { + sum = 17 * A[i] + overflow; + overflow = parseInt(sum / 10); + result += parseInt(sum % 10); + } + + result += parseInt(overflow % 10) + parseInt(overflow / 10); + + return result; } - test([3, 5, 1]); // 9 @@ -32,4 +36,4 @@ test([3, 5, 1]); function test(...params) { console.log('\n(', ...params, ')\n'); console.log('\n=>', solution(...params), '\n\n'); -} \ No newline at end of file +} diff --git a/js/distinct.js b/js/distinct.js index eda4a99..6743360 100644 --- a/js/distinct.js +++ b/js/distinct.js @@ -7,19 +7,17 @@ */ function solution(A) { - - var seen = []; - var count = 0; - var len = A.length; - for (var i = 0; i < len; i++) { - var item = A[i]; - if (seen[item] !== 1) { - seen[item] = 1; - count++ + let sorted = [], count = 0; + + sorted = A.sort((a, b) => a - b); + + for(let i = 0; i < sorted.length; i++) { + if (sorted[i] !== sorted[i-1]) { + count++; } } + return count; - } @@ -40,4 +38,4 @@ test([2, 1, 1, 2, 3, 1]); function test(...params) { console.log('\n(', ...params, ')\n'); console.log('\n=>', solution(...params), '\n\n'); -} \ No newline at end of file +}