ag 1 Flashcards
Sum all the prime numbers up to and including the provided number.
A prime number is defined as a number greater than one and having only two divisors, one and itself. For example, 2 is a prime number because it’s only divisible by one and two.
The provided number may not be a prime.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
For Loops
Array.prototype.push()
function primeVerified(num){
for (var j = 2 ; j < num ; j++ ) { if ( num % j === 0 ) { return false; } } return true; }
function sumPrimes(number){ var totalz = 0 ; for (var a = 2 ; a <= number ; a++ ){ if ( primeVerified(a)){ totalz += a; console.log(totalz);
} } return totalz; }
sumPrimes (977);
Given a positive integer num, return the sum of all odd Fibonacci numbers that are less than or equal to num.
The first two numbers in the Fibonacci sequence are 1 and 1. Every additional number in the sequence is the sum of the two previous numbers. The first six numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8.
For example, sumFibs(10) should return 10 because all odd Fibonacci numbers less than 10 are 1, 1, 3, and 5.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
function getSum(total, num){ return total + num;
}
function sumFibs(number) {
var start = 0, next = 1, sum = 1, total = 1, totalOdds
= [], fibb = [1], currentSum = 0;
for(var i = 2; next <= number; i++) {
fibb.push(next);
if ( sum % 2 !== 0 ) {
totalOdds.push(next);
currentSum = totalOdds.reduce(getSum);
console.log(currentSum);
} sum = start + next; start = next; next = sum; total += sum;
} return currentSum; }
sumFibs (1000);
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
RegExp
String.prototype.replace()
function spinalCase(str) {
// "It's such a fine line between stupid, and clever." // --David St.
var res = str.replace( /[A-Z]/g, function(x){ return "-" + x; }); var sam = res.replace( /[_ ]/g, function(x){ return "-"; }); var mas = sam.replace( /--/g, function(x){ return "-"; }); if ( mas[0] == "-" ){ mas = mas.substring(1, mas.length); } return mas.toLowerCase();
}
spinalCase(“thisIsSpinalTap”) ;
spinalCase(“The_Andy_Griffith_Show”) ;
spinalCase(“Teletubbies say Eh-oh”) ;
spinalCase(“AllThe-small Things”) ;
Convert the characters &, , “ (double quote), and ‘ (apostrophe), in a string to their corresponding HTML entities.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
RegExp
HTML Entities
String.prototype.replace()
function convertHTML(str) { str = str.replace(/&/gmi, "&") ; str = str.replace(//gmi, ">" ) ; str = str.replace(/'/gmi, "'") ; str = str.replace(/"/gmi, """) ; return str; } convertHTML("Dolce & Gabbana");
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 array should not be sorted in numerical order.
Check the assertion tests for examples.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
var valid; function uniteUnique(arr) { var args = arguments; for ( var x = 0 ; x < args.length ; x++ ) { for ( var a = 0 ; a < args[x].length ; a++ ) { for ( var i = 0 ; i < args[0].length ; i++ ) { valid = true; if ( args[x][a] == args[0][i] ) { valid = false; break; } } if( valid ) { arr.push(args[x][a]); } } } return arr; }
uniteUnique([1, 3, 2], [1, [5]], [2, [4]]);
Check if a value is classified as a boolean primitive. Return true or false.
Boolean primitives are true and false.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Boolean Objects
function booWho(bool) { // What is the new fad diet for ghost developers? The Boolean
return ((typeof bool) == “boolean”);
}
Find the missing letter in the passed letter range and return it.
If all letters are present in the range, return undefined.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
String.prototype.charCodeAt()
String.fromCharCode()
function fearNotLetter(str) { var array, y; var z = []; console.log("checking " + str); for (var eachLetter in str ) { var curr = str.charCodeAt(eachLetter); var prev; // if its the first time, set the prev letter and go if( eachLetter == 0 ) { prev = curr; } else { // otherwise make the comparison if ( curr - prev == 1 ) { // if sequential, continue } else { // if non sequential console.log("NON sequential ARRAY");
console.log("ANSWER: "+ String.fromCharCode(prev + 1)); return String.fromCharCode(prev + 1);
} // set the current # as the prev for the next loop prev = curr;
}
}
console.log("sequential ARRAY"); return undefined;
}
fearNotLetter( “abce” ) ;
fearNotLetter( “abcdefghjklmno” ) ;
fearNotLetter( “bcd” ) ;
fearNotLetter( “yz” ) ;
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”]]
The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Array.prototype.push()
String.prototype.split()
function pairElement ( str ) { var pairsOfDNA = {A: 'T', T: 'A' , C: 'G', G: 'C'}; return str.split("").map(function(otherPair){
return [otherPair,pairsOfDNA[otherPair]]; });
}
Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like “the” and “of”.
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
String.prototype.split()
// refactored function titleCase(a) { var b, y, u, z; var q = "";
b = a.toLowerCase().split(" "); for ( var x = 0 ; x < b.length ; x++ ){
y = b[x].split(""); u = y[0].toUpperCase(); y.splice(0,1,u).join(""); z = y.join(""); if( q === "" ){ q = z ; }else { q = q + " " + z; }
}
console.log(q);
return q;
}
titleCase(“HERE IS MY HANDLE HERE IS MY SPOUT”);
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.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Array.prototype.indexOf() Array.prototype.push() Array.prototype.join() String.prototype.substr() String.prototype.split()
var str2, vw, chunk, str3, str4;
function translatePigLatin(str) { // split to array str2 = str.split(""); // find index of first vowel vw = str.search(/[aeiou]/i); // move chunk to the back chunk = str.substr(0,vw); if(vw > 0){ str3 = str2.splice( vw ).concat(chunk + "ay") ; } else { str3 = str2.splice( vw ).concat("way") ; } str4 = str3.join(""); return str4; }
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 original word when you are replacing it. For example if you mean to replace the word “Book” with the word “dog”, it should be replaced as “Dog”
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Array.prototype.splice()
String.prototype.replace()
Array.prototype.join()
function myReplace(str, before, after) {
var after2; // test the capitalization var capitalized = ( /[A-Z]/.test( before[0]) ); // split to an array var splitted = after.split(""); // adjust the capitalization if(capitalized){ after2 = splitted[0].toUpperCase(); } else { after2 = splitted[0].toLowerCase(); } // splice into one string splitted.splice(0,1,after2); // place in an array var joined = splitted.join(""); var str2 = str.replace(before, joined); return str2; }
myReplace(“A quick brown fox jumped over the lazy dog”, “jumped”, “leaped”);
Convert the given number into a roman numeral.
All roman numerals answers should be provided in upper-case.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Roman Numerals
Array.prototype.splice()
Array.prototype.indexOf()
Array.prototype.join()
function convertToRoman(n) { var r = ""; var thousands, hundreds, tens, ones = 0;
while ( n >= 1000 ) { r += "M" ; n -= 1000 ; } while ( n >= 900 ) { r += "CM" ; n -= 900 ; } while ( n >= 500 ) { r += "D" ; n -= 500 ; } while ( n >= 400 ) { r += "CD" ; n -= 400 ; } while ( n >= 100 ) { r += "C" ; n -= 100 ; } while ( n >= 90 ) { r += "XC" ; n -= 90 ; } while ( n >= 50 ) { r += "L" ; n -= 50 ; } while ( n >= 40 ) { r += "XL" ; n -= 40 ; } while ( n >= 10 ) { r += "X" ; n -= 10 ; } while ( n >= 9 ) { r += "IX" ; n -= 9 ; } while ( n >= 5 ) { r += "V" ; n -= 5 ; } while ( n >= 4 ) { r += "IV" ; n -= 4 ; } while ( n >= 1 ) { r += "I" ; n -= 1 ; }
console.log(r);
return r;
}
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Comparison Operators Array.prototype.slice() Array.prototype.filter() Array.prototype.indexOf() Array.prototype.concat()
function diffArray(arr1, arr2) { var newArr = []; var validNumber;
for (var i = 0; i < arr1.length; i++) {
validNumber = true; for ( a = 0 ; a < arr2.length ; a ++ ){ if( arr1[i] == arr2[a] ){ validNumber = false; break; } } if(validNumber === true){ newArr.push(arr1[i]); } }
for (i = 0; i < arr2.length; i++) {
validNumber = true; for ( a = 0 ; a < arr1.length ; a ++ ){ if( arr2[i] == arr1[a] ){ validNumber = false; break; } } if(validNumber === true){ newArr.push(arr2[i]);
} }
console.log(newArr);
return newArr;
}
diffArray([1, 2, 3, 5], [1, 2, 3, 4, 5]);
We’ll pass you an array of two numbers. Return the sum of those two numbers and all numbers between them.
The lowest number will not always come first.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
Math.max()
Math.min()
Array.prototype.reduce()
function sumAll(arr) { var big = Math.max(arr[0],arr[1]); var small = Math.min(arr[0],arr[1]); var diff = big-small; var total = small; for (a = 0 ; a < diff ; a++){ total += small + 1; small = small+ 1; }
return total;
}
One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus ‘A’ ↔ ‘N’, ‘B’ ↔ ‘O’ and so on.
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
String.prototype.charCodeAt()
String.fromCharCode()
function rot13(str){
function upperToHyphenLower(match){ return String.fromCharCode(((match.charCodeAt(match) - 13) < 65) ? match.charCodeAt(match) + 13 : match.charCodeAt(match) - 13); }
var newstr = str.replace(/[A-Z]/gi, upperToHyphenLower);
return newstr;
}