diff --git a/AB Check b/AB Check index fe5ecc4..ca71ba7 100644 --- a/AB Check +++ b/AB Check @@ -19,14 +19,11 @@ * 3) Else return false * * * ***************************************************************************************/ -function ABCheck(str) { - - var match = str.search(/a...b/); - if (match > -1) { - return "true"; +function ABCheck(str) { + for (let i = 0; i < str.length - 4; i++) { + if ((str[i] === 'a' && str[i + 4] === 'b') || (str[i] === 'b' && str[i + 4] === 'a')) { + return true; + } } - else { - return "false"; - } - + return false; } diff --git a/Additive Persistence b/Additive Persistence index b2efa69..3e99494 100644 --- a/Additive Persistence +++ b/Additive Persistence @@ -24,22 +24,11 @@ * 4) Return number of loops as answer * * * ***************************************************************************************/ - -function AdditivePersistence(num) { - - var sum, loop = 0; - var val1 = num.toString(10).split("").map(function(t){return parseInt(t)}); - - while (val1.length > 1) { - sum = 0; - val1.forEach( function(number) { - sum = sum + number; - }); - - val1 = sum.toString(10).split("").map(function(t){return parseInt(t)}); - loop++; - } - - return loop; - +function additivePersistence(num) { + let count = 0; + while (num >= 10) { + num = num.toString().split('').reduce((sum, digit) => sum + parseInt(digit), 0); + count++; + } + return count; } diff --git a/Arith Geo b/Arith Geo index c160c95..b06fbd9 100644 --- a/Arith Geo +++ b/Arith Geo @@ -35,32 +35,24 @@ * * ***************************************************************************************/ -function ArithGeo(arr) { +function ArithGeo(arr) { + if (arr.length < 2) return -1; - var arithFlag = true, geoFlag = true; - var diff = arr[1] - arr[0]; - - for (var i = 2; i < arr.length; i++) { - if ((arr[i] - arr[i-1]) !== diff) { - arithFlag = false; - } - } - if (arithFlag) { - return "Arithmetic"; - } - else { // check for geometric pattern - diff = arr[1] / arr[0]; - for (var i = 2; i < arr.length; i++) { - if ((arr[i] / arr[i-1]) !== diff) { - geoFlag = false; - } - } - if (geoFlag) { - return "Geometric"; + let isArithmetic = true; + let isGeometric = true; + let diff = arr[1] - arr[0]; + let ratio = arr[1] / arr[0]; + + for (let i = 1; i < arr.length; i++) { + if (arr[i] - arr[i - 1] !== diff) { + isArithmetic = false; } - else { - return "-1"; + if (arr[i] / arr[i - 1] !== ratio) { + isGeometric = false; } } - + + if (isArithmetic) return "Arithmetic"; + if (isGeometric) return "Geometric"; + return -1; } diff --git a/Array Addition I b/Array Addition I index 85c0179..89c0fe4 100644 --- a/Array Addition I +++ b/Array Addition I @@ -27,34 +27,26 @@ * * ***************************************************************************************/ -function ArrayAdditionI(arr) { +function ArrayAdditionI(arr) { + const largest = Math.max(...arr); + arr.splice(arr.indexOf(largest), 1); - arr.sort(function(a,b){return a - b}); - var maxNum = arr.pop(); - var tot = 0; - - for (var i = 0; i < arr.length; i++){ - tot += arr[i]; - for (var j = 0; j < arr.length; j++){ - if (i != j) { - tot += arr[j]; - if (tot == maxNum) { - return true; - } + function canSum(target, nums) { + if (target === 0) return true; + if (target < 0) return false; + for (let i = 0; i < nums.length; i++) { + const remaining = nums.slice(); + remaining.splice(i, 1); + if (canSum(target - nums[i], remaining)) { + return true; } } - - for (var k = 0; k < arr.length; k++) { - if (i != k) { - tot -= arr[k]; - if (tot == maxNum) { - return true; - } - } - } - tot = 0; + return false; } - - return false; - + + return canSum(largest, arr); } + +// Test cases with console.log +console.log(ArrayAdditionI([4, 6, 23, 10, 1, 3])); // Expected output: true +console.log(ArrayAdditionI([4, 6, 12, 10, 1, 3])); // Expected output: false diff --git a/Counting Minutes I b/Counting Minutes I index e6474d4..9ad0e04 100644 --- a/Counting Minutes I +++ b/Counting Minutes I @@ -31,36 +31,34 @@ * 3) Else return false * * * ***************************************************************************************/ -function CountingMinutesI(str) { - var time1Obj = {}, time2Obj = {}, timeDiff; - - time1Obj = setTimeObject(str, 0); - time2Obj = setTimeObject(str, 1); - - if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot > time2Obj.tot) { - timeDiff = (((12 - time1Obj.hours + 12) * 60) - (time1Obj.mins)) + ((time2Obj.hours * 60) + time2Obj.mins); - } - else if (time1Obj.ampm == time2Obj.ampm && time1Obj.tot < time2Obj.tot) { - timeDiff = ((time2Obj.hours * 60) + time2Obj.mins) - ((time1Obj.hours * 60) + time1Obj.mins); - } - else if (time1Obj.ampm !== time2Obj.ampm && time1Obj.ampm === "am") { - timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); - } - else { - timeDiff = (((12 - time1Obj.hours) * 60) - time1Obj.mins) + ((time2Obj.hours * 60) + time2Obj.mins); +function CountingMinutesI(str) { + function timeToMinutes(time) { + let [hours, minutesPart] = time.split(':'); + let minutes = parseInt(minutesPart.slice(0, -2)); + let period = minutesPart.slice(-2); + hours = parseInt(hours); + + if (period === 'pm' && hours !== 12) { + hours += 12; + } else if (period === 'am' && hours === 12) { + hours = 0; } - return timeDiff; -} - -function setTimeObject(str, num) { - var arr = str.split("-"); - var tObject = {}; - - tObject.hours = Number(arr[num].slice(0,arr[num].length-2).split(":")[0]); - tObject.mins = Number(arr[num].slice(0,arr[num].length-2).split(":")[1]); - tObject.ampm = arr[num].slice(-2); - tObject.tot = tObject.hours * 100 + tObject.mins; - - return tObject; + return hours * 60 + minutes; + } + + const [start, end] = str.split('-'); + const startMinutes = timeToMinutes(start); + const endMinutes = timeToMinutes(end); + + let difference = endMinutes - startMinutes; + if (difference < 0) { + difference += 1440; // add 24 hours worth of minutes + } + + return difference; } + +// Test cases +console.log(CountingMinutesI("9:00am-10:00am")); // Expected output: 60 +console.log(CountingMinutesI("1:00pm-11:00am")); // Expected output: 1320 diff --git a/Dash Insert b/Dash Insert index c1777d6..74316d3 100644 --- a/Dash Insert +++ b/Dash Insert @@ -20,18 +20,23 @@ * * ***************************************************************************************/ -function DashInsert(str) { +function DashInsert(str) { + let result = ''; - var idx = 0; - while (idx < str.length-1) { - if (Number(str[idx]) % 2 === 1 && Number(str[idx+1]) % 2 === 1) { - str = str.slice(0,idx+1) + "-" + str.slice(idx+1); - idx = idx + 2; - } - else { - idx++; - } + for (let i = 0; i < str.length; i++) { + result += str[i]; + if (i < str.length - 1) { + const currentNum = parseInt(str[i]); + const nextNum = parseInt(str[i + 1]); + if (currentNum % 2 !== 0 && nextNum % 2 !== 0) { + result += '-'; + } + } } - return str; - + + return result; } + +// Test cases +console.log(DashInsert("454793")); // Expected output: "4547-9-3" +console.log(DashInsert("123456")); // Expected output: "123456" diff --git a/Division Stringified b/Division Stringified index 8d81ccb..a7d4f3f 100644 --- a/Division Stringified +++ b/Division Stringified @@ -18,11 +18,14 @@ * 2) Use RegExp to format string as required * * * ***************************************************************************************/ -function DivisionStringified(num1,num2) { - - var tot = Math.round(num1 / num2); - var newNum = tot.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,"); +function DivisionStringified(num1, num2) { + // Perform the division and round the result to the nearest integer + const result = Math.round(num1 / num2); - return newNum; - + // Convert the result to a string and format it with commas + return result.toLocaleString(); } + +// Test cases +console.log(DivisionStringified(123456789, 10000)); // Expected output: "12,346" +console.log(DivisionStringified(1000000, 3)); // Expected output: "333,333" diff --git a/Ex Oh b/Ex Oh index 42bdcee..a3bb734 100644 --- a/Ex Oh +++ b/Ex Oh @@ -24,26 +24,21 @@ * 4) Else return false * * * ***************************************************************************************/ -function ExOh(str) { +function ExOh(str) { + let countX = 0; + let countO = 0; - if (str.length % 2 === 1) { - return false; - } - else { - var tot = 0; - for (var i = 0; i < str.length; i++) { - if (str[i] === "x") { - tot++; - } + for (let char of str) { + if (char === 'x') { + countX++; + } else if (char === 'o') { + countO++; } - - if (tot === (str.length / 2)) { - return true; - } - else { - return false; - } - } - + + return countX === countO; } + +// Test cases +console.log(ExOh("xooxxxxooxo")); // Expected output: false +console.log(ExOh("xoxo")); // Expected output: true diff --git a/First Factorial b/First Factorial index 747283a..0719227 100644 --- a/First Factorial +++ b/First Factorial @@ -22,13 +22,15 @@ * * ***************************************************************************************/ -function FirstFactorial(num) { - - var tot = 1; - for (var i = 2; i <= num; i++) { - tot *= i; +function FirstFactorial(num) { + let factorial = 1; + for (let i = 1; i <= num; i++) { + factorial *= i; } - - return tot; - + return factorial; } + +// Test cases +console.log(FirstFactorial(4)); // Expected output: 24 +console.log(FirstFactorial(5)); // Expected output: 120 +console.log(FirstFactorial(1)); // Expected output: 1 diff --git a/Kick Off b/Kick Off new file mode 100644 index 0000000..a8c7e85 --- /dev/null +++ b/Kick Off @@ -0,0 +1,5 @@ +• Introduce the project charter, outlining the project’s objectives, stakeholders, and key deliverables. +• Establish communication protocols and reporting structures. +• Assign roles and responsibilities clearly to avoid ambiguity. +• Set preliminary timelines and milestones through a collaborative discussion to ensure buy-in. +• Discuss initial risks and mitigation strategies. Additionally, I facilitate team-building activities to foster collaboration and alignment, essential for a cohesive project environment.