Code Monkey home page Code Monkey logo

basic-algorithm-scripting-fcc's Introduction

Basic Algorithm Scripting (FreeCodeCamp) Solutions

This repository contains my answers to the challenges from FreeCodeCamp's Basic Algorithm Scripting module.

Celsius to Fahrenheit

function convertCtoF(celsius) {
  return fahrenheit = (celsius*(9/5))+32;
};

Reverse a String

function reverseString(str) {
    let temp = ``;
    for (let i = str.length-1; i > -1; i--){
    	temp += str[i];
    }
    str = temp;
    return str;
}

Factorialize a Number

function factorialize(num){
	if(num == 0){
		return 1;
	} else {
		return num * factorialize(num-1);
	}
}

Find the Longest Word in a String

function findLongestWordLength(str) {
	let arr, highest;
	str = str.replace(/[^a-z 0-9]/ig, "");
	arr = str.split(" ");
	
	highest = arr[0];
	for(let i=0; i<arr.length-1; i++){
		if(highest.length<arr[i+1].length){
        	highest = arr[i+1];
    		}
	}
	str = highest;
  	return str.length;
}

Return Largest Numbers in Arrays

function largestOfFour(arr) {
  let temp = [];
  for(let i=0; i<arr.length; i++){
      temp.push(Math.max(...arr[i]));
  }
  arr = temp;
  return arr;
}

Confirm the Ending

function confirmEnding(str, target) {
  return (str.slice(str.length-target.length)==target);
}

Repeat a String Repeat a String

function repeatStringNumTimes(str, num){
    if(num <= 0){
        return "";
    } else {
        return str + repeatStringNumTimes(str, num-1);
    }
}

Truncate a String

function truncateString(str, num) {
  return (str.length <= num) ? str
  : str = (`${str.slice(0, num)}...`);
}

Finders Keepers

function findElement(arr, func) {
  let num = 0;
  for(let elem of arr){
      if (func(elem)){
          return num = elem;
      }
  }
  return undefined;  
}

Boo Who

function booWho(bool) {
  return (typeof(bool) === "boolean") ? true : false;
}

Title Case a Sentence

function titleCase(str) {
    str = str.toLowerCase().split(" ");
    for(let i=0; i<str.length; i++){
        str[i] = str[i].charAt(0).toUpperCase().concat(str[i].slice(1));
    }
    return str.join(" ");
}

Slice and Splice

function frankenSplice(arr1, arr2, n) {
  let temp = arr2.slice(0);
  temp.splice(n, 0, ...arr1);
  return temp;
}

Falsy Bouncer

function bouncer(arr) {
  return arr.filter((arr) => (!!arr));
}

Where do I Belong

function getIndexToIns(arr, num) {
  if(arr.indexOf(num) < 0){
      arr.push(num);
  }
  return arr.sort(sortAscend).indexOf(num);
}

function sortAscend(a,b){ return a - b; }

Mutations

function mutation(arr) {
  let arr1 = arr[0].toLowerCase().split(""),
      arr2 = arr[1].toLowerCase().split("");
  for(let item of arr2){
      if(arr1.indexOf(item) < 0){
          return false;
      }
  }
  return true;
}

Chunky Monkey

function chunkArrayInGroups(arr, size) {
  let temp = [];
  for(let i=0; i<arr.length; i+=size){
      temp.push(arr.slice(i, i+size));
  }
  return arr = temp;
}

basic-algorithm-scripting-fcc's People

Contributors

denissejoyce avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.