diff --git a/SQL/sql_basic_sum.sql b/SQL/sql_basic_sum.sql index af4effb..014bf85 100644 --- a/SQL/sql_basic_sum.sql +++ b/SQL/sql_basic_sum.sql @@ -5,7 +5,7 @@ * MySQL version: 5.5.56-log * * * * In this MySQL challenge, * -* your query should return LastName & the sum of Age from your table for all users with a LastName = Smith* +* your query should return LastName & the sum of Age from your table for all users with a LastName = Smith* * The column title of the summed ages should be SumAge. Your output should look like the following table. * * * * +----------+---------+ * @@ -15,6 +15,7 @@ * +----------+---------+ * * * *****************************************************************************************************************/ + SELECT LastName, SUM(Age) AS SumAge diff --git a/SQL/sql_user_logins.sql b/SQL/sql_user_logins.sql index 0cd1699..895d1a1 100644 --- a/SQL/sql_user_logins.sql +++ b/SQL/sql_user_logins.sql @@ -38,8 +38,8 @@ * Problem Statement * * In this MySQL challenge, the table provided shows all new users signing up on a specific * * date in the format YYYY-MM-DD. Your query should output the change from one month to the * - * next. Because the first month has no preceding month, your output should skip that row. * - * Your output should look like the following table. * + * next. Because the first month has no preceding month, your output should skip that row. * + * Your output should look like the following table. * * * * * * +-----------------+---------------------+ * @@ -53,9 +53,7 @@ * * ********************************************************************************************/ -/* - SOLUTION ONE WITHOUT LAG FUNCTION -*/ +-- SOLUTION ONE WITHOUT LAG FUNCTION SELECT MONTHNAME(STR_TO_DATE(MONTH(DateJoined), '%m')) AS Month, date1 - previous AS MonthToMonthChange @@ -80,10 +78,7 @@ FROM ) AS SubQuery2 WHERE previous > 0 -/* - SOLUTION 2 WITH LAG FUNCTION, USED IN MYSQL VERSION 8.0+ -*/ - +-- SOLUTION 2 WITH LAG FUNCTION, USED IN MYSQL VERSION 8.0+ SELECT MONTHNAME(STR_TO_DATE(MONTH(DateJoined), '%m')) AS Month, date1 - date2 AS MonthToMonthChange diff --git a/SQL/sql_vendor_join.sql b/SQL/sql_vendor_join.sql index a9376f8..b3ed8c9 100644 --- a/SQL/sql_vendor_join.sql +++ b/SQL/sql_vendor_join.sql @@ -14,7 +14,7 @@ * +---------+---------------------+-------+ * * | 27| Machinx | 1| * * | 5| WaterBus Enterprise | 1| * - * | 36| Johnson and Sons | 2| * + * | 36| Johnson and Sons | 2| * * | 35| Shipping & Co. | 3| * * | 6| Alloy LLC | 3| * * | 40| FireConsulting | 5| * diff --git a/math_fundamentals/Binary_Converter.ts b/math_fundamentals/Binary_Converter.ts index 69de6dc..f639bc8 100644 --- a/math_fundamentals/Binary_Converter.ts +++ b/math_fundamentals/Binary_Converter.ts @@ -18,16 +18,16 @@ * * ***************************************************************/ -function BinaryConverter( str: string ): number { - let sum: number = 0; - for( let i:number = str.length-1; i >= 0; i-- ) { - if( str[i] === '1' ) { - sum += Math.pow( 2, ( str.length - 1 ) - i ); - } +function BinaryConverter(str: string): number { + let sum: number = 0; + for (let i: number = str.length - 1; i >= 0; i--) { + if (str[i] === "1") { + sum += Math.pow(2, str.length - 1 - i); } - return sum; + } + return sum; } - + // KEEP THIS FUNCTION CALL HERE // @ts-ignore -console.log( BinaryConverter( readline() ) ); \ No newline at end of file +console.log(BinaryConverter(readline())); diff --git a/math_fundamentals/Consecutive.ts b/math_fundamentals/Consecutive.ts index ee24b93..0ed5011 100644 --- a/math_fundamentals/Consecutive.ts +++ b/math_fundamentals/Consecutive.ts @@ -21,15 +21,15 @@ * * ***************************************************************/ -function Consecutive( arr: Array ): number { - let number_of_consecutives = 0; - arr = arr.sort( ( a,b ) => a - b ); - for( let i: number = arr[0]; i<=arr[arr.length-1]; i++ ) { - number_of_consecutives++; - } - return number_of_consecutives - arr.length; +function Consecutive(arr: Array): number { + let number_of_consecutives = 0; + arr = arr.sort((a, b) => a - b); + for (let i: number = arr[0]; i <= arr[arr.length - 1]; i++) { + number_of_consecutives++; + } + return number_of_consecutives - arr.length; } -// keep this function call here +// keep this function call here // @ts-ignore -console.log(Consecutive(readline())); \ No newline at end of file +console.log(Consecutive(readline())); diff --git a/math_fundamentals/Happy_Numbers.js b/math_fundamentals/Happy_Numbers.js index a40e105..5a53ca7 100644 --- a/math_fundamentals/Happy_Numbers.js +++ b/math_fundamentals/Happy_Numbers.js @@ -3,53 +3,44 @@ * * * Problem Statement * * Have the function HappyNumbers(num) determine if a number is * - * Happy, which is a number whose sum of the square of the * + * Happy, which is a number whose sum of the square of the * * digits eventually converges to 1. Return true if it's a Happy* - * number, otherwise return false. * + * number, otherwise return false. * * * - * For example: the number 10 is Happy because 1^2 + 0^2 * - * converges to 1. * + * For example: the number 10 is Happy because 1^2 + 0^2 * + * converges to 1. * * * * Examples * - * Input 1: 1 * + * Input 1: 1 * * Output 1: true * * * - * Input 2: 101 * + * Input 2: 101 * * Output 2: false * * * ***************************************************************/ - -function Calculate_Sum_Of_Square_Of_Digits( number ) -{ - let sum = 0; - for( let i=0; i=0; i-- ){ - str += s[i]; - } - return str; +function reverseString(s) { + let str = ""; + for (let i = s.length - 1; i >= 0; i--) { + str += s[i]; + } + return str; } -function PalindromicSubstring( str ) { - let PalindromicSubstrings = []; - for( let i=0; i max) { - max = PalindromicSubstrings[i].length; - index = i; - } + } + let max = -1; + let index = -1; + for (let i = 0; i < PalindromicSubstrings.length; i++) { + if (PalindromicSubstrings[i].length > max) { + max = PalindromicSubstrings[i].length; + index = i; } - return index === -1 ? "none" : PalindromicSubstrings[index]; + } + return index === -1 ? "none" : PalindromicSubstrings[index]; } - + // KEEP THIS FUNCTION CALL HERE -console.log(PalindromicSubstring(readline())); \ No newline at end of file +console.log(PalindromicSubstring(readline())); diff --git a/string manipulation/Calculator.js b/string manipulation/Calculator.js index 1de7597..804eb0e 100644 --- a/string manipulation/Calculator.js +++ b/string manipulation/Calculator.js @@ -2,7 +2,7 @@ * CODERBYTE CALCULATOR CHALLENGE * * * * Problem Statement * - * Have the function Calculator(str) take the str parameter * + * Have the function Calculator(str) take the str parameter * * being passed and evaluate the mathematical expression within * * in. For example, if str were "2+(3-1)*3" the output should * * be 8. Another example: if str were "(2-0)(6/2)" the output * @@ -26,46 +26,39 @@ * * ***************************************************************/ -function parse( str ) { - return Function(`'use strict'; return (${str})`)() +function parse(str) { + return Function(`'use strict'; return (${str})`)(); } -function Calculator(string) { +function Calculator(string) { let evaluateExpression = new String(""); let splittedString = string.split(""); let flag = false; - for( let i=0; i 0 && string[i] !== temp && ab_counter === 3) return true; + for (let i = 0; i < string.length; i++) { + if (string[i] === "a" || string[i] === "b") { + if (temp.length > 0 && string[i] !== temp && ab_counter === 3) return true; ab_counter = 0; temp = string[i]; - } - else ab_counter++; + } else ab_counter++; } return false; } // KEEP THIS FUNCTION CALL HERE. -console.log(ABCheck(readline())); \ No newline at end of file +console.log(ABCheck(readline())); diff --git a/string manipulation/alphabet_searching.js b/string manipulation/alphabet_searching.js index 8d527c4..93d8d39 100644 --- a/string manipulation/alphabet_searching.js +++ b/string manipulation/alphabet_searching.js @@ -19,21 +19,20 @@ * Output 2: false * ***************************************************************/ -function AlphabetSearching( string ) { - //SORT THE STRING & REMOVE DUPLICATES FROM THE STRING - let sortedStringArray = Array.from( new Set( string.split("").sort() ) ); - //COUNT THE ENGLISH ALPHABET CHARACTERS - let count = 0; - - //LOOP THROUGH STRING - for ( let i = 0; i < sortedStringArray.length; i++ ) { - if ( sortedStringArray[i].toLowerCase() >= "a" && sortedStringArray[i].toLowerCase() <= "z" ) { - count++; - } - } +function AlphabetSearching(string) { + //SORT THE STRING & REMOVE DUPLICATES FROM THE STRING + let sortedStringArray = Array.from(new Set(string.split("").sort())); + //COUNT THE ENGLISH ALPHABET CHARACTERS + let count = 0; - return ( count === 26 ) ? true : false; + //LOOP THROUGH STRING + for (let i = 0; i < sortedStringArray.length; i++) { + if (sortedStringArray[i].toLowerCase() >= "a" && sortedStringArray[i].toLowerCase() <= "z") { + count++; + } + } + return count === 26 ? true : false; } // KEEP THIS FUNCTION CALL HERE diff --git a/string manipulation/alphabet_soup.js b/string manipulation/alphabet_soup.js index 772a908..8512362 100644 --- a/string manipulation/alphabet_soup.js +++ b/string manipulation/alphabet_soup.js @@ -21,7 +21,7 @@ ***************************************************************/ function AlphabetSoup(str) { - return str.split("").sort().join(""); + return str.split("").sort().join(""); } // KEEP THIS FUNCTION CALL HERE diff --git a/string manipulation/ascii_convertion.js b/string manipulation/ascii_convertion.js index 43602d4..4f63210 100644 --- a/string manipulation/ascii_convertion.js +++ b/string manipulation/ascii_convertion.js @@ -17,20 +17,19 @@ * Output 2: 979899 4242 * ***************************************************************/ -function ASCIIConversion( str ) { - let asciiValueString = new String(""); +function ASCIIConversion(str) { + let asciiValueString = new String(""); - for ( let i = 0; i < str.length; i++ ) { - if ( str[i] === " " ) { - asciiValueString += str[i]; - } - else { - asciiValueString += str.charCodeAt(i); - } + for (let i = 0; i < str.length; i++) { + if (str[i] === " ") { + asciiValueString += str[i]; + } else { + asciiValueString += str.charCodeAt(i); } + } - return asciiValueString; + return asciiValueString; } // KEEP THIS FUNCTION CALL HERE -console.log(ASCIIConversion(readline())); \ No newline at end of file +console.log(ASCIIConversion(readline())); diff --git a/string manipulation/basic_roman_numerals.js b/string manipulation/basic_roman_numerals.js index 08935ed..03feeef 100644 --- a/string manipulation/basic_roman_numerals.js +++ b/string manipulation/basic_roman_numerals.js @@ -24,16 +24,17 @@ ***************************************************************/ function BasicRomanNumerals(str) { - const NumeralsArray = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 }; - let numeralString = str; - let currentIndex = numeralString[0]; - let numberTotal = NumeralsArray[currentIndex]; - for (let i = 1; i < numeralString.length; i++) { - if (NumeralsArray[currentIndex] >= NumeralsArray[numeralString[i]]) numberTotal += NumeralsArray[numeralString[i]]; - else numberTotal = numberTotal - NumeralsArray[numeralString[i - 1]] * 2 + NumeralsArray[numeralString[i]]; - currentIndex = numeralString[i]; - } - return numberTotal; + const NumeralsArray = { I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000 }; + let numeralString = str; + let currentIndex = numeralString[0]; + let numberTotal = NumeralsArray[currentIndex]; + for (let i = 1; i < numeralString.length; i++) { + if (NumeralsArray[currentIndex] >= NumeralsArray[numeralString[i]]) + numberTotal += NumeralsArray[numeralString[i]]; + else numberTotal = numberTotal - NumeralsArray[numeralString[i - 1]] * 2 + NumeralsArray[numeralString[i]]; + currentIndex = numeralString[i]; + } + return numberTotal; } // KEEP THIS FUNCTION CALL HERE console.log(BasicRomanNumerals(readline())); diff --git a/string manipulation/binary_reversal.js b/string manipulation/binary_reversal.js index 05a445f..87efa12 100644 --- a/string manipulation/binary_reversal.js +++ b/string manipulation/binary_reversal.js @@ -27,11 +27,13 @@ function BinaryReversal(str) { let paddedBinaryRepresentation = (+str).toString(2); - while(paddedBinaryRepresentation.length % 8 !== 0) paddedBinaryRepresentation = String.fromCharCode(48) + paddedBinaryRepresentation; + while (paddedBinaryRepresentation.length % 8 !== 0) + paddedBinaryRepresentation = String.fromCharCode(48) + paddedBinaryRepresentation; let reversePaddedBinaryRepresentation = new String(""); - for(let i=paddedBinaryRepresentation.length-1; i>=0; i--) reversePaddedBinaryRepresentation += paddedBinaryRepresentation[i]; + for (let i = paddedBinaryRepresentation.length - 1; i >= 0; i--) + reversePaddedBinaryRepresentation += paddedBinaryRepresentation[i]; return parseInt(reversePaddedBinaryRepresentation, 2); } // KEEP THIS FUNCTION CALL HERE -console.log(BinaryReversal(readline())); \ No newline at end of file +console.log(BinaryReversal(readline())); diff --git a/string manipulation/caesar_cipher.js b/string manipulation/caesar_cipher.js index ccc8431..9afb3da 100644 --- a/string manipulation/caesar_cipher.js +++ b/string manipulation/caesar_cipher.js @@ -26,21 +26,19 @@ ***************************************************************/ function CaesarCipher(string, number) { - let caesarCiper = new String(""); - for(let i=0; i= 97 && value <= 122) caesarCiper += String.fromCharCode(string.charCodeAt(i)+number); - else if(value > 122) caesarCiper += String.fromCharCode(96+(value - 122)); - } - else if(string[i].match(/^[A-Z]+$/)) { - let value = Number(string.charCodeAt(i)+number); - if(value >= 65 && value <= 90) caesarCiper += String.fromCharCode(string.charCodeAt(i)+number); - else if(value > 90) caesarCiper += String.fromCharCode(64+(value - 90)); - } - else caesarCiper += string[i]; + let caesarCiper = new String(""); + for (let i = 0; i < string.length; i++) { + if (string[i].match(/^[a-z]+$/)) { + let value = Number(string.charCodeAt(i) + number); + if (value >= 97 && value <= 122) caesarCiper += String.fromCharCode(string.charCodeAt(i) + number); + else if (value > 122) caesarCiper += String.fromCharCode(96 + (value - 122)); + } else if (string[i].match(/^[A-Z]+$/)) { + let value = Number(string.charCodeAt(i) + number); + if (value >= 65 && value <= 90) caesarCiper += String.fromCharCode(string.charCodeAt(i) + number); + else if (value > 90) caesarCiper += String.fromCharCode(64 + (value - 90)); + } else caesarCiper += string[i]; } return caesarCiper; } // KEEP THIS FUNCTION CALL HERE -console.log(CaesarCipher(readline())); \ No newline at end of file +console.log(CaesarCipher(readline())); diff --git a/string manipulation/camelCase.js b/string manipulation/camelCase.js index 5c3e33d..e29c972 100644 --- a/string manipulation/camelCase.js +++ b/string manipulation/camelCase.js @@ -21,37 +21,38 @@ ***************************************************************/ function CamelCase(str) { - let string = str; - let camelCaseString = new String(""); - let camelCaseArray = new Array(); - for (let i = 0; i < string.length; i++) if (!(string[i].toLowerCase() >= "a" && string[i].toLowerCase() <= "z")) camelCaseArray.push(i); - if (camelCaseArray.length === 0) return string; - let position = 0; - let toggleFlag = 0; - for (let i = 0; i < camelCaseArray.length; i++) { + let string = str; + let camelCaseString = new String(""); + let camelCaseArray = new Array(); + for (let i = 0; i < string.length; i++) + if (!(string[i].toLowerCase() >= "a" && string[i].toLowerCase() <= "z")) camelCaseArray.push(i); + if (camelCaseArray.length === 0) return string; + let position = 0; + let toggleFlag = 0; + for (let i = 0; i < camelCaseArray.length; i++) { + toggleFlag = 0; + for (let j = position; j < string.length; j++) { + if (j == camelCaseArray[i]) { + position++; + break; + } + if (i == 0) camelCaseString += string[j].toLowerCase(); + else { + if (toggleFlag == 0) camelCaseString += string[j].toUpperCase(); + else camelCaseString += string[j].toLowerCase(); + } + position++; + toggleFlag++; + } + } toggleFlag = 0; - for (let j = position; j < string.length; j++) { - if (j == camelCaseArray[i]) { + for (let k = position; k < string.length; k++) { + if (toggleFlag == 0) camelCaseString += string[k].toUpperCase(); + else camelCaseString += string[k].toLowerCase(); position++; - break; - } - if (i == 0) camelCaseString += string[j].toLowerCase(); - else { - if (toggleFlag == 0) camelCaseString += string[j].toUpperCase(); - else camelCaseString += string[j].toLowerCase(); - } - position++; - toggleFlag++; + toggleFlag++; } - } - toggleFlag = 0; - for (let k = position; k < string.length; k++) { - if (toggleFlag == 0) camelCaseString += string[k].toUpperCase(); - else camelCaseString += string[k].toLowerCase(); - position++; - toggleFlag++; - } - return camelCaseString; + return camelCaseString; } // KEEP THIS FUNCTION CALL HERE diff --git a/string manipulation/codeland_username_validation.js b/string manipulation/codeland_username_validation.js index d374724..e27373c 100644 --- a/string manipulation/codeland_username_validation.js +++ b/string manipulation/codeland_username_validation.js @@ -21,13 +21,14 @@ * * ***************************************************************/ -function CodelandUsernameValidation(string) { - if(string.length<4 || string.length>25) return false; - if(!string[0].match(/[a-zA-Z]/g)) return false; - if(string[string.length-1] === '_') return false; - for(let i=0; i 25) return false; + if (!string[0].match(/[a-zA-Z]/g)) return false; + if (string[string.length - 1] === "_") return false; + for (let i = 0; i < string.length; i++) + if (!(string[i].match(/[a-zA-Z]/g) || string[i].match(/[1-9]/g) | (string[i] === "_"))) return false; + return true; } -// KEEP THIS FUNCTION CALL HERE -console.log(CodelandUsernameValidation(readline())); \ No newline at end of file +// KEEP THIS FUNCTION CALL HERE +console.log(CodelandUsernameValidation(readline())); diff --git a/string manipulation/command_line.js b/string manipulation/command_line.js index 306be5e..c3a8085 100644 --- a/string manipulation/command_line.js +++ b/string manipulation/command_line.js @@ -31,33 +31,32 @@ * * ***************************************************************/ -function CommandLine( string ) { - let splitStirngAtEqualSign = string.split("="); - let argumentsArray = new Array(); - let argumentsString = new String(""); +function CommandLine(string) { + let splitStirngAtEqualSign = string.split("="); + let argumentsArray = new Array(); + let argumentsString = new String(""); - for( let i=0; i= 'a' && str[i].toLowerCase() <= 'z' ) { - count++; - } - } - return count; + +function ConsonantCount(str) { + let count = 0; + let vowelRegex = /^[aeiouAEIOU]$/; + for (let i = 0; i < str.length; i++) { + if (!str[i].match(vowelRegex) && str[i].toLowerCase() >= "a" && str[i].toLowerCase() <= "z") { + count++; + } + } + return count; } // keep this function call here diff --git a/string manipulation/correct_path.js b/string manipulation/correct_path.js index 14db30c..bb3d882 100644 --- a/string manipulation/correct_path.js +++ b/string manipulation/correct_path.js @@ -37,98 +37,98 @@ let quotientMarkCount = 0; //LOOP THROUGH STRING & COUNT QUOTIENT MARKS for (let i = 0; i < string.length; i++) { - if (string[i] === "?") { - quotientMarkCount++; - } + if (string[i] === "?") { + quotientMarkCount++; + } } //IF QUOTIENTMARKCOUNT IS 1, THEN INIALIZE TO POSSIBLEMOVESARRAY if (quotientMarkCount === 1) { - combinationArray = possibleMovesArray; + combinationArray = possibleMovesArray; } //DYNAMIC FUNCTION WHICH MAKES MOVES ARRAY COMBINATION & RETURN ARRAY RECURSIVELY function makeMovesCombination(movesCount, movesArray) { - let combinationArrayCopy = []; + let combinationArrayCopy = []; - for (let i = 0; i < possibleMovesArray.length; i++) { - for (let j = 0; j < movesArray.length; j++) { - combinationArrayCopy.push(possibleMovesArray[i] + "" + movesArray[j]); + for (let i = 0; i < possibleMovesArray.length; i++) { + for (let j = 0; j < movesArray.length; j++) { + combinationArrayCopy.push(possibleMovesArray[i] + "" + movesArray[j]); + } } - } - return combinationArrayCopy; + return combinationArrayCopy; } //LOOP THROUGH QUOTIENTMARK & CONSTRUCT MOVES ARRAY RECURSIVELY for (let i = 2; i <= quotientMarkCount; i++) { - if (combinationArray.length === 0) { - combinationArray = makeMovesCombination(i, possibleMovesArray); - } else { - combinationArray = makeMovesCombination(i, combinationArray); - } + if (combinationArray.length === 0) { + combinationArray = makeMovesCombination(i, possibleMovesArray); + } else { + combinationArray = makeMovesCombination(i, combinationArray); + } } //LOOP THROUGH COMBINATION ARRAY & CONSTRUCT MOVES STRING & TRY EACH COMBINATION for (let i = 0; i < combinationArray.length; i++) { - let temp = 0; - let combinationString = new String(""); - - for (let j = 0; j < string.length; j++) { - if (string[j] === "?") { - combinationString += combinationArray[i][temp]; - temp++; - } else { - combinationString += string[j]; + let temp = 0; + let combinationString = new String(""); + + for (let j = 0; j < string.length; j++) { + if (string[j] === "?") { + combinationString += combinationArray[i][temp]; + temp++; + } else { + combinationString += string[j]; + } } - } - let isPathGood = makeArrayMovement(combinationString); - if (isPathGood === true) { - console.log(combinationString); - break; - } + let isPathGood = makeArrayMovement(combinationString); + if (isPathGood === true) { + console.log(combinationString); + break; + } } //CHECK STRING COMBINATION function makeArrayMovement(combinationString) { - let array = [ - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - [0, 0, 0, 0, 0], - ]; - let row = 0; - let column = 0; - - for (let i = 0; i < combinationString.length; i++) { - if (combinationString[i] === "r") { - column++; - } else if (combinationString[i] === "d") { - row++; - } else if (combinationString[i] === "l") { - column--; - } else if (combinationString[i] === "u") { - row--; + let array = [ + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0], + ]; + let row = 0; + let column = 0; + + for (let i = 0; i < combinationString.length; i++) { + if (combinationString[i] === "r") { + column++; + } else if (combinationString[i] === "d") { + row++; + } else if (combinationString[i] === "l") { + column--; + } else if (combinationString[i] === "u") { + row--; + } + + //IF PATH GOES OUT OF GRID + if (row < 0 || column < 0 || row > 4 || column > 4) { + break; + } + + //IF THE PATH IS ALREADY TRAVERSED, THEN RETURN FALSE + if (array[row][column] === 1) { + return false; + } + + array[row][column] = 1; } - //IF PATH GOES OUT OF GRID - if (row < 0 || column < 0 || row > 4 || column > 4) { - break; - } - - //IF THE PATH IS ALREADY TRAVERSED, THEN RETURN FALSE - if (array[row][column] === 1) { - return false; + if (row === 4 && column === 4) { + return true; + } else { + return false; } - - array[row][column] = 1; - } - - if (row === 4 && column === 4) { - return true; - } else { - return false; - } } diff --git a/string manipulation/counting_minutes.js b/string manipulation/counting_minutes.js index ed9e081..95d4384 100644 --- a/string manipulation/counting_minutes.js +++ b/string manipulation/counting_minutes.js @@ -7,7 +7,7 @@ * (each properly formatted with a colon and am or pm) * * separated by a hyphen and return the total number of minutes * * between the two times. The time will be in a 12 hour clock * - * format. * + * format. * * For example: if str is 9:00am-10:00am then the * * output should be 60. If str is 1:00pm-11:00am the output * * should be 1320. * @@ -34,23 +34,22 @@ function CountingMinutes(dateString) { let minutes2 = ""; let ampm2 = ""; for (let i = 0; i < splitDateStringAtHiphen.length; i++) { - let splitDateStringAtColon = splitDateStringAtHiphen[i].split(":"); - for (let j = 0; j < splitDateStringAtColon.length; j++) { - if (j == 0 && i == 0) hours1 = Number(splitDateStringAtColon[j]); - else if (j == 0 && i == 1) hours2 = Number(splitDateStringAtColon[j]); - else if (j == 1 && i == 0) { - for (let k = 0; k < splitDateStringAtColon[j].length; k++) { - if (k < 2) minutes1 += splitDateStringAtColon[j][k]; - else ampm1 += splitDateStringAtColon[j][k]; - } - } - else if (j == 1 && i == 1) { - for (let k = 0; k < splitDateStringAtColon[j].length; k++) { - if (k < 2) minutes2 += splitDateStringAtColon[j][k]; - else ampm2 += splitDateStringAtColon[j][k]; - } + let splitDateStringAtColon = splitDateStringAtHiphen[i].split(":"); + for (let j = 0; j < splitDateStringAtColon.length; j++) { + if (j == 0 && i == 0) hours1 = Number(splitDateStringAtColon[j]); + else if (j == 0 && i == 1) hours2 = Number(splitDateStringAtColon[j]); + else if (j == 1 && i == 0) { + for (let k = 0; k < splitDateStringAtColon[j].length; k++) { + if (k < 2) minutes1 += splitDateStringAtColon[j][k]; + else ampm1 += splitDateStringAtColon[j][k]; + } + } else if (j == 1 && i == 1) { + for (let k = 0; k < splitDateStringAtColon[j].length; k++) { + if (k < 2) minutes2 += splitDateStringAtColon[j][k]; + else ampm2 += splitDateStringAtColon[j][k]; + } + } } - } } minutes1 = Number(minutes1); minutes2 = Number(minutes2); @@ -61,17 +60,16 @@ function CountingMinutes(dateString) { let minutesOneConverted = hours1 * 60 + minutes1; let minutesTwoConverted = hours2 * 60 + minutes2; if (minutesOneConverted > minutesTwoConverted) { - hours2 += 24; - let timeOne = hours2 * 60 + minutes2; - let timeTwo = hours1 * 60 + minutes1; - return timeOne - timeTwo; + hours2 += 24; + let timeOne = hours2 * 60 + minutes2; + let timeTwo = hours1 * 60 + minutes1; + return timeOne - timeTwo; } else { - let timeOne = hours2 * 60 + minutes2; - let timeTwo = hours1 * 60 + minutes1; - return timeOne - timeTwo; + let timeOne = hours2 * 60 + minutes2; + let timeTwo = hours1 * 60 + minutes1; + return timeOne - timeTwo; } - } - - // KEEP THIS FUNCTION CALL HERE - console.log(CountingMinutes(readline())); - \ No newline at end of file +} + +// KEEP THIS FUNCTION CALL HERE +console.log(CountingMinutes(readline())); diff --git a/string manipulation/counting_minutes_one.js b/string manipulation/counting_minutes_one.js index b549ec9..fc00de7 100644 --- a/string manipulation/counting_minutes_one.js +++ b/string manipulation/counting_minutes_one.js @@ -7,7 +7,7 @@ * (each properly formatted with a colon and am or pm) * * separated by a hyphen and return the total number of minutes * * between the two times. The time will be in a 12 hour clock * - * format. * + * format. * * For example: if str is 9:00am-10:00am then the * * output should be 60. If str is 1:00pm-11:00am the output * * should be 1320. * @@ -26,50 +26,49 @@ ***************************************************************/ function CountingMinutesI(dateString) { - let splitDateStringAtHiphen = dateString.split("-"); - let hours1; - let minutes1 = ""; - let ampm1 = ""; - let hours2; - let minutes2 = ""; - let ampm2 = ""; - for (let i = 0; i < splitDateStringAtHiphen.length; i++) { - let splitDateStringAtColon = splitDateStringAtHiphen[i].split(":"); - for (let j = 0; j < splitDateStringAtColon.length; j++) { - if (j == 0 && i == 0) hours1 = Number(splitDateStringAtColon[j]); - else if (j == 0 && i == 1) hours2 = Number(splitDateStringAtColon[j]); - else if (j == 1 && i == 0) { - for (let k = 0; k < splitDateStringAtColon[j].length; k++) { - if (k < 2) minutes1 += splitDateStringAtColon[j][k]; - else ampm1 += splitDateStringAtColon[j][k]; + let splitDateStringAtHiphen = dateString.split("-"); + let hours1; + let minutes1 = ""; + let ampm1 = ""; + let hours2; + let minutes2 = ""; + let ampm2 = ""; + for (let i = 0; i < splitDateStringAtHiphen.length; i++) { + let splitDateStringAtColon = splitDateStringAtHiphen[i].split(":"); + for (let j = 0; j < splitDateStringAtColon.length; j++) { + if (j == 0 && i == 0) hours1 = Number(splitDateStringAtColon[j]); + else if (j == 0 && i == 1) hours2 = Number(splitDateStringAtColon[j]); + else if (j == 1 && i == 0) { + for (let k = 0; k < splitDateStringAtColon[j].length; k++) { + if (k < 2) minutes1 += splitDateStringAtColon[j][k]; + else ampm1 += splitDateStringAtColon[j][k]; + } + } else if (j == 1 && i == 1) { + for (let k = 0; k < splitDateStringAtColon[j].length; k++) { + if (k < 2) minutes2 += splitDateStringAtColon[j][k]; + else ampm2 += splitDateStringAtColon[j][k]; + } + } } - } - else if (j == 1 && i == 1) { - for (let k = 0; k < splitDateStringAtColon[j].length; k++) { - if (k < 2) minutes2 += splitDateStringAtColon[j][k]; - else ampm2 += splitDateStringAtColon[j][k]; - } - } } - } - minutes1 = Number(minutes1); - minutes2 = Number(minutes2); - if (ampm1 === "pm" && hours1 <= 11) hours1 += 12; - if (ampm2 === "pm" && hours2 <= 11) hours2 += 12; - if (ampm1 === "am" && hours1 == 12) hours1 = 0; - if (ampm2 === "am" && hours2 == 12) hours2 = 0; - let minutesOneConverted = hours1 * 60 + minutes1; - let minutesTwoConverted = hours2 * 60 + minutes2; - if (minutesOneConverted > minutesTwoConverted) { - hours2 += 24; - let timeOne = hours2 * 60 + minutes2; - let timeTwo = hours1 * 60 + minutes1; - return timeOne - timeTwo; - } else { - let timeOne = hours2 * 60 + minutes2; - let timeTwo = hours1 * 60 + minutes1; - return timeOne - timeTwo; - } + minutes1 = Number(minutes1); + minutes2 = Number(minutes2); + if (ampm1 === "pm" && hours1 <= 11) hours1 += 12; + if (ampm2 === "pm" && hours2 <= 11) hours2 += 12; + if (ampm1 === "am" && hours1 == 12) hours1 = 0; + if (ampm2 === "am" && hours2 == 12) hours2 = 0; + let minutesOneConverted = hours1 * 60 + minutes1; + let minutesTwoConverted = hours2 * 60 + minutes2; + if (minutesOneConverted > minutesTwoConverted) { + hours2 += 24; + let timeOne = hours2 * 60 + minutes2; + let timeTwo = hours1 * 60 + minutes1; + return timeOne - timeTwo; + } else { + let timeOne = hours2 * 60 + minutes2; + let timeTwo = hours1 * 60 + minutes1; + return timeOne - timeTwo; + } } // KEEP THIS FUNCTION CALL HERE diff --git a/string manipulation/dash_insert.js b/string manipulation/dash_insert.js index ecd83cd..b6e8ef8 100644 --- a/string manipulation/dash_insert.js +++ b/string manipulation/dash_insert.js @@ -15,24 +15,19 @@ * Output 2: 567-30 * ***************************************************************/ -function DashInsert( str ) { - //VARIABLE DECLARATION - let fs = ""; - //LOOP THROUGH STRING - for ( let i = 0; i < str.length; i++ ) { - if ( - i != str.length - 1 && - Number(str[i]) % 2 != 0 && - Number(str[i + 1]) % 2 != 0 - ) { - fs += str[i] + "-"; - } - else { - fs += str[i]; - } +function DashInsert(str) { + //VARIABLE DECLARATION + let fs = ""; + //LOOP THROUGH STRING + for (let i = 0; i < str.length; i++) { + if (i != str.length - 1 && Number(str[i]) % 2 != 0 && Number(str[i + 1]) % 2 != 0) { + fs += str[i] + "-"; + } else { + fs += str[i]; } - return fs; + } + return fs; } // KEEP THIS FUNCTION CALL HERE -console.log( DashInsert( readline() ) ); +console.log(DashInsert(readline())); diff --git a/string manipulation/dash_insert_two.js b/string manipulation/dash_insert_two.js index 1f36317..348fc13 100644 --- a/string manipulation/dash_insert_two.js +++ b/string manipulation/dash_insert_two.js @@ -25,16 +25,27 @@ function DashInsertII(number) { let string = String(number); let dashInsertTwo = new String(""); - for(let i=0; i0 && Number(string[i+1])>0) dashInsertTwo += string[i] + "-"; - else if(Number(string[i])%2===0 && Number(string[i+1])%2===0 && Number(string[i])>0 && Number(string[i+1])>0) dashInsertTwo += string[i] + "*"; + for (let i = 0; i < string.length; i++) { + if (i !== string.length - 1) { + if ( + Number(string[i]) % 2 !== 0 && + Number(string[i + 1]) % 2 !== 0 && + Number(string[i]) > 0 && + Number(string[i + 1]) > 0 + ) + dashInsertTwo += string[i] + "-"; + else if ( + Number(string[i]) % 2 === 0 && + Number(string[i + 1]) % 2 === 0 && + Number(string[i]) > 0 && + Number(string[i + 1]) > 0 + ) + dashInsertTwo += string[i] + "*"; else dashInsertTwo += string[i]; - } - else if(i===string.length-1) dashInsertTwo += string[i]; + } else if (i === string.length - 1) dashInsertTwo += string[i]; } return dashInsertTwo; } - + // KEEP THIS FUNCTION CALL HERE -console.log(DashInsertII(readline())); \ No newline at end of file +console.log(DashInsertII(readline())); diff --git a/string manipulation/different_cases.js b/string manipulation/different_cases.js index 0c3ef98..4a242eb 100644 --- a/string manipulation/different_cases.js +++ b/string manipulation/different_cases.js @@ -26,11 +26,13 @@ function DifferentCases(string) { let punctuationRegex = /\w+|\s+|[^\s\w]+/g; - let array = string.match(punctuationRegex); + let array = string.match(punctuationRegex); let differentCases = new String(""); - for(let i=0; i= 10 ? true : false; -} \ No newline at end of file +function DistinctCharacters(str) { + return Array.from(new Set(str.split(""))).length >= 10 ? true : false; +} diff --git a/string manipulation/ex_oh.js b/string manipulation/ex_oh.js index 16dd4f0..f10dd75 100644 --- a/string manipulation/ex_oh.js +++ b/string manipulation/ex_oh.js @@ -23,17 +23,17 @@ ***************************************************************/ function ExOh(string) { - //VARIABLE DECLARATION - let x_count = 0; - let o_count = 0; - //LOOP THROUGH STRING - for (let i = 0; i < string.length; i++) { - if (string[i].toLowerCase() === "x") x_count++; - else if (string[i].toLowerCase() === "o") o_count++; - } - //CONDITIONAL CHECKING - if (x_count === o_count) return "true"; - else return "false"; + //VARIABLE DECLARATION + let x_count = 0; + let o_count = 0; + //LOOP THROUGH STRING + for (let i = 0; i < string.length; i++) { + if (string[i].toLowerCase() === "x") x_count++; + else if (string[i].toLowerCase() === "o") o_count++; + } + //CONDITIONAL CHECKING + if (x_count === o_count) return "true"; + else return "false"; } // KEEP THIS FUNCTION CALL HERE diff --git a/string manipulation/first_reverse.js b/string manipulation/first_reverse.js index c75959b..d717716 100644 --- a/string manipulation/first_reverse.js +++ b/string manipulation/first_reverse.js @@ -18,7 +18,7 @@ * Output 2: edoC evoL I * * * * Solution Efficiency * - * This user scored higher than 29.4% of users who solved this * + * This user scored higher than 29.4% of users who solved this * * challenge. * * * ***************************************************************/ @@ -26,6 +26,6 @@ function FirstReverse(string) { return string.split("").reverse().join(""); } - + // KEEP THIS FUNCTION CALL HERE -console.log(FirstReverse(readline())); \ No newline at end of file +console.log(FirstReverse(readline())); diff --git a/string manipulation/fizzbuzz.js b/string manipulation/fizzbuzz.js index 5e8d3fa..fc797eb 100644 --- a/string manipulation/fizzbuzz.js +++ b/string manipulation/fizzbuzz.js @@ -25,21 +25,21 @@ * Output 2: "1 2 Fizz 4 Buzz Fizz 7 8" * * * * Solution Efficiency * - * This user scored higher than 62.1% of users who solved this * + * This user scored higher than 62.1% of users who solved this * * challenge. * * * ***************************************************************/ -function FizzBuzz(num) { +function FizzBuzz(num) { let string = new String(""); - for(let i=1; i<=num; i++){ - if(i%3===0 && i%5===0) string += "FizzBuzz "; - else if(i%3===0) string += "Fizz "; - else if(i%5===0) string += "Buzz "; + for (let i = 1; i <= num; i++) { + if (i % 3 === 0 && i % 5 === 0) string += "FizzBuzz "; + else if (i % 3 === 0) string += "Fizz "; + else if (i % 5 === 0) string += "Buzz "; else string += i + " "; } return string.trim(); } - + // KEEP THIS FUNCTION CALL HERE -console.log(FizzBuzz(readline())); \ No newline at end of file +console.log(FizzBuzz(readline())); diff --git a/string manipulation/formatted_division.js b/string manipulation/formatted_division.js index 4d1bec4..59c92f1 100644 --- a/string manipulation/formatted_division.js +++ b/string manipulation/formatted_division.js @@ -5,7 +5,7 @@ * Have the function FormattedDivision(num1,num2) take both * * parameters being passed, divide num1 by num2, and return the * * result as a string with properly formatted commas and 4 * - * significant digits after the decimal place. * + * significant digits after the decimal place. * * * * For example: if num1 is 123456789 and num2 is 10000 * * the output should be "12,345.6789". * @@ -20,15 +20,15 @@ * Output 2: 1.0000 * * * * Solution Efficiency * - * This user scored higher than 53.8% of users who solved this * + * This user scored higher than 53.8% of users who solved this * * challenge. * * * ***************************************************************/ -function FormattedDivision(num1,num2) { - let array = ((num1/num2).toFixed(4)).split("."); +function FormattedDivision(num1, num2) { + let array = (num1 / num2).toFixed(4).split("."); return new String(new Intl.NumberFormat().format(array[0]) + "." + array[1]).toString(); } - + // KEEP THIS FUNCTION CALL HERE -console.log(FormattedDivision(readline())); \ No newline at end of file +console.log(FormattedDivision(readline())); diff --git a/string manipulation/formatted_numbers.js b/string manipulation/formatted_numbers.js index e334875..b8e4b8f 100644 --- a/string manipulation/formatted_numbers.js +++ b/string manipulation/formatted_numbers.js @@ -21,20 +21,21 @@ * * ***************************************************************/ -function FormattedNumber(strArr) { - let noOfDecimals = strArr[0].split("."); - if(noOfDecimals.length > 2) return false; - let unformattedNumber = new String(""); - for(let i=0; i1?("."+noOfDecimals[1]) : ""); - let zeroPadding = 0; - for(let i=0; i 1) for(let i=0; i 2) return false; + let unformattedNumber = new String(""); + for (let i = 0; i < noOfDecimals[0].length; i++) if (strArr[0][i] !== ",") unformattedNumber += strArr[0][i]; + unformattedNumber = + new Intl.NumberFormat().format(unformattedNumber) + (noOfDecimals.length > 1 ? "." + noOfDecimals[1] : ""); + let zeroPadding = 0; + for (let i = 0; i < noOfDecimals[0].length; i++) { + if (noOfDecimals[0][i] === "0") zeroPadding++; + else break; + } + if (noOfDecimals[0].length > 1) for (let i = 0; i < zeroPadding; i++) unformattedNumber = "0" + unformattedNumber; + return strArr[0] === unformattedNumber ? true : false; } // KEEP THIS FUNCTION CALL HERE -console.log(FormattedNumber(readline())); \ No newline at end of file +console.log(FormattedNumber(readline())); diff --git a/string manipulation/html_elements.js b/string manipulation/html_elements.js index 86d07b8..451d498 100644 --- a/string manipulation/html_elements.js +++ b/string manipulation/html_elements.js @@ -30,7 +30,7 @@ * Output 2: i * * * * Solution Efficiency * - * This user scored higher than 84.5% of users who solved this * + * This user scored higher than 84.5% of users who solved this * * challenge. * * * ****************************************************************/ @@ -42,17 +42,20 @@ function HTMLElements(string) { let closingTags = new Array(); let temp1 = new String(""); let temp2 = new String(""); - for(let i=0; i') temp1 += openingTags[i][j]; - for(let m=0; m") temp1 += openingTags[i][j]; + for (let m = 0; m < closingTags.length; m++) { temp2 = new String(""); - for(let n=0; n') temp2 += closingTags[m][n]; - if(temp1===temp2) { + for (let n = 0; n < closingTags[m].length; n++) + if (closingTags[m][n] !== "<" && closingTags[m][n] !== "/" && closingTags[m][n] !== ">") + temp2 += closingTags[m][n]; + if (temp1 === temp2) { openingTags.splice(i, 1); i--; closingTags.splice(m, 1); @@ -60,8 +63,10 @@ function HTMLElements(string) { } } } - return (openingTags.length === 0 ) ? true : openingTags[openingTags.length-1].slice(1).slice(0, openingTags[0].length-2); - } - - // KEEP THIS FUNCTION CALL HERE - console.log(HTMLElements(readline())); \ No newline at end of file + return openingTags.length === 0 + ? true + : openingTags[openingTags.length - 1].slice(1).slice(0, openingTags[0].length - 2); +} + +// KEEP THIS FUNCTION CALL HERE +console.log(HTMLElements(readline())); diff --git a/string manipulation/letter_capitalize.js b/string manipulation/letter_capitalize.js index 87c813e..4af8650 100644 --- a/string manipulation/letter_capitalize.js +++ b/string manipulation/letter_capitalize.js @@ -14,7 +14,7 @@ * Output 2: I Ran There * * * * Solution Efficiency * - * The user scored higher than 50.6% of users who solved this * + * The user scored higher than 50.6% of users who solved this * * challenge. * * * ***************************************************************/ @@ -22,9 +22,11 @@ function LetterCapitalize(string) { let splitStringAtSpace = string.split(" "); let letterCapitalizedString = new String(""); - for(let i=0; i 1) { + if (count > 1) { temp2d.push(array[i][j]); temp2d.push(count); } @@ -41,15 +41,15 @@ function LetterCountI(str) { } let maxElement = -1; let maxIndex = -1; - for(let i=0; i maxElement) { - maxElement = mainArray[i].length; - maxIndex = i; + for (let i = 0; i < mainArray.length; i++) { + if (mainArray[i].length === 0) continue; + if (mainArray[i].length > maxElement) { + maxElement = mainArray[i].length; + maxIndex = i; } } - return (maxIndex >= 0) ? array[maxIndex] : maxIndex; + return maxIndex >= 0 ? array[maxIndex] : maxIndex; } //KEEP THIS FUNCTION CALL HERE -console.log(LetterCountI(readline())); \ No newline at end of file +console.log(LetterCountI(readline())); diff --git a/string manipulation/longest_word.js b/string manipulation/longest_word.js index d795a6f..a6be5f5 100644 --- a/string manipulation/longest_word.js +++ b/string manipulation/longest_word.js @@ -16,7 +16,7 @@ * Output 2: love * * * * Solution Efficiency * - * This user scored higher than 63.3% of users who solved this * + * This user scored higher than 63.3% of users who solved this * * challenge. * ***************************************************************/ @@ -24,9 +24,12 @@ function LongestWord(sentence) { let splitStringAtSpace = sentence.split(" "); let punctuationString = /[.,\/#!$%\^&\*;:{}=\-_`~()]/g; let maxLength = 0; - for(let i=0; i maxLength) maxLength = splitStringAtSpace[i].replace(punctuationString, "").length; - for(let i=0; i maxLength) + maxLength = splitStringAtSpace[i].replace(punctuationString, "").length; + for (let i = 0; i < splitStringAtSpace.length; i++) + if (maxLength === splitStringAtSpace[i].length) return splitStringAtSpace[i]; } // KEEP THIS FUNCTION CALL HERE -console.log(LongestWord(readline())); \ No newline at end of file +console.log(LongestWord(readline())); diff --git a/string manipulation/minimum_window_substring.js b/string manipulation/minimum_window_substring.js index 8b44a50..ee310c3 100644 --- a/string manipulation/minimum_window_substring.js +++ b/string manipulation/minimum_window_substring.js @@ -31,14 +31,13 @@ * * ***************************************************************/ - -function MinWindowSubstring (stringArray) { +function MinWindowSubstring(stringArray) { let string = stringArray[0]; let target = stringArray[1]; let targetDictionary = {}; let stringDictionary = {}; - for(let i=0; i rightPointer || rightPointer > string.length) break; - let flag = checkObjectKeysArray(Object.keys(targetDictionary), Object.keys(stringDictionary), targetDictionary, stringDictionary); - if(flag === true) { - if((rightPointer - leftPointer + 1) <= subStringLength){ + while (true) { + if (leftPointer > rightPointer || rightPointer > string.length) break; + let flag = checkObjectKeysArray( + Object.keys(targetDictionary), + Object.keys(stringDictionary), + targetDictionary, + stringDictionary + ); + if (flag === true) { + if (rightPointer - leftPointer + 1 <= subStringLength) { subStringLength = rightPointer - leftPointer + 1; lp = leftPointer; rp = rightPointer; } - if(string[leftPointer] in stringDictionary) stringDictionary[string[leftPointer]] -= 1; + if (string[leftPointer] in stringDictionary) stringDictionary[string[leftPointer]] -= 1; leftPointer++; - } - else { - if(string[rightPointer] in stringDictionary) stringDictionary[string[rightPointer]] += 1; + } else { + if (string[rightPointer] in stringDictionary) stringDictionary[string[rightPointer]] += 1; else stringDictionary[string[rightPointer]] = 1; - rightPointer++; + rightPointer++; } } return string.substring(lp, rp); -}; -function checkObjectKeysArray(array1, array2, targetDictionary, stringDictionary){ - if(array2.length===0 || array1.length > array2.length) return false; +} +function checkObjectKeysArray(array1, array2, targetDictionary, stringDictionary) { + if (array2.length === 0 || array1.length > array2.length) return false; let temp = false; - for(let i=0; i>> 0).toString(2); + let count = 0; - let number = +strArr[0]; - let receivedBinary = strArr[1]; - let actualBinary = ( number >>> 0 ).toString(2); - let count = 0; - - for( let i=0; i= 0; i--) reversedString += originalString[i]; - if (originalString === reversedString) return true; - else return false; + let originalString = str.replace(/\s/g, ""); + let reversedString = ""; + for (let i = originalString.length - 1; i >= 0; i--) reversedString += originalString[i]; + if (originalString === reversedString) return true; + else return false; } // KEEP THIS FUNCTION CALL HERE diff --git a/string manipulation/palindrome_two.js b/string manipulation/palindrome_two.js index 41e3dab..ef9edf6 100644 --- a/string manipulation/palindrome_two.js +++ b/string manipulation/palindrome_two.js @@ -2,7 +2,7 @@ * CODERBYTE PALINDROME TWO CHALLENGE * * * * Problem Statement * - * Have the function PalindromeTwo(str) take the str parameter * + * Have the function PalindromeTwo(str) take the str parameter * * being passed and return the string true if the parameter is * * a palindrome, (the string is the same forward as it is * * backward) otherwise return the string false. The parameter * @@ -29,11 +29,13 @@ function PalindromeTwo(string) { let array = string.match(punctuationRegex); let originalStringWithoutPunctuation = new String(""); let reversedString = new String(""); - for(let i=0; i=0; i--) reversedString += originalStringWithoutPunctuation[i]; - if(originalStringWithoutPunctuation.toLowerCase() === reversedString.toLowerCase()) return true; - else return false; + for (let i = 0; i < array.length; i++) + if (array[i].match(/^[a-zA-Z]+$/)) originalStringWithoutPunctuation += array[i]; + for (let i = originalStringWithoutPunctuation.length - 1; i >= 0; i--) + reversedString += originalStringWithoutPunctuation[i]; + if (originalStringWithoutPunctuation.toLowerCase() === reversedString.toLowerCase()) return true; + else return false; } - + // KEEP THIS FUNCTION CALL HERE -console.log(PalindromeTwo(readline())); \ No newline at end of file +console.log(PalindromeTwo(readline())); diff --git a/string manipulation/question_marks.js b/string manipulation/question_marks.js index f87fc3d..54b569e 100644 --- a/string manipulation/question_marks.js +++ b/string manipulation/question_marks.js @@ -37,23 +37,22 @@ ***************************************************************/ function QuestionsMarks(string) { - let questionMarkCount = 0; - let temp = 0; - let flag = false; - for(let i=0; i= 31) return false; - return true; +function SimplePassword(str) { + finalString = str.replace(/[^\w\s]|_/g, "").replace(/\s+/g, " "); + if (str.length === finalString.length) return false; + if (str.match(/[a-zA-Z]/g).length === 0 || str.match(/[0-9]/g).length === 0) return false; + str = str.toLowerCase(); + if (str.includes("password")) return false; + if (str.length <= 7 || str.length >= 31) return false; + return true; } - + // KEEP THIS FUNCTION CALL HERE -console.log(SimplePassword(readline())); \ No newline at end of file +console.log(SimplePassword(readline())); diff --git a/string manipulation/simple_symbols.js b/string manipulation/simple_symbols.js index 43370a1..0e49c67 100644 --- a/string manipulation/simple_symbols.js +++ b/string manipulation/simple_symbols.js @@ -24,9 +24,11 @@ ***************************************************************/ function SimpleSymbols(string) { - for(let i=0; i= 'a' && string[i].toLowerCase() <= 'z') if(!(string[i-1] === '+' && string[i+1] === '+')) return false; + for (let i = 0; i < string.length; i++) + if (string[i].toLowerCase() >= "a" && string[i].toLowerCase() <= "z") + if (!(string[i - 1] === "+" && string[i + 1] === "+")) return false; return true; } //KEEP THIS FUNCTION CALL HERE -console.log(SimpleSymbols(readline())); \ No newline at end of file +console.log(SimpleSymbols(readline())); diff --git a/string manipulation/star_rating.js b/string manipulation/star_rating.js index 5b37980..3e6d860 100644 --- a/string manipulation/star_rating.js +++ b/string manipulation/star_rating.js @@ -27,15 +27,15 @@ ***************************************************************/ function StarRating(string) { - let answer = new String(""); - for(let i=0; i= 0.75) answer += "full "; - else if((Number(string)%1) >= 0.25 && (Number(string)%1) < 0.75) answer += "half "; - else answer += "empty "; - for(let i=0; i= 0.75) answer += "full "; + else if (Number(string) % 1 >= 0.25 && Number(string) % 1 < 0.75) answer += "half "; + else answer += "empty "; + for (let i = 0; i < parseInt(5 - parseInt(Number(string))) - 1; i++) answer += "empty "; + return answer.trim(); } - + // KEEP THIS FUNCTION CALL HERE -console.log(StarRating(readline())); \ No newline at end of file +console.log(StarRating(readline())); diff --git a/string manipulation/string_calculate.js b/string manipulation/string_calculate.js index b409b73..b517317 100644 --- a/string manipulation/string_calculate.js +++ b/string manipulation/string_calculate.js @@ -27,30 +27,35 @@ * * ***************************************************************/ -function parse( str ) { - return Function(`'use strict'; return (${str})`)() +function parse(str) { + return Function(`'use strict'; return (${str})`)(); } function StringCalculate(string) { let evaluateExpression = new String(""); let splittedString = string.split(""); let flag = false; - for(let i=0; i= strArr[0].length) zigZagArray = new Array(Number(strArr[0].length)); - else zigZagArray = new Array(Number(strArr[1])); - for(let i=0; i=1 && count=Number(strArr[1]) && count<=(Number(strArr[1])+(Number(strArr[1])-1))) { - if(count == (Number(strArr[1])+(Number(strArr[1])-1))) { - row = 1; - count = row + 1; - } - else { - count++; - row--; - } - } + if (Number(strArr[1]) === 1) return strArr[0]; + let zigZagArray; + if (Number(strArr[1]) >= strArr[0].length) zigZagArray = new Array(Number(strArr[0].length)); + else zigZagArray = new Array(Number(strArr[1])); + for (let i = 0; i < zigZagArray.length; i++) zigZagArray[i] = new Array(Number(strArr[0].length)); + let row = 0, + column = 0, + count = 1; + for (let i = 0; i < strArr[0].length; i++) { + zigZagArray[row][column] = strArr[0][i]; + column++; + if (count >= 1 && count < Number(strArr[1])) { + count++; + row++; + } else if (count >= Number(strArr[1]) && count <= Number(strArr[1]) + (Number(strArr[1]) - 1)) { + if (count == Number(strArr[1]) + (Number(strArr[1]) - 1)) { + row = 1; + count = row + 1; + } else { + count++; + row--; + } } - let zigZagString = new String(""); - for(let i=0; i