Skip to content

Commit

Permalink
repository global
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Giordo committed Feb 8, 2016
1 parent 7f533d0 commit c207564
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Code Wars Practice scripts
======
15 changes: 15 additions & 0 deletions Js/VAT_calc.js
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);
9 changes: 9 additions & 0 deletions Js/add_missing_numbers_array.js
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])
4 changes: 4 additions & 0 deletions Js/array_map_images.js
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"]);
15 changes: 15 additions & 0 deletions Js/contructor_function.js
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
17 changes: 17 additions & 0 deletions Js/convert_to_money.js
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);
5 changes: 5 additions & 0 deletions Js/counting_array_elements.js
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']);
7 changes: 7 additions & 0 deletions Js/create_remove_function_prototype.js
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);
58 changes: 58 additions & 0 deletions Js/depreciation-car.js
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);
}
15 changes: 15 additions & 0 deletions Js/dice_script.js
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);
32 changes: 32 additions & 0 deletions Js/find_if_squared.js
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]);
21 changes: 21 additions & 0 deletions Js/find_next_squared.js
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
6 changes: 6 additions & 0 deletions Js/find_smallest_integer.js
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]);
11 changes: 11 additions & 0 deletions Js/find_whitespace_regex.js
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");
16 changes: 16 additions & 0 deletions Js/prime_practice.js
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);
9 changes: 9 additions & 0 deletions Js/regex-positive-lookahead.js
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
23 changes: 23 additions & 0 deletions Js/regex.test.js
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)
*/
20 changes: 20 additions & 0 deletions Js/regex_count_lowercase.js
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!@€£#$%^&*()_-+=}{[]|\':;?/>.<,~");
35 changes: 35 additions & 0 deletions Js/sparse_dense.js
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;
});
}
38 changes: 38 additions & 0 deletions Js/sparse_to_dense_array.html
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>
Loading

0 comments on commit c207564

Please sign in to comment.