Skip to content

Instantly share code, notes, and snippets.

View JeffOgah's full-sized avatar
:octocat:

JeffOgah

:octocat:
View GitHub Profile
@JeffOgah
JeffOgah / maxSumOfConcatElements.js
Last active June 20, 2022 03:04
Rearrange Array Elements so as to form two number such that their sum is maximum. The number of digits in both the numbers cannot differ by more than 1
/* Challenge: Rearrange Array Elements so as to form two number such that their sum is maximum. The number of digits in both the numbers cannot differ by more than 1. You're not allowed to use any sorting function.
for e.g. [1, 2, 3, 4, 5]
The expected answer would be [531, 42]. Another expected answer can be [542, 31]. In scenarios such as these when there are more than one possible answers, return any one.
*
*******************************************************************
Breakdown of my solution:
@JeffOgah
JeffOgah / getIndicesOfItemWeights.js
Created June 6, 2019 12:10
Function to return an array containing indexes of 2 number that sum up to a given limit otherwise return empty array
function getIndicesOfItemWeights(arr, limit) {
for (let num in arr) {
var difference = (limit - arr[num])
//Check if a sum is possible
if (arr[num] <= limit && arr.includes(difference)) {
//Sort index in descending order
if (arr.indexOf(arr[num]) < arr.indexOf(difference)) {
return [arr.indexOf(difference), arr.indexOf(arr[num])]
}
return [arr.indexOf(arr[num]), arr.indexOf(difference)]
@JeffOgah
JeffOgah / cash-register.js
Created February 23, 2019 07:03
JavaScript Algorithms and Data Structures Projects: Cash Register
/* Desc: Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price),
payment as the second argument (cash), and cash-in-drawer (cid) as the third argument.
The checkCashRegister() function should always return an object with a status key and a change key.
Full description at => https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/
*/
function checkCashRegister(price, cash, cid) {
var change = cash - price; //change due
let totalCash = 0; //variable to hold total cash in register
@JeffOgah
JeffOgah / telephone-number-validator.js
Created February 22, 2019 18:23
JavaScript Algorithms and Data Structures Projects: Telephone Number Validator
//Return true if the passed string looks like a valid US phone number.
//The area code is required. If the country code is provided, confirm that the country code is 1
function telephoneCheck(str) {
//^(1\s?)? - checks if country code is provided. Checks for possible spacing
//(\(\d{3}\)|\d{3})[\s\-]? - checks that area code is 3 numbers possibly enclosed in parenthesis and also matches space or hyphen after
//\d{3}[\s\-]?\d{4}$ //checks 7 digit number possibly broken into 3 - 4 format. Ensures str ends in number
@JeffOgah
JeffOgah / caesars-cipher.js
Created February 22, 2019 12:39
JavaScript Algorithms and Data Structures Projects: Caesars Cipher
//A function which takes a ROT13 encoded string as input and returns a decoded string.
function rot13(str) {
//declare both alphabets halfs in order
const rotFirstHalf = [ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"];
const rotSecondHalf = [ "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ];
//function to check which half elements lies in
//and return equivalent element
@JeffOgah
JeffOgah / roman-numeral-converter.js
Last active April 23, 2022 08:19
JavaScript Algorithms and Data Structures Projects: Roman Numeral Converter
// Convert number to roman numerals
//Create object mapping relevant numbers to roman symbol equivalent
function convertToRoman(num) {
const romanSymbols = {
1000: 'M',
900: 'CM',
500: 'D',
400: 'CD',
100: 'C',
@JeffOgah
JeffOgah / palindrome-checker.js
Created February 21, 2019 16:07
JavaScript Algorithms and Data Structures Projects: Palindrome Checker
/* Return true if the given string is a palindrome. Otherwise, return false.
* A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
*/
function palindrome(str) {
// create new array holding lowercase of str argument
//match all alphanumeric characters (this returns an array of matched characters)
//join array to convert to string
let filtered = str.toLowerCase().match(/[a-z0-9]/gi).join("");
@JeffOgah
JeffOgah / arguments-optional.js
Created February 21, 2019 00:07
JavaScript Intermediate Algorithm Scripting: Arguments Optional
/*Create a function that sums two arguments together. If only one argument is provided,
then return a function that expects one argument and returns the sum.
For example, addTogether(2, 3) should return 5, and addTogether(2) should return a function.
*/
function addTogether() {
let firstNum, secondNum; //numbers to be added
//function to check if argument is a number
function validNumber(num) {
@JeffOgah
JeffOgah / everything-be-true.js
Last active February 20, 2019 03:14
JavaScript Intermediate Algorithm Scripting: Everything Be True
/* Check if the predicate (second argument) is truthy on all elements of a collection (first argument).
In other words, you are given an array collection of objects.
The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false.
In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context.
*/
function truthCheck(collection, pre) {
// Is everyone being true?
for (let obj in collection) {
if (!collection[obj][pre]) {
@JeffOgah
JeffOgah / binary-agents.js
Last active February 23, 2019 07:14
JavaScript Intermediate Algorithm Scripting: Binary Agents
// Return an English translated sentence of the passed binary string. The binary string will be space separated.
function binaryAgent(str) {
//split str separated by space then map each binary string to dec
let arr = str.split(" ").map(item => parseInt(item, 2));
//map each dec to string equivalent and join characters
return arr.map(item => String.fromCharCode(item)).join("");
}