Skip to content

Instantly share code, notes, and snippets.

View nickihastings's full-sized avatar
🏠
Working from home

Nicki Hastings nickihastings

🏠
Working from home
View GitHub Profile
@nickihastings
nickihastings / dna-pairs.js
Created March 20, 2018 19:35
The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array. Base pairs are a pair of AT and CG. Match the missing element to the provided character. Return the provided character as the first element in each array. For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]…
function pairElement(str) {
str = str.split("");
var pairs = [];
for(var i = 0; i < str.length; i++){
switch (str[i]) {
case "C":
pairs.push(["C", "G"]);
break;
case "G":
@nickihastings
nickihastings / pig-latin.js
Created March 20, 2018 08:18
Translate the provided string to pig latin. Pig Latin takes the first consonant (or consonant cluster) of an English word, moves it to the end of the word and suffixes an "ay". If a word begins with a vowel you just add "way" to the end. Input strings are guaranteed to be English words in all lowercase.
function translatePigLatin(str) {
var vowels = ['a','e','i','o','u'];
var strArr = str.split(''); //split the string into an array
//walk through the string array and test for vowels.
for(var i = 0; i<strArr.length; i++){
//if the letter is a vowel create the new string
if(vowels.includes(strArr[i])){
//if the first letter is a vowel just add 'way' to the end.
if(i == 0){
@nickihastings
nickihastings / search-and-replace.js
Created March 16, 2018 20:11
Perform a search and replace on the sentence using the arguments provided and return the new sentence. First argument is the sentence to perform the search and replace on. Second argument is the word that you will be replacing (before). Third argument is what you will be replacing the second argument with (after). NOTE: Preserve the case of the …
function myReplace(str, before, after) {
//search for the before string and then use a function to check for capitalisation before replacing.
str = str.replace(before, function(x){
//check whether the first letter of before is a capital
//replace after with a capitalised first letter and then
//the rest of the string, cannot just replace first letter as its read only.
if(before[0] == before[0].toUpperCase()){
after = after[0].toUpperCase() + after.slice(1);
return after;
}
@nickihastings
nickihastings / wherefore-art-thou.js
Created March 15, 2018 20:48
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching property and value pairs (second argument). Each property and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array. From FreeCodeCamp
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var sKeyVal = [];
sKeyVal.push( Object.keys(source) ); //find the keys in the source
sKeyVal.push( Object.values(source) ); //find the values in source
var match = 0;
@nickihastings
nickihastings / roman-numeral-converter.js
Last active August 22, 2023 14:29
Convert the given number into a roman numeral. All roman numerals answers should be provided in upper-case.
function convertToRoman(num) {
var singles = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
var tens = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
var hundreds = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
var thousands = ["", "M", "MM", "MMM", "MMMM"];
//turn the number to a string, split individual characters and then turn each one to a number.
var length = num.toString().length;
var numbers = num.toString().split("").map(Number);
var roman = '';
@nickihastings
nickihastings / geolocate.js
Created February 6, 2018 22:06
Javascript function to get the latitude and longitude gelocation info from the browser.
$(document).ready(function(){
var latitude = '';
var longitude = '';
navigator.geolocation.getCurrentPosition(function(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('lat=' + latitude + ' long=' + longitude);
});
});
@nickihastings
nickihastings / movie-quotes-tweet.js
Created February 2, 2018 22:08
Random movie quote generator jquery ajax call to api and tweet button to tweet out quote.
$(document).ready(function(){
//get the quote api information with Ajax
getQuote();
var bgCols = ['#2e67f7','#ea5c30','#38b582','#683a20','#59357d', '#555'];
function getQuote(){
var num = Math.floor(Math.random()*6+1);
$.ajax({
url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=1",
type: "GET",
@nickihastings
nickihastings / movie-quotes.js
Created February 1, 2018 21:31
ajax code used to import a JSON API for random quote machine
$(document).ready(function(){
//get the quote api information with Ajax
getQuote();
function getQuote(){
$.ajax({
url: "https://andruxnet-random-famous-quotes.p.mashape.com/?cat=movies&count=1",
type: "GET",
data: {},
dataType: "json",
beforeSend: function(xhr) {