Skip to content

Commit

Permalink
more js code_wars
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessandro Giordo committed Feb 8, 2016
1 parent a6b55c0 commit 11e2a70
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Js/formatting_decimals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function twoDecimalPlaces(n) {
return Math.round(n * 100) / 100
}

// or

function twoDecimalPlaces(n) {
return parseFloat(n.toFixed(2));
}

twoDecimalPlaces(4.659725356); //, 4.66

twoDecimalPlaces(2.675); //, 2.68
17 changes: 17 additions & 0 deletions Js/regex_match_0-9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
String.prototype.digit = function() {
return /^\d$/.test(this);
};

// mine is below


String.prototype.digit = function() {
if (/[a-z\s]/gi.test(this)) return false;
if (this>9) return false;
return /[0-9]/g.test(this);
};

''.digit() // , false);
'7'.digit() // , true);
' '.digit() //, false);
'14'.digit() //, false);
14 changes: 14 additions & 0 deletions Js/validate_regex_beginning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function validateCode(code){
return /^1|^2|^3/g.test(code);
}

// or

function validateCode(code){
return /^[123]/.test(code);
}


validateCode(123); // true
validateCode(321); // true
validateCode(421); // false

0 comments on commit 11e2a70

Please sign in to comment.