Posts

Code Wars: String incrementer

https://www.codewars.com/kata/54a91a4883a7de5d7800009c/train/javascript Your job is to write a function which increments a string, to create a new string. If the string already ends with a number, the number should be incremented by 1. If the string does not end with a number the number 1 should be appended to the new string. Examples: foo -> foo1 foobar23 -> foobar24 foo0042 -> foo0043 foo9 -> foo10 foo099 -> foo100 Attention: If the number has leading zeros the amount of digits should be considered. My Solution:  function incrementString (strng) {   let regex = /[0-9]/g;   let index = strng.search(regex);   let arr = [];      //split string at where a number first appears   if (index === -1 || strng === "") {     return strng + "1";   }   else arr.push( strng.substring(0, index), strng.substring(index, strng.length) );      //loop through element of ...

FCC: Project Euler: Problem 4: Largest palindrome product

https://learn.freecodecamp.org/coding-interview-prep/project-euler/problem-4-largest-palindrome-product A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two  n -digit numbers. My solution: function largestPalindromeProduct(n) { let largestPalindrome = 0 ; //tracker //get starting and ending numbers of n-digit numbers let lowest = "1" ; while (n > 1 ) { lowest += "0" ; n--; } let largest = parseInt(lowest + "0" ) - 1 ; lowest = parseInt(lowest); //start with largest number since likely to get to largest palindrome first for ( let i = largest; i >= lowest; i--) { for ( let j = largest - 1 ; j >= lowest; j--) { let number = i * j; //check if number is greater before checking if palindrome for efficiency if ( number...

Jquery: this == event.target

function(event) { event.target.hide(); } can replace 'event.target' with 'this' (i.e. what you clicked on)

Code Wars: Delete occurrences of an element if it occurs more than n times (SOLVED 2nd try)

https://www.codewars.com/kata/554ca54ffa7d91b236000023/train/javascript Enough is enough! Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like this sessions, since the motive usually repeats. He isn't fond of seeing the Eiffel tower 40 times. He tells them that he will only sit during the session if they show the same motive at most N times. Luckily, Alice and Bob are able to encode the motive as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order? Task Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which lead...

JQuery: on method (Team Treehouse Notes)

Instead of having 2 separate event listeners for click and keypress, use the on method to combine the two: $('#element').on(' click keypress ', function() { //do stuff when element is clicked OR when a key is pressed });

Code Wars: Find the odd int

https://www.codewars.com/kata/54da5a58ea159efa38000836/train/javascript Given an array, find the int that appears an odd number of times. There will always be only one integer that appears an odd number of times. function findOdd(A) {   let arr = A.sort((a,b) => a - b);   let counter = 1;     if (arr.length === 1) return arr[0];     for (let i = 0; i < arr.length - 1; i++) {         if (arr[i] === arr[i + 1]) {       counter++;     }     else if (arr[i] !== arr[i + 1]) {             if (counter % 2 !== 0) {         return arr[i];       }       // if it turns out to be last number in array with a count of 1 (since for loop doesnt get to it)       else if (i + 1 === arr.length - 1) {         return arr[i + 1];       }       count...

Code Wars: Maximum subarray sum

https://www.codewars.com/kata/54521e9ec8e60bc4de000d6c/train/javascript The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: maxSequence([ -2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4 ]) // should be 6: [4, -1, 2, 1] Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead. Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray. SOLUTION: var maxSequence = function(arr){   let maxSum = arr[0]; //current highest consecutive sum or single element     if (arr.find(item => item > 0) === undefined || arr.length === 0) {     return 0;   }     for (let i = 0; i < arr.length - 1; i++) {     let tracker = 0; //keeps track of consecutive sums. Reset at every 'pointer...