Skip to content

Instantly share code, notes, and snippets.

@CodeLikeAGirl29
Last active March 12, 2024 09:16
Show Gist options
  • Save CodeLikeAGirl29/3a3ce2e9d204a4ce3f4ae31811d32469 to your computer and use it in GitHub Desktop.
Save CodeLikeAGirl29/3a3ce2e9d204a4ce3f4ae31811d32469 to your computer and use it in GitHub Desktop.
Popular solutions to some javascript problems.

library.js

const inventory = {
  sunglasses: 1900,
  pants: 1088,
  bags: 1344
};

const checkInventory = (order) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      let inStock = order.every(item => inventory[item[0]] >= item[1]);
      if (inStock) {
        resolve(`Thank you. Your order was successful.`);
      } else {
        reject(`We're sorry. Your order could not be completed because some items are sold out.`);
      }
    }, 1000);
  })
};

module.exports = { checkInventory };

app.js

const {checkInventory} = require('./library.js');

const order = [['sunglasses', 1], ['bags', 2]];

// Write your code below:
const handleSuccess = (resolvedValue) => {
  console.log(resolvedValue);
};

const handleFailure = (rejectReason) => {
  console.log(rejectReason);
};

checkInventory(order)
  .then(handleSuccess, handleFailure);

This is one of Codecademy's practice code for async javascript practice!

Js Calculator

The goal for this exercise is to create a calculator that does the following:

add, subtract, get the sum, multiply, get the power, and find the factorial

In order to do this please fill out each function with your solution. Make sure to return the value so you can test it in Jest! To see the expected value take a look at the spec file that houses the Jest test cases.

const add = function (a, b) {
  return a + b;
};

const subtract = function (a, b) {
  return a - b;
};

const sum = function (array) {
  return array.reduce((total, current) => total + current, 0);
};

const multiply = function(...args){
  let product = 1;
  for (let i = 0; i < args.length; i++) {
    product *= args[i];
  }
  return product;
 };

const power = function (a, b) {
  return Math.pow(a, b);
};

const factorial = function (n) {
  if (n === 0) return 1;
  let product = 1;
  for (let i = n; i > 0; i--) {
    product *= i;
  }
  return product;
};

// This is another implementation of Factorial that uses recursion
// THANKS to @ThirtyThreeB!
const recursiveFactorial = function (n) {
  if (n === 0) {
    return 1;
  }
  return n * recursiveFactorial(n - 1);
};

Palindromes

const palindromes = function (string) {
  const processedString = string.toLowerCase().replace(/[^a-z0-9]/g, "");
  return processedString.split("").reverse().join("") == processedString;
};

Fibonacci

const fibonacci = function(count) {
	if (count < 0) return "OOPS"
	const fibPart = [0, 1];
	for (let index = 1; index < count; index++) {
		fibPart.push(fibPart[index] + fibPart[index -1]);
	}
	return fibPart[count];
};

Get the Titles from an Object

You are given an array of objects that represent books with an author and a title that looks like this:

const books = [
  {
    title: 'Book',
    author: 'Name'
  },
  {
    title: 'Book2',
    author: 'Name2'
  }
]

Your job is to write a function that takes the array and returns an array of titles:

const getTheTitles = function (array) {
  return array.map((book) => book.title);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment