JavaScript: PigLatin!

Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an “ay”. If a word begins with a vowel you just add “way” to the end.

function translatePigLatin(str) {
 var firstVowelIndex =0;
 
 while(str.charCodeAt(firstVowelIndex)!=97 && 
str.charCodeAt(firstVowelIndex)!=101 && 
str.charCodeAt(firstVowelIndex)!=105 && 
str.charCodeAt(firstVowelIndex)!=111 && 
str.charCodeAt(firstVowelIndex)!=117 &&
str.charCodeAt(firstVowelIndex)!=65 && 
str.charCodeAt(firstVowelIndex)!=69 && 
str.charCodeAt(firstVowelIndex)!=73 && 
str.charCodeAt(firstVowelIndex)!=79 && 
str.charCodeAt(firstVowelIndex)!=85){
       firstVowelIndex++;
 }
 
 if(firstVowelIndex===0){
      str = str + "way";
 }else{
      str = str.substr(firstVowelIndex) + str.substr(0,firstVowelIndex) + "ay";
 }
 return str;
}

JavaScript: Convert to Roman Numbers

 

function convertToRoman(num) {
var romanNumberMap = {
1: "I",
2: "II",
3: "III",
4: "IV",
5: "V",
6: "VI",
7: "VII",
8: "VIII",
9: "IX",
10: "X",
20: "XX",
30: "XXX",
40: "XL",
50: "L",
60: "LX",
70: "LXX",
80: "LXXX",
90: "XC",
100: "C",
200: "CC",
300: "CCC",
400: "CD",
500: "D",
600: "DC",
700: "DCC",
800: "DCCC",
900: "CM",
1000: "M",
2000: "MM",
3000: "MMM",
4000: "MV",
5000: "V",
6000: "VM",
7000: "VMM",
8000: "VMMM"
};
 
 var resultArr = [];
 var numStr = num.toString();
 var multiplier = 1;
 
 for (var count=numStr.length-1; count>=0; --count){
 resultArr.unshift(romanNumberMap[parseInt(numStr[count])*multiplier]);
 multiplier*=10;
 }
 
 return resultArr.join('');
}

JavaScript: Array Manipulations

Remove all elements from the initial array that are of the same value as the arguments:

function destroyer(arr) {
 for(var j=1; j<arguments.length; j++){
 arr = arr.filter(checkVal, arguments[j]);
 } 
 return arr;
}

function checkVal(value){
 return (value != this);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return[].
destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].

Return Largest Number in an Array:

function largestOfFour(arr) {
 var resultArr=[];
 
 for(var i=0; i<arr.length;i++){
 var maxInt=0;
 
 for(var j=0; j<arr[i].length; j++){
 maxInt = Math.max(maxInt, arr[i][j]);
 }
 
 resultArr[i] = maxInt;
 }
 
 return resultArr;
}

 

Find Index to Insert an Item in an Array:

function getIndexToIns(arr, num) {
 arr = arr.sort(function(a,b){
 return a>b;
 });
 
 var i=0;
 
 while(i<arr.length && arr[i]<num){
 i++;
 }
 
 return i;
}

Find all items that exist in either one of two arrays:

function diffArray(arr1, arr2) {
 var newArr = [];
 var newArr2 = [];

newArr = arr1.filter(function(elem, index, array) {
 for(var i=0; i<arr2.length; i++){
 if(elem==arr2[i]) return false;
 }
 return true;
 });

newArr2 = arr2.filter(function(elem, index, array) {
 for(var i=0; i<arr1.length; i++){
 if(elem==arr1[i]) return false;
 }
 return true;
 });
 
 return newArr.concat(newArr2);
}

 

 

 

Javascript: There are two isNaN functions!

Javascript has two separate functions to test whether the value being tested is a Number or not.

isNaN()

The first one is a global isNan() function, which strictly tests whether the test value is a Number or not. So if you pass a string to this function, it will return true. For example, the following will all return true (i.e. the following arguments are all strictly Not A Number)

isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN(undefined) //true
isNaN('NaN') //true
isNaN(NaN) //true
isNaN(0 / 0) //true

The global isNaN() function, converts the tested value to a Number, then tests it. This may not be the behavior you are looking for, though.

Number.isNaN()

Number.isNan() does not convert the values to a Number, and will not return true for any value that is not of the type Number. So it has the following behavior:

Number.isNaN(123) //false
Number.isNaN(-1.23) //false
Number.isNaN(5-2) //false
Number.isNaN(0) //false
Number.isNaN('123') //false
Number.isNaN('Hello') //false
Number.isNaN('2005/12/12') //false
Number.isNaN('') //false
Number.isNaN(true) //false
Number.isNaN(undefined) //false
Number.isNaN('NaN') //false
Number.isNaN(NaN) //true
Number.isNaN(0 / 0) //true

Javascript: String Manipulations

Check String for Palindrome:

function palindrome(str) {
var result = true;
 var newStr='';
 
 for(var i=0;i<str.length;i++){
 if(/[a-zA-Z0-9]/gi.test(str.charAt(i))){
 newStr+= str.charAt(i);
 }
 }
 
 var revStr='';
for(var j=0;j<newStr.length; j++){
 if(newStr.charAt(j).toLowerCase() != newStr.charAt(newStr.length-j-1).toLowerCase()){
 result = false;
 }
}
 return result;
}

Find the Longest Word in a String:

function findLongestWord(str) {
 var arrStr = str.split(' ');
 var maxLength=0;
 
 for(var i=0;i<arrStr.length;i++){
 maxLength = Math.max(arrStr[i].length,maxLength);

}
 return maxLength;
}

Change the String to Title Case:

function titleCase(str) {
 var arrStr = str.split(' ');
 
 for(var i=0; i<arrStr.length; i++){
 arrStr[i] = arrStr[i].charAt(0).toUpperCase() + arrStr[i].substr(1).toLowerCase();
 }
 
 str = arrStr.join(' ');
 return str;
}

 Shift Cipher (Unshift by 13):

function rot13(str) { 
 var resultStr = '';
 
 for(var i=0; i<str.length; i++){
 if(str.charCodeAt(i)>=65 & str.charCodeAt(i)<=90){
 if(str.charCodeAt(i)<78){
 resultStr+= String.fromCharCode(str.charCodeAt(i)+13);
 }else{
 resultStr+= String.fromCharCode(str.charCodeAt(i)-13);
 }
 }else{
 resultStr+= str.charAt(i);
 }
 }
 return resultStr;
}

 

Documentum DFC: How to get custom attributes

Here is a way to get all the custom attributes of an object in DFC without knowing them ahead of time:

                //Custom Attributes
                String allAttributes = dctmFileObject.dump();
                String customAttributes = allAttributes.substring(allAttributes.indexOf("language_code"), allAttributes.indexOf("SYSTEM ATTRIBUTES"));
                
                try (BufferedReader reader = new BufferedReader(new StringReader(customAttributes))) {
                    String line = reader.readLine().trim();
                    while (line != null && !line.isEmpty()) {
                        String attributeName = line.substring(0, line.indexOf(':')).trim();
                        String attributeValue = line.substring(line.indexOf(':')).trim();
                        int attributeDataType = dctmFileObject.getAttr(dctmFileObject.findAttrIndex(attributeName)).getDataType();

                        line = reader.readLine();
                    }
                } catch (IOException exc) {
                    // quit
                }