Skip to content

Instantly share code, notes, and snippets.

View JoshuaRotimi's full-sized avatar

Joshua Olorunnipa JoshuaRotimi

View GitHub Profile
@JoshuaRotimi
JoshuaRotimi / dailyTemperatures.js
Created June 24, 2024 13:39
Write a function that takes an array of daily temperatures and returns an array where each element is the number of days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
const dailyTemp = (arr) => {
const answer = []
for (let i = 0; i < arr.length; i++) {
const current = arr[i];
const higher = arr.slice(i).findIndex((item) => item > current);
higher >= 0 ? answer.push(higher) : answer.push(0);
}
return answer;
}
@JoshuaRotimi
JoshuaRotimi / flipArray.js
Created January 15, 2024 08:36
Given a 2D array, write a function that flips it vertically or horizontally.
let array = [
[1,2,3],
[4,5,6],
[7,8,9]
]
const flip = (arr, type) => {
let newList = [];
if (type === 'horizontal') {
@JoshuaRotimi
JoshuaRotimi / isBitonic.js
Created December 19, 2023 09:13
Write a function that determines if an array of numbers is a bitonic sequence.
const isBitonic = (arr) => {
let peakNumber = 0;
for (let i = 0; i < arr.length; i++) {
if(arr[i] > peakNumber) {
peakNumber = arr[i];
}
if(arr[i] === peakNumber && arr[i] > arr[i + 1]) {
console.log('extra credit: ', peakNumber);
return true
@JoshuaRotimi
JoshuaRotimi / MajorityNumber.js
Created December 12, 2023 08:33
Given an array of integers, return the majority element.
// Given an array of integers, return the majority element.
// If there is no majority element, return if the array is majority even or odd numbers,
// and if there is none, say so.
function findMajorityNumber(arr) {
const counts = new Map();
let evenCount = 0;
let oddCount = 0;
@JoshuaRotimi
JoshuaRotimi / BetweenNums.js
Created November 20, 2023 15:07
Returns all numbers either even, odd or prime between a range of numbers.
function numbersBetween(num1, num2) {
const min = Math.min(num1, num2);
const max = Math.max(num1, num2);
const result = [];
for (let i = min + 1; i < max; i++) {
result.push(i);
}
return result;
@JoshuaRotimi
JoshuaRotimi / scoreWordGame.js
Created November 6, 2023 10:40
Given a list of words and a dictionary of letter scores, find the word with the highest score according to the rules.
/*
Given a list of words and a dictionary of letter scores, find the word with the highest score
according to the rules: score = word_length * (sum of letter scores in the word).
If there are multiple words with the same highest score,
return the lexicographically smallest one.
*/
const wordList = ["apple", "banana", "cherry", "date", "fig"];
const letterScores = [...Array(26).keys()].reduce(
@JoshuaRotimi
JoshuaRotimi / operators.js
Created October 2, 2023 17:30
Add Operators
const add = (nums) => {
const allNums = nums.toString().split("").map((num) => parseInt(num));
return allNums.reduce((acc, cur) => acc + cur, 0);
};
const multiply = (nums) => {
const allNums = nums.toString().split("").map((num) => parseInt(num));
return allNums.reduce((acc, cur) => acc * cur, 1);
};
@JoshuaRotimi
JoshuaRotimi / pokemonTypes.js
Created September 28, 2023 14:56
Fighting Types
const fetchStrengths = async (url) => {
try {
const strengths = await fetch(url);
const allStrengths = await strengths.json();
const damageCaused = allStrengths.damage_relations.double_damage_from.map(
(item) => item.name
);
const damageDone = allStrengths.damage_relations.double_damage_to.map(
(item) => item.name
);