Skip to content

Instantly share code, notes, and snippets.

Working from home

Nicki Hastings nickihastings

Working from home
View GitHub Profile
nickihastings / movie-quotes.js
Created Feb 1, 2018
ajax code used to import a JSON API for random quote machine
View movie-quotes.js
//get the quote api information with Ajax
function getQuote(){
url: "",
type: "GET",
data: {},
dataType: "json",
beforeSend: function(xhr) {
nickihastings / geolocate.js
Created Feb 6, 2018
Javascript function to get the latitude and longitude gelocation info from the browser.
View geolocate.js
var latitude = '';
var longitude = '';
navigator.geolocation.getCurrentPosition(function(position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
console.log('lat=' + latitude + ' long=' + longitude);
nickihastings / roman-numeral-converter.js
Last active Mar 14, 2018
Convert the given number into a roman numeral. All roman numerals answers should be provided in upper-case.
View roman-numeral-converter.js
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 / wherefore-art-thou.js
Created Mar 15, 2018
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
View wherefore-art-thou.js
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 / search-and-replace.js
Created Mar 16, 2018
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 …
View search-and-replace.js
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 / pig-latin.js
Created Mar 20, 2018
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.
View pig-latin.js
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 the first letter is a vowel just add 'way' to the end.
if(i == 0){
nickihastings / dna-pairs.js
Created Mar 20, 2018
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"]…
View dna-pairs.js
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"]);
case "G":
nickihastings / missing-letters.js
Created Mar 21, 2018
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
View missing-letters.js
function fearNotLetter(str) {
//in unicode A=65 and Z=90, a=97 and z=122
//get the starting letter's unicode, and then add one to check the next letter
var start = str.charCodeAt(0) + 1;
var missing = ''; //store missing letters
//loop over the string, get the letters unicode value and compare it to what the next letter's code should be:
for(var i = 1; i < str.length; i++){
//if the codes don't match append the letter to the variable
if(str.charCodeAt(i) !== start){
nickihastings / sorted-union.js
Created Mar 22, 2018
Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays. In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array. The unique numbers should be sorted by their original order, but the final …
View sorted-union.js
function uniteUnique(arr) {
var args = [];
//loop over the arguments provided and then loop over each array
//within the arguments, check if the value is already in the new
//array, if it's not, add it in.
for(var i = 0; i < arguments.length; i++){
for(var j = 0; j<arguments[i].length; j++){
if(args.indexOf(arguments[i][j]) == -1){
nickihastings / convert-html-entities.js
Created Mar 23, 2018
Convert the characters &, <, >, " (double quote), and ' (apostrophe), in a string to their corresponding HTML entities.
View convert-html-entities.js
function convertHTML(str) {
// &colon;&rpar;
//create an object to store the html conversions
var htmlEntities = {
'&' : '&amp;',
'<' : '&lt;',
'>' : '&gt;',
'"' : '&quot;',
"'" : '&apos;'