-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alessandro Giordo
committed
Feb 8, 2016
1 parent
7f533d0
commit c207564
Showing
24 changed files
with
437 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Code Wars Practice scripts | ||
====== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function Calculator(vat){ | ||
this.getNet = function(grossPrice){ | ||
grossPrice = (grossPrice / (vat / 100 + 1)).toFixed(2); | ||
console.log(grossPrice); | ||
return parseFloat(grossPrice); | ||
|
||
}; | ||
this.getVat = function(grossPrice){ | ||
grossPrice = (grossPrice - this.getNet(grossPrice)).toFixed(2); | ||
return parseFloat(grossPrice); | ||
}; | ||
} | ||
|
||
var calc = new Calculator(20); | ||
calc.getNet(100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function pipeFix(numbers){ | ||
for (var i = 0; i < numbers.length; i++) { | ||
if (numbers[i] !== numbers[i+1]-1 && i < numbers.length-1 ) { | ||
numbers.splice(i+1,0,numbers[i]+1); | ||
} | ||
} | ||
return numbers; | ||
} | ||
pipeFix([1,2,3,5,6,8,9]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function imageFilter(arr) { | ||
return arr.map(file => file.match(/^(.+)\.(bmp|gif|jpg|png|tiff)$/i)) | ||
} | ||
imageFilter(["favicon.gif", "img.tiff"]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function Counter() { | ||
var counter = 0; | ||
this.check = function(){return counter;}; | ||
this.increment = function(){counter++;}; | ||
}; | ||
var myCounter = new Counter(); | ||
myCounter.increment(); | ||
myCounter.increment(); | ||
myCounter.check(); | ||
|
||
test. | ||
test2 | ||
test3 | ||
test5 | ||
test 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
var numberToMoney = function(n) { | ||
n = (Math.floor(n * 100) / 100); | ||
n = String(n); | ||
n = n.split(""); | ||
if (n.length > 6) { | ||
n = n.slice(0, 7); | ||
n.splice(1, 0, ","); | ||
n = n.join(""); | ||
} | ||
else { | ||
n = n.join(""); | ||
n = (Math.floor(n * 100) / 100); | ||
|
||
} | ||
return n; | ||
}; | ||
numberToMoney(100.2233567); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function count(array){ | ||
var count = {}; | ||
array.forEach(function(i) { count[i] = (count[i]||0)+1; }); | ||
} | ||
count(['a', 'a', 'b', 'b', 'b']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Array.prototype.remove = function(val) { | ||
if(isNaN(val) || val < 0 || val > this.length) { return false; } | ||
return this.splice(val, 1); | ||
} | ||
var someArray = [1, 2, 3]; | ||
|
||
someArray.remove(1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
function car(p,n){ | ||
console.log(p, n); | ||
if ( n == 1 ){ | ||
n = 80 / 100; | ||
return (p * n).toFixed(2); | ||
} | ||
if ( n == 2 ){ | ||
n = 80 / 100; | ||
var first = (p * n).toFixed(2); | ||
console.log(first); | ||
var second = (first * n).toFixed(2) | ||
console.log(second); | ||
return second; | ||
} | ||
if ( n == 3 ) { | ||
n = 80 / 100; | ||
var first = (p * n).toFixed(2); | ||
var second = (first * n).toFixed(2); | ||
var third = (second * 0.9).toFixed(2); | ||
return third; | ||
} | ||
if ( n == 4) { | ||
n = 80 / 100; | ||
var first = (p * n).toFixed(2); | ||
var second = (first * n).toFixed(2); | ||
var third = (second * 0.9).toFixed(2); | ||
var fourth = (third * 0.9).toFixed(2); | ||
return fourth; | ||
} | ||
if ( n == 5) { | ||
n = 80 / 100; | ||
var first = (p * n).toFixed(2); | ||
var second = (first * n).toFixed(2); | ||
var third = (second * 0.9).toFixed(2); | ||
var fourth = (third * 0.9).toFixed(2); | ||
var fifth = (fourth * 0.9).toFixed(2); | ||
return fifth; | ||
} | ||
} | ||
|
||
|
||
// better | ||
|
||
function car(p,n){ | ||
for( i=0;i<n;i++ ){ | ||
if( i<2 ) p = p*0.8; | ||
else p = p*0.9; | ||
} | ||
return p.toFixed(2); | ||
} | ||
|
||
// or | ||
|
||
function car(p ,n){ | ||
var i = 0; | ||
while (i++ < n) p *= (i < 3 ? .8 : .9); | ||
return p.toFixed(2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function dice(minimum, maximum){ | ||
console.log(minimum, maximum); | ||
var operation1 = Math.floor(Math.random() * (maximum - minimum + 1)); | ||
var operation2 = operation1 + minimum; | ||
console.log(operation1); | ||
console.log(operation2); | ||
var result = operation2; | ||
console.log(result); | ||
return result; | ||
} | ||
|
||
function dice(minimum, maximum){ | ||
return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum; | ||
} | ||
dice(300, 600); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var isSquare = function(arr){ | ||
if(arr.length == 0) | ||
return undefined; | ||
|
||
var allSquares = true; | ||
for(var i = 0; i < arr.length; i++) | ||
if(Math.sqrt(arr[i]) % 1 != 0) | ||
allSquares = false; | ||
return allSquares; | ||
} | ||
|
||
var count = 0; | ||
var isSquare = function(arr){ | ||
for (i=0; i<arr.length; i++) { | ||
if (Math.sqrt(arr[i]) % 1 === 0 ) { | ||
count = count + 1; | ||
console.log(count); | ||
} | ||
} | ||
if(count < arr.length || count > arr.length) { | ||
count = 0; | ||
return false; | ||
} | ||
else if(count === 0) { | ||
count = 0; | ||
return undefined; | ||
} | ||
else if(count == arr.length) { | ||
return true; | ||
} | ||
}; | ||
isSquare([1, 4, 9, 16, 25, 36]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
function findNextSquare(sq) { | ||
console.log(sq); | ||
var sqr = Math.sqrt(sq); | ||
console.log(sqr); | ||
var next = (sqr + 1) * (sqr + 1); | ||
console.log(next); | ||
if (sqr % 1 !== 0) return -1; | ||
if (next != sqr + 1) return next; | ||
else return -1; | ||
} | ||
findNextSquare(121); | ||
findNextSquare(151); | ||
|
||
// better | ||
|
||
function findNextSquare(sq) { | ||
return Math.sqrt(sq)%1? -1 : Math.pow(Math.sqrt(sq)+1,2); | ||
} | ||
|
||
// if sq % 1 return -1 | ||
// else return exponential (Math.sqrt(sq) +1 --> so 12 for example using 121, to the power of 2) --> so 144 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class SmallestIntegerFinder { | ||
findSmallestInt(args) { | ||
return Math.min.apply(Math, args); | ||
} | ||
} | ||
findSmallestInt([78,56,232,12,8]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function ghostBusters(building) { | ||
if(hasWhiteSpace(building) === false) { | ||
return "You just wanted my autograph didn't you?"; | ||
} | ||
else | ||
return building.replace(/ /g,''); | ||
} | ||
function hasWhiteSpace(building) { | ||
return building.indexOf(' ') >= 0; | ||
} | ||
ghostBusters("Bus Station"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
function primes(start, end){ | ||
var prime = []; | ||
var isPrime = function(num) { | ||
if(num < 2) return false; | ||
for (var i = 2; i < num; i++) { | ||
if(num%i === 0) return false; | ||
} | ||
return true; | ||
}; | ||
for(var i = start; i <= end; i++){ | ||
if(isPrime(i)) prime.push(i); | ||
} | ||
if(prime.length === 0) prime = null; | ||
return prime; | ||
} | ||
primes(1, 10); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function insertDash(num) { | ||
return String(num).replace | ||
(/([13579])(?=[13579])/g, '$1-') | ||
} | ||
insertDash(454793); // returns '4547-9-3' | ||
// ^ ^ | ||
// two odd numbers together are split with - | ||
// x = ([13579]) is followed by y -> (?= y) where y = [13579] | ||
// () = means capturing group... so -> $1 picks up the first and only capturing group |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
function validateHello(greetings) { | ||
return /(ahoj|ciao|czesc|h[ae]llo|hola|salut)/i.test(greetings) | ||
} | ||
|
||
validateHello(hallo); | ||
/* | ||
You received a whatsup message from an unknown number. Could it be from that girl/boy with a foreign accent you met yesterday evening? | ||
Write a simple regex to check if the string contains the world hallo in different languages. | ||
These are the languages of the possible people you met the night before: | ||
hello - english | ||
ciao - italian | ||
salut - french | ||
hallo - german | ||
hola - spanish | ||
ahoj - czech republic | ||
czesc - polish | ||
By the way, how cool is the czech repubblic hallo!! | ||
PS. you can assume the input is a string. PPS. to keep this a beginner exercise you don't need to check if the greeting is a subset of word ('Hallowen' can pass the test) | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
function lowercaseCount(str){ | ||
if (str.length === 0 ) { return 0;} | ||
if (str.length === null) { return 0;} | ||
if (str === undefined) { return 0;} | ||
else if (str.match(/[a-z]/g)) { | ||
return str.match(/[a-z]/g).length; | ||
} | ||
else return 0; | ||
} | ||
lowercaseCount("ABC123!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~"); | ||
lowercaseCount("abcABC123!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~"); | ||
|
||
|
||
//better: | ||
|
||
function lowercaseCount(str){ | ||
return (str.match(/[a-z]/g) || []).length | ||
} | ||
lowercaseCount("ABC123!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~"); | ||
lowercaseCount("abcABC123!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
function toDense(sparse){ | ||
for (i=0; i<=sparse.length-1; i++) { | ||
if (sparse[i] === null) { | ||
sparse.splice(sparse[i], 1); | ||
} | ||
} | ||
console.log(sparse); | ||
return sparse; | ||
} | ||
//90% working OR | ||
|
||
|
||
var sparse = [undefined, 2, null, , , 4, 6, null]; | ||
var newArray = []; | ||
function toDense(sparse) { | ||
for (var i = 0; i < sparse.length; i++) { | ||
if (sparse[i] === 0) { | ||
newArray.push(sparse[i]); | ||
} | ||
if (sparse[i]) { | ||
newArray.push(sparse[i]); | ||
} | ||
} | ||
console.log(newArray); | ||
return newArray; | ||
} | ||
toDense(sparse); | ||
|
||
// better below | ||
|
||
function toDense(sparse){ | ||
return sparse.filter(function(e){ | ||
return e !== null && e !== undefined; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<html><script> | ||
function toDense(sparse){ | ||
for (i=0; i<=sparse.length-1; i++) { | ||
if (sparse[i] === null) { | ||
sparse.splice(sparse[i], 1); | ||
} | ||
} | ||
console.log(sparse); | ||
return sparse; | ||
} | ||
//90% working OR | ||
|
||
|
||
var sparse = [undefined, 2, null, , , 4, 6, null]; | ||
var newArray = []; | ||
function toDense(sparse) { | ||
for (var i = 0; i < sparse.length; i++) { | ||
if (sparse[i] === 0) { | ||
newArray.push(sparse[i]); | ||
} | ||
if (sparse[i]) { | ||
newArray.push(sparse[i]); | ||
} | ||
} | ||
console.log(newArray); | ||
return newArray; | ||
} | ||
toDense(sparse); | ||
|
||
// better below | ||
|
||
function toDense(sparse){ | ||
return sparse.filter(function(e){ | ||
return e !== null && e !== undefined; | ||
}); | ||
} | ||
</script> | ||
</html> |
Oops, something went wrong.