codefights isSubTress only one hide test case can not pass - javascript

Given two binary trees t1 and t2, determine whether the second tree is a subtree of the first tree. A subtree for vertex v in a binary tree t is a tree consisting of v and all its descendants in t. Determine whether or not there is a vertex v (possibly none) in tree t1 such that a subtree for vertex v (possibly empty) in t1 equals t2.
//
// Definition for binary tree:
// function Tree(x) {
// this.value = x;
// this.left = null;
// this.right = null;
// }
function isSubtree(t1, t2) {
function findroot(t1,t2){
if(t2==null){
return true;
}else if(t1==null){
if(t2==null){
return true;
}else{
return false;
}
}else if((
t1.value == t2.value && machedTree(t1,t2)) || findroot(t1.left,t2) || findroot(t1.right,t2)){
return true;
}else{
return false
}
}
function machedTree(t1,t2){
if((t1 == null && t2 == null)
&&(t1 == null && t2==null)){
return true;
}else if( t2 == null ||
((t1 != null && t2!=null)
&& machedTree(t1.left,t2.left)
&&t1.value == t2.value
&&machedTree(t1.right,t2.right))){
return true;
}else{
return false;
}
}
return findroot(t1,t2)
}
Has only one hide test case can not pass. do you have any idea where is wrong for the code

function isSubtree(t1, t2) {
function findroot(t1,t2){
if(t2==null){
return true;
}else if(t1==null){
return false;
}else if((
t1.value == t2.value && machedTree(t1,t2)) || findroot(t1.left,t2) || findroot(t1.right,t2)){
return true;
}else{
return false
}
}
function machedTree(t1,t2){
if(t1==null && t2 == null){
return true;
}
else if((t2 != null && t1 != null)
&& machedTree(t1.left,t2.left)
&& (t1.value === t2.value)
&&machedTree(t1.right,t2.right)){
return true;
}else{
return false;
}
}
return findroot(t1,t2)
}

Related

This function's cyclomatic complexity is too high. (38)

I have a switch case statement for my filters.
But it throws me complexity issues of 39 can u help me solve it??
Below is my sample code. It includes some more cases.
angular.forEach(docsClone, function(doc) {
var added = false;
angular.forEach($scope.filterOptions, function(opt) {
if (!added) {
switch (opt.AttributeId) {
case 'documentStatus':
if ((doc.documentStatus !== undefined && doc.documentStatus !== null && doc.documentStatus.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'planStatus':
if ((doc.planStatus !== undefined && doc.planStatus.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'planFamily':
if ((doc.planProductFamily !== undefined && doc.planProductFamily !== null && doc.planProductFamily.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'planYear':
planYear(doc, opt.AttributeValue, filteredDocs, added);
break;
case 'documentType':
if ((doc.documentType !== undefined && doc.documentType !== null && doc.documentType.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'businessEntity':
if ((doc.businessEntity !== undefined && doc.businessEntity !== null && doc.businessEntity.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'productClass':
if ((doc.productClass !== undefined && doc.productClass !== null && doc.productClass !== null && doc.productClass.indexOf(opt.AttributeValue) !== -1) ||
(doc.planProductClass !== undefined && doc.planProductClass.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
case 'productType':
if ((doc.productType !== undefined && doc.productType !== null && doc.productType.indexOf(opt.AttributeValue) !== -1) ||
(doc.planProductType !== undefined && doc.planProductType.indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
}
I see you have a lot of common conditions, why not break down into something common :
angular.forEach(docsClone, function(doc) {
var added = false;
angular.forEach($scope.filterOptions, function(opt) {
if (!added) {
switch (opt.AttributeId) {
case 'planStatus':
planYear(doc, opt.AttributeValue, filteredDocs, added);
break;
case 'documentType':
case 'documentStatus':
case 'planStatus':
case 'planFamily':
if ((doc[opt.AttributeId] !== undefined && doc[opt.AttributeId] !== null && doc[opt.AttributeId].indexOf(opt.AttributeValue) !== -1)) {
filteredDocs.push(doc);
added = true;
}
break;
}
}
});
}
You can generalise your case statements depending upon the comparisons / manipulations performed.

Validating numeric values using JavaScript

I have the following code. It works fine for blank fields, but it doesn't catch the other numeric exceptions. What am I doing wrong?
function validateForm() {
var a = document.forms["Form"]["percentage"].value;
var b = document.forms["Form"]["minutes"].value;
if (a == null || b == null || a == "" || b == "") {
alert("Please Fill All Required Field");
return false;
} else if (isNan(a) == true || isNan(b) == true) {
alert("Please enter valid numeric values");
return false;
} else if (parseInt(a) > 100) {
alert("Percentage can't exceed 100");
return false;
} else if (parseInt(b) < 0 || parseInt(a) < 0) {
alert("Values can't be negative");
return false;
}
}
Change this line:
else if((isNan(a)==true) ||(isNan(b)==true)){
to this:
else if (isNaN(a) || isNaN(b)) {
as the function is named #isNaN(). Using == true in conditionals is quite redundant, so I removed them.
I have also made a fiddle for you. It contains the fixed code, and it is working well.

Datatables multiple select afnFiltering

I have multiple select menus which are used to filter a table using jquery datatables.
The following code i use works brilliant, but for this example I am only using 3 select menus.
I now have a table which will be using upwards of 10.
Is there a better way of writing this so I don't have to write every variation of matches.
//UPDATE
If I put the select vars and the tabledata column vars in array can I iterate through them.
$.fn.dataTableExt.afnFiltering.push(
function( oSettings, aData, iDataIndex ) {
if ( oSettings.nTable == document.getElementById( 'logtable' ))
{
var nature_of_complaint = document.getElementById('nature_of_complaint_select').value;
var division = document.getElementById('division_select').value;
var resolved = document.getElementById('resolved_select').value;
var tabledata_nature_of_complaint = aData[22];
var tabledata_division = aData[12];
var tabledata_resolved = aData[26];
if (nature_of_complaint == "" && division == "" && resolved == "")
{ return true; }
else if (tabledata_division == division && nature_of_complaint == "" && resolved == "")
{ return true; }
else if (tabledata_nature_of_complaint == nature_of_complaint && division == "" && resolved == "")
{ return true; }
else if (tabledata_resolved == resolved && division == "" && nature_of_complaint == "")
{ return true; }
else if (tabledata_nature_of_complaint == nature_of_complaint && tabledata_division == division && resolved == "")
{ return true; }
else if (tabledata_division == division && tabledata_resolved == resolved && nature_of_complaint == "")
{ return true; }
else if (tabledata_resolved == resolved && tabledata_nature_of_complaint == nature_of_complaint && division == "")
{ return true; }
else if (tabledata_nature_of_complaint == nature_of_complaint && tabledata_division == division && tabledata_resolved == resolved)
{ return true; }
return false;
} else
return true;
}
);
Figured it out using this tutorial.
http://www.flynsarmy.com/2011/12/save-custom-filter-state-jquery-data-tables/
Just add class of 'dtable_filter' to each select.
Here is the reduced code:
$.fn.dataTableExt.afnFiltering = new Array();
var oControls = $('#adv_search_filters').find(':input[name]');
oControls.each(function() {
var oControl = $(this);
//Add custom filters
$.fn.dataTableExt.afnFiltering.push(function( oSettings, aData, iDataIndex ) {
if ( !oControl.val() || !oControl.hasClass('dtable_filter') ) return true;
for ( i=0; i<aData.length; i++ )
if ( aData[i].indexOf(oControl.val()) != -1 )
return true;
return false;
});
});

JavaScript: Parsing a string Boolean value? [duplicate]

This question already has answers here:
How can I convert a string to boolean in JavaScript?
(102 answers)
Closed 4 years ago.
JavaScript has parseInt() and parseFloat(), but there's no parseBool or parseBoolean method in the global scope, as far as I'm aware.
I need a method that takes strings with values like "true" or "false" and returns a JavaScript Boolean.
Here's my implementation:
function parseBool(value) {
return (typeof value === "undefined") ?
false :
// trim using jQuery.trim()'s source
value.replace(/^\s+|\s+$/g, "").toLowerCase() === "true";
}
Is this a good function? Please give me your feedback.
Thanks!
I would be inclined to do a one liner with a ternary if.
var bool_value = value == "true" ? true : false
Edit: Even quicker would be to simply avoid using the a logical statement and instead just use the expression itself:
var bool_value = value == 'true';
This works because value == 'true' is evaluated based on whether the value variable is a string of 'true'. If it is, that whole expression becomes true and if not, it becomes false, then that result gets assigned to bool_value after evaluation.
You can use JSON.parse for that:
JSON.parse("true"); //returns boolean true
It depends how you wish the function to work.
If all you wish to do is test for the word 'true' inside the string, and define any string (or nonstring) that doesn't have it as false, the easiest way is probably this:
function parseBoolean(str) {
return /true/i.test(str);
}
If you wish to assure that the entire string is the word true you could do this:
function parseBoolean(str) {
return /^true$/i.test(str);
}
You can try the following:
function parseBool(val)
{
if ((typeof val === 'string' && (val.toLowerCase() === 'true' || val.toLowerCase() === 'yes')) || val === 1)
return true;
else if ((typeof val === 'string' && (val.toLowerCase() === 'false' || val.toLowerCase() === 'no')) || val === 0)
return false;
return null;
}
If it's a valid value, it returns the equivalent bool value otherwise it returns null.
You can use JSON.parse or jQuery.parseJSON and see if it returns true using something like this:
function test (input) {
try {
return !!$.parseJSON(input.toLowerCase());
} catch (e) { }
}
last but not least, a simple and efficient way to do it with a default value :
ES5
function parseBool(value, defaultValue) {
return (value == 'true' || value == 'false' || value === true || value === false) && JSON.parse(value) || defaultValue;
}
ES6 , a shorter one liner
const parseBool = (value, defaultValue) => ['true', 'false', true, false].includes(value) && JSON.parse(value) || defaultValue
JSON.parse is efficient to parse booleans
Personally I think it's not good, that your function "hides" invalid values as false and - depending on your use cases - doesn't return true for "1".
Another problem could be that it barfs on anything that's not a string.
I would use something like this:
function parseBool(value) {
if (typeof value === "string") {
value = value.replace(/^\s+|\s+$/g, "").toLowerCase();
if (value === "true" || value === "false")
return value === "true";
}
return; // returns undefined
}
And depending on the use cases extend it to distinguish between "0" and "1".
(Maybe there is a way to compare only once against "true", but I couldn't think of something right now.)
Why not keep it simple?
var parseBool = function(str) {
if (typeof str === 'string' && str.toLowerCase() == 'true')
return true;
return (parseInt(str) > 0);
}
You can add this code:
function parseBool(str) {
if (str.length == null) {
return str == 1 ? true : false;
} else {
return str == "true" ? true : false;
}
}
Works like this:
parseBool(1) //true
parseBool(0) //false
parseBool("true") //true
parseBool("false") //false
Wood-eye be careful.
After looking at all this code, I feel obligated to post:
Let's start with the shortest, but very strict way:
var str = "true";
var mybool = JSON.parse(str);
And end with a proper, more tolerant way:
var parseBool = function(str)
{
// console.log(typeof str);
// strict: JSON.parse(str)
if(str == null)
return false;
if (typeof str === 'boolean')
{
if(str === true)
return true;
return false;
}
if(typeof str === 'string')
{
if(str == "")
return false;
str = str.replace(/^\s+|\s+$/g, '');
if(str.toLowerCase() == 'true' || str.toLowerCase() == 'yes')
return true;
str = str.replace(/,/g, '.');
str = str.replace(/^\s*\-\s*/g, '-');
}
// var isNum = string.match(/^[0-9]+$/) != null;
// var isNum = /^\d+$/.test(str);
if(!isNaN(str))
return (parseFloat(str) != 0);
return false;
}
Testing:
var array_1 = new Array(true, 1, "1",-1, "-1", " - 1", "true", "TrUe", " true ", " TrUe", 1/0, "1.5", "1,5", 1.5, 5, -3, -0.1, 0.1, " - 0.1", Infinity, "Infinity", -Infinity, "-Infinity"," - Infinity", " yEs");
var array_2 = new Array(null, "", false, "false", " false ", " f alse", "FaLsE", 0, "00", "1/0", 0.0, "0.0", "0,0", "100a", "1 00", " 0 ", 0.0, "0.0", -0.0, "-0.0", " -1a ", "abc");
for(var i =0; i < array_1.length;++i){ console.log("array_1["+i+"] ("+array_1[i]+"): " + parseBool(array_1[i]));}
for(var i =0; i < array_2.length;++i){ console.log("array_2["+i+"] ("+array_2[i]+"): " + parseBool(array_2[i]));}
for(var i =0; i < array_1.length;++i){ console.log(parseBool(array_1[i]));}
for(var i =0; i < array_2.length;++i){ console.log(parseBool(array_2[i]));}
I like the solution provided by RoToRa (try to parse given value, if it has any boolean meaning, otherwise - don't). Nevertheless I'd like to provide small modification, to have it working more or less like Boolean.TryParse in C#, which supports out params. In JavaScript it can be implemented in the following manner:
var BoolHelpers = {
tryParse: function (value) {
if (typeof value == 'boolean' || value instanceof Boolean)
return value;
if (typeof value == 'string' || value instanceof String) {
value = value.trim().toLowerCase();
if (value === 'true' || value === 'false')
return value === 'true';
}
return { error: true, msg: 'Parsing error. Given value has no boolean meaning.' }
}
}
The usage:
var result = BoolHelpers.tryParse("false");
if (result.error) alert(result.msg);
stringjs has a toBoolean() method:
http://stringjs.com/#methods/toboolean-tobool
S('true').toBoolean() //true
S('false').toBoolean() //false
S('hello').toBoolean() //false
S(true).toBoolean() //true
S('on').toBoolean() //true
S('yes').toBoolean() //true
S('TRUE').toBoolean() //true
S('TrUe').toBoolean() //true
S('YES').toBoolean() //true
S('ON').toBoolean() //true
S('').toBoolean() //false
S(undefined).toBoolean() //false
S('undefined').toBoolean() //false
S(null).toBoolean() //false
S(false).toBoolean() //false
S({}).toBoolean() //false
S(1).toBoolean() //true
S(-1).toBoolean() //false
S(0).toBoolean() //false
I shamelessly converted Apache Common's toBoolean to JavaScript:
JSFiddle: https://jsfiddle.net/m2efvxLm/1/
Code:
function toBoolean(str) {
if (str == "true") {
return true;
}
if (!str) {
return false;
}
switch (str.length) {
case 1: {
var ch0 = str.charAt(0);
if (ch0 == 'y' || ch0 == 'Y' ||
ch0 == 't' || ch0 == 'T' ||
ch0 == '1') {
return true;
}
if (ch0 == 'n' || ch0 == 'N' ||
ch0 == 'f' || ch0 == 'F' ||
ch0 == '0') {
return false;
}
break;
}
case 2: {
var ch0 = str.charAt(0);
var ch1 = str.charAt(1);
if ((ch0 == 'o' || ch0 == 'O') &&
(ch1 == 'n' || ch1 == 'N') ) {
return true;
}
if ((ch0 == 'n' || ch0 == 'N') &&
(ch1 == 'o' || ch1 == 'O') ) {
return false;
}
break;
}
case 3: {
var ch0 = str.charAt(0);
var ch1 = str.charAt(1);
var ch2 = str.charAt(2);
if ((ch0 == 'y' || ch0 == 'Y') &&
(ch1 == 'e' || ch1 == 'E') &&
(ch2 == 's' || ch2 == 'S') ) {
return true;
}
if ((ch0 == 'o' || ch0 == 'O') &&
(ch1 == 'f' || ch1 == 'F') &&
(ch2 == 'f' || ch2 == 'F') ) {
return false;
}
break;
}
case 4: {
var ch0 = str.charAt(0);
var ch1 = str.charAt(1);
var ch2 = str.charAt(2);
var ch3 = str.charAt(3);
if ((ch0 == 't' || ch0 == 'T') &&
(ch1 == 'r' || ch1 == 'R') &&
(ch2 == 'u' || ch2 == 'U') &&
(ch3 == 'e' || ch3 == 'E') ) {
return true;
}
break;
}
case 5: {
var ch0 = str.charAt(0);
var ch1 = str.charAt(1);
var ch2 = str.charAt(2);
var ch3 = str.charAt(3);
var ch4 = str.charAt(4);
if ((ch0 == 'f' || ch0 == 'F') &&
(ch1 == 'a' || ch1 == 'A') &&
(ch2 == 'l' || ch2 == 'L') &&
(ch3 == 's' || ch3 == 'S') &&
(ch4 == 'e' || ch4 == 'E') ) {
return false;
}
break;
}
default:
break;
}
return false;
}
console.log(toBoolean("yEs")); // true
console.log(toBoolean("yES")); // true
console.log(toBoolean("no")); // false
console.log(toBoolean("NO")); // false
console.log(toBoolean("on")); // true
console.log(toBoolean("oFf")); // false
Inspect this element, and view the console output.
Enough to using eval javascript function to convert string to boolean
eval('true')
eval('false')

phone number validation with added input

I recently filled out a form and when I got to the phone number textBox I noticed some really cool things going on. As I entered my number, general phone symbols were getting added automatically. Example:
I start entering my area code '555'
and my input was changed to 1 (555)
to test what just happened I backspaced on the ) and it quickly added it back in.
So my question is, how do I get this input to happen?
I use a javascript library called automask - you dont see the mask but it wont let you type anything outside the mask
for instance if your mask is ###-###-#### then any other characters are ignored (ie not 0-9) and the dashes are put in automatically.
I can post the library if you would like to take a look at
example of implementation
<input type=text name=ssn onkeypress="return autoMask(this,event, '###-##-####');">
// email kireol at yahoo.com
// autoMask - an adaption of anyMask
//
// this will force #'s, not allowing alphas where the #'s are, and auto add -'s
function autoMask(field, event, sMask) {
//var sMask = "**?##?####";
var KeyTyped = String.fromCharCode(getKeyCode(event));
var targ = getTarget(event);
keyCount = targ.value.length;
if (getKeyCode(event) < 32)
{
return true;
}
if(keyCount == sMask.length && getKeyCode(event) > 32)
{
return false;
}
if ((sMask.charAt(keyCount+1) != '#') && (sMask.charAt(keyCount+1) != 'A' ) && (sMask.charAt(keyCount+1) != '~' ))
{
field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
return false;
}
if (sMask.charAt(keyCount) == '*')
return true;
if (sMask.charAt(keyCount) == KeyTyped)
{
return true;
}
if ((sMask.charAt(keyCount) == '~') && isNumeric_plusdash(KeyTyped))
return true;
if ((sMask.charAt(keyCount) == '#') && isNumeric(KeyTyped))
return true;
if ((sMask.charAt(keyCount) == 'A') && isAlpha(KeyTyped))
return true;
if ((sMask.charAt(keyCount+1) == '?') )
{
field.value = field.value + KeyTyped + sMask.charAt(keyCount+1);
return true;
}
return false;
}
function getTarget(e) {
// IE5
if (e.srcElement) {
return e.srcElement;
}
if (e.target) {
return e.target;
}
}
function getKeyCode(e) {
//IE5
if (e.srcElement) {
return e.keyCode
}
// NC5
if (e.target) {
return e.which
}
}
function isNumeric(c)
{
var sNumbers = "01234567890";
if (sNumbers.indexOf(c) == -1)
return false;
else
return true;
}
function isNumeric_plusdash(c)
{
var sNumbers = "01234567890-";
if (sNumbers.indexOf(c) == -1)
return false;
else
return true;
}
function isAlpha(c)
{
var lCode = c.charCodeAt(0);
if (lCode >= 65 && lCode <= 122 )
{
return true;
}
else
return false;
}
function isPunct(c)
{
var lCode = c.charCodeAt(0);
if (lCode >= 32 && lCode <= 47 )
{
return true;
}
else
return false;
}
If this was an aspx page, they were probably using the AJAX Control Toolkit MaskedEdit Extender. There is also the Masked Input plugin for jQuery.

Categories