Skip to content

Instantly share code, notes, and snippets.

Avatar

Nicki Hastings nickihastings

View GitHub Profile
@nickihastings
nickihastings / movie-quotes.js
Created Feb 1, 2018
ajax code used to import a JSON API for random quote machine
View movie-quotes.js
$(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) {
@nickihastings
nickihastings / geolocate.js
Created Feb 6, 2018
Javascript function to get the latitude and longitude gelocation info from the browser.
View geolocate.js
$(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 / 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
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
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
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(vowels.includes(strArr[i])){
//if the first letter is a vowel just add 'way' to the end.
if(i == 0){
@nickihastings
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"]);
break;
case "G":
@nickihastings
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
nickihastings / boo-who.js
Created Mar 22, 2018
Check if a value is classified as a boolean primitive. Return true or false. Boolean primitives are true and false.
View boo-who.js
function booWho(bool) {
// What is the new fad diet for ghost developers? The Boolean.
//check if the value 'bool' is strictly true or false, this means the value is a Boolean Primitive and should return true.
//Everything else should return false.
if(bool === true || bool === false){
return true;
}
else {
return false;
}
@nickihastings
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){
args.push(arguments[i][j]);
}
You can’t perform that action at this time.