ag 1 Flashcards

1
Q

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()

A

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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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:

A
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);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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()

A

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”) ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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()

A
function convertHTML(str) {
      str = str.replace(/&amp;/gmi, "&amp;")   ; 
      str = str.replace(//gmi, ">" )   ;
      str = str.replace(/'/gmi, "'")  ;
      str = str.replace(/"/gmi, """)  ;
    return str;
}
convertHTML("Dolce &amp; Gabbana");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A
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]]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A
function booWho(bool) {
  // What is the new fad diet for ghost developers? The Boolean

return ((typeof bool) == “boolean”);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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()

A
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” ) ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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()

A
function pairElement ( str ) {
  var pairsOfDNA = {A: 'T', T: 'A' , C: 'G', G: 'C'};
  return str.split("").map(function(otherPair){
return [otherPair,pairsOfDNA[otherPair]];   });

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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()

A
// 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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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()
A

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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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()

A

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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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()

A
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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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()
A
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]);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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()

A
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;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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()

A

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;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number.

For example, getIndexToIns([1,2,3,4], 1.5) should return 1 because it is greater than 1 (index 0), but less than 2 (index 1).

Likewise, getIndexToIns([20,3,5], 19) should return 2 because once the array has been sorted it will look like [3,5,20] and 19 is less than 20 (index 2) and greater than 5 (index 1).

Remember to use Read-Search-Ask if you get stuck. Write your own code.

Here are some helpful links:

Array.prototype.sort()

A
function getIndexToIns(arr, num) {
  // Find my place in this sorted array.
  arr.sort(function( a , b ){
    return a - b ;
  });
  console.log(arr);
  for ( i = 0 ; i < arr.length ; i++ ){
    console.log( "is " + num + " > or = to " + arr[ i ] + " ? " );
    // if its == it goes right there
      if( num == arr[ i ] ) {
          return i;
        }
          if( num > arr[ i ] ) {
          // num is greater then the prev element, see if its less then the next one
          console.log("it is, so is it less then " + arr[( i + 1 )] + "?");
      if(  num <= arr[( i + 1 )] || arr[( i + 1 )] == undefined ) {

          console.log("it is or theres no next number to check, so insert " + num + " at index *** " + ( i + 1 ) + " ****");

          return i + 1 ;
          } 

      }

  } 

}