Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse string complete #116

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
38 changes: 37 additions & 1 deletion exercises/anagrams/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,42 @@
// anagrams('RAIL! SAFETY!', 'fairy tales') --> True
// anagrams('Hi there', 'Bye there') --> False

function anagrams(stringA, stringB) {}
function anagrams(stringA, stringB) {
return cleanString(stringA) === cleanString(stringB);
}

function cleanString(str) {
return str.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
}

module.exports = anagrams;

// solution 1

// const aCharMap = buildCharMap(stringA);
// const bCharMap = buildCharMap(stringB);

// // checking to see if length matches
// if (Object.keys(aCharMap).length !== Object.keys(bCharMap).length) {
// return false;
// }

// // checking to see if the value of the key are matching
// for (let char in aCharMap) {
// if (aCharMap[char] !== bCharMap[char]) {
// return false;
// }
// }

// return true;
// }

// // helper function that creates hash map
// function buildCharMap(str) {
// const charMap = {};

// for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
// charMap[char] = charMap[char] + 1 || 1;
// }

// return charMap;
17 changes: 16 additions & 1 deletion exercises/capitalize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
// capitalize('a lazy fox') --> 'A Lazy Fox'
// capitalize('look, it is working!') --> 'Look, It Is Working!'

function capitalize(str) {}
function capitalize(str) {
// Make an empty array 'words'
const words = []

// for of loop iterates everything

// Split the input string by spaces to get an array
// and iterate the array
for (let word of str.split(' ')) {
// we are pushing (the 0th index of the word in uppercase + the rest of the word)
words.push(word[0].toUpperCase() + word.slice(1));
}

// return the array 'words' but joined as a string with spaces in between
return words.join(' ');
}

module.exports = capitalize;
49 changes: 48 additions & 1 deletion exercises/chunk/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,53 @@
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]

function chunk(array, size) {}
function chunk(array, size) {
// create empty array to hold chunks called chunked
// for each element in the "unchunked" array
// retrieve the last element in 'chunked'
// if last element does not exists, or if its length is equal to chunk size
// push a new chunk into 'chunked' with the current element
// else add the current element into the chunk

// array that holds different chunked data
// eg. [[1,2],[3,4],[5]]
// const chunked = [];

// for (let element of array) {

// // checking for the last element in chunked
// // chunked = [chunked[0], chunked[1]]
// // chunked = []
// const last = chunked[chunked.length - 1];

// // if last does not exist
// // or the length of the last chunked piece is equal to size
// if (!last || last.length === size) {
// // push a new element into a chunk
// chunked.push([element]);
// } else {
// // push the element into last
// last.push(element);
// }
// }
// return chunked;

// Create empty 'chunked' array
const chunked = [];

// Create 'index' start at 0
// since this variable will change over time
let index = 0;

// while index is less than array.length
while(index < array.length) {
// push a slice of length 'size' from 'array' into 'chunked'
chunked.push(array.slice(index, index + size));
// add 'size' to 'index'
index += size;
}

return chunked
}

module.exports = chunk;
27 changes: 26 additions & 1 deletion exercises/fizzbuzz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@
// 4
// buzz

function fizzBuzz(n) {}
function fizzBuzz(n) {
// for loop n
for(let i = 1; i <= n; i++) {

// if i is divisible by 15 with no leftovers
if(i % 15 === 0) {
// return fizzbuzz
console.log('fizzbuzz');

// if i is divisible by 3 with no leftovers
}else if(i % 3 === 0) {
// return fizz
console.log('fizz');

// if i is divisible by 3 with no leftovers
}else if(i % 5 === 0) {
// return fizz
console.log('buzz');

// if i is not divisible by 3, 5 nor 15
}else{
// return the number
console.log(i);
}
}
}

module.exports = fizzBuzz;
54 changes: 53 additions & 1 deletion exercises/maxchar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,58 @@
// maxChar("abcccccccd") === "c"
// maxChar("apple 1231111") === "1"

function maxChar(str) {}
function maxChar(str) {
// What is the most common character in the string?
// Does string A have the same characters as string B (is it an anagram?)
// Does the given string have any repeated characters in it?

// Counting and verifying -> Use a key value pair(Object, dictionary/hashmap in Python)
const string = "Hello There!";
const chars = {};

for(let char of string) {
// basic if statement to see if key exists
// assign 1 or add 1
if (!chars[char]) {
chars[char] = 1;
} else {
chars[char]++;
}

// add one to the value of chars[char] if it's not null
// if it is null(or) assign 1
chars[char] = chars[char] + 1 || 1;
}

// 1. create an empty object
const dict = {};

// 3a. to keep track of the max number of common usage of char and char itself
let max = 0;
let maxChar = '';

// 2. assign key value pairs to individual i you iterate
for(let i of str) {
dict[i] = dict[i] + 1 || 1;
}

// 3b. iterating through dict to find the char that is used most frequently
// when iterating an object/dict you don't use 'of', you use 'in'
// dict in sounds like dick in
for(let i in dict) {
// checking to see if the value is greater than max(start 0)
if(dict[i] > max) {
// if it is, the value of max is replaced with the current value
max = dict[i];
// as well as the maxChar which is replaced with the current i(key)
maxChar = i;
}
}

return maxChar;



}

module.exports = maxChar;
34 changes: 33 additions & 1 deletion exercises/palindrome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@
// palindrome("abba") === true
// palindrome("abcdefg") === false

function palindrome(str) {}
function palindrome(str) {
// create an empty string called reversed
let reversed = '';

// for each character in the provided string
// take the character and add it to the start of reverse
// JS adds things to the start
for (let character of str) {
reversed = character + reversed;
}

// you could reverse the string with this one-liner
// and continue with the comparison
// return str == rev
const rev = str
// splitting the string one by one into an array
.split('')
// reversing the array
.reverse()
// joining back the array together as a string
.join('');

// return the bool of str == reversed
return str == reversed;


// approach with usage of every() helper
// [0, 10, 14] is every value greater than 5?
// array.every((val) => val > 5);

// check the if first item of the array is equal to the last
// repeat
}

module.exports = palindrome;
19 changes: 18 additions & 1 deletion exercises/reverseint/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
// reverseInt(-15) === -51
// reverseInt(-90) === -9

function reverseInt(n) {}
function reverseInt(n) {

// converting n into string then reversing
// still a string
const reversed = n
.toString()
.split('')
.reverse()
.join('');

// parseInt() convert reversed back into int, '5' -> 5
// it will always be positive tho, '5-' -> 5

// by multiplying the sign of n to parseInt(reversed)
// if n is positive, it will return positive value
// if n is negative, it will return negative value
return parseInt(reversed) * Math.sign(n);
}

module.exports = reverseInt;
33 changes: 32 additions & 1 deletion exercises/reversestring/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,37 @@
// reverse('hello') === 'olleh'
// reverse('Greetings!') === '!sgniteerG'

function reverse(str) {}
function reverse(str) {
// solution 1:

// turn str into an array

// call reverse method on the array

// join the array back into a string
// return the result
// return str.split('').reverse().join('');


// solution 2:

// create an empty string called reversed
// let reversed = '';

// for each character in the provided string
// take the character and add it to the start of reverse
// JS adds things to the start
// for (let character of str) {
// reversed = character + reversed;
// }

// return the variable reversed
// return reversed;


// solution 3:
// using reduce
return str.split('').reduce((rev, char) => char + rev, '');
}

module.exports = reverse;
52 changes: 51 additions & 1 deletion exercises/steps/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,56 @@
// '### '
// '####'

function steps(n) {}
function steps(n, row = 0, stair = '') {
// Recursive Approach

// if (row === n) then we have hit the end of our problem
if (n === row) {
return;
}

// if the 'stair' string has a length === n then we are at the end of a row
if (n === stair.length) {
console.log(stair);
return steps(n, row + 1);
}

// if the length of the stair string is less than or equal to the row number we're working on, we add a '#', otherwise add a space
if (stair.length <= row) {
stair += '#';
} else {
stair += ' ';
}
steps(n, row, stair);
}

module.exports = steps;



// // from 0 to n(iterate through rows)
// for (let row = 0; row < n; row++) {

// // create an empty string, 'stair'
// let stair = '';

// // from 0 to n (iterate through columns)
// for (let column = 0; column < n; column++) {

// // if the current column is equal to or less than the current row
// if (column <= row) {

// // add a "#' to 'stair'
// stair += '#';

// // else
// } else {

// // add a '#' to 'stair'
// stair += ' ';
// }
// }

// // console.log 'stair'
// console.log(stair);
// }
1 change: 1 addition & 0 deletions testing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest steps/test.js --watch