Quotes help make search much faster. Example: "Practice Makes Perfect"

Sunday, February 3, 2013

Sqlcourse Selecting Data Exercise 1

Problem: 

Display the first name and age for everyone that's in the table.

Solution:

select first, age from empinfo;

Friday, February 1, 2013

Codecademy: Bind It In Chains


// Sample solution

// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
    n %= this.length; // too large numbers: modulo
    var cut = n < 0 ? -n : this.length - n; // cutting point
    return this.substr(cut) + this.substr(0,cut);
};

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");

var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");

array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
//rot13_dict;
function rot13_map(somechar){
if(alphabet.indexOf(somechar)>=0 || alphabet.toLowerCase().indexOf(somechar)>=0){
return rot13_dict[somechar];
} else {
return somechar;
}
}

//console.log(rot13_map('?'));
//console.log(rot13_map('A'));

function rot13(somestring){
if(somestring.length>1){
return somestring.split("").map(function(n){ return rot13_map(n);}).join("");
} else {
return rot13_map(somestring);
}
}

Codecademy: Wrap It!


// Sample solution

// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
    n %= this.length; // too large numbers: modulo
    var cut = n < 0 ? -n : this.length - n; // cutting point
    return this.substr(cut) + this.substr(0,cut);
};

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");

var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");

array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
//rot13_dict;
function rot13_map(somechar){
if(alphabet.indexOf(somechar)>=0 || alphabet.toLowerCase().indexOf(somechar)>=0){
return rot13_dict[somechar];
} else {
return somechar;
}
}

console.log(rot13_map('?'));
console.log(rot13_map('A'));

Codecademy: Write A Dictionary


// Sample solution

// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
    n %= this.length; // too large numbers: modulo
    var cut = n < 0 ? -n : this.length - n; // cutting point
    return this.substr(cut) + this.substr(0,cut);
};

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var rot13_dict = {};
var newString = alphabet + "" + alphabet.toLowerCase();
var array1 = newString.split("");

var newString2 = alphabet.rotate(13) + "" + alphabet.rotate(13).toLowerCase();
var array2 = newString2.split("");

array1.forEach(function(element, index, array1){
rot13_dict[element]=array2[index];
});
rot13_dict;

Codecademy: ROT Stands For Rotation


// Sample solution

// Paste the rotate function here:
String.prototype.rotate = function(n) { // n is an integer
    n %= this.length; // too large numbers: modulo
    var cut = n < 0 ? -n : this.length - n; // cutting point
    return this.substr(cut) + this.substr(0,cut);
};

var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Now use it:
console.log(alphabet.rotate(13));
console.log(alphabet.rotate(13).rotate(13));

Codecademy: Getting Closer To String Translation


// Sample solution

// This is a dummy, to be refined later
function mapping(character) {
    if (character >= 'A' && character <= 'z') {
        return 'x';
    }
    else return character;
}

function substitute(string) {
    /* your code here */
    return string.split("").map(function(n){ return mapping(n); }).join("");
}

// Test your function:
// this should print   "xxxx xx xxxxxxxxxx!"
console.log(substitute("Make me unreadable!"));

Codecademy: Inline Functions In Chains


// Sample solution

// Sum of squares of negatives, using a loop:
function sum_squares_negatives_loop(array) {
    var sum = 0;
    for (var i = 0; i < array.length; i++) {
        if (array[i] < 0) { // filter: negatives only
            sum += (array[i] * array[i]); // square & add
        }
    }
    return sum;
}

// Same thing without a loop, using Array methods:
function my_sum_squares_neg(array) {
    return array
        .filter( // filter negative numbers:
            function(n) {
                return n<0 ? n : null; // <-- true if and only if n is negative
            }
        )
              // at this point we have an array of negative numbers
        .map( // map each of these numbers to its square
            function(n) {
                return n * n; // <-- the square of n
            }
        )
                 // now, we have an array of squared numbers
        .reduce( // let's add them all together!
            function(sum, next) { // each array element in turn is next
                return sum+=next; // <-- add next (value) to sum
            }, 0 // set sum = 0 initially
        );
}

// test if your function does the same as the loop:
var numbers = [-23, 95, 0, -2, -47];
console.log(sum_squares_negatives_loop(numbers));
console.log(my_sum_squares_neg(numbers));

This is an example of scrolling text using Javascript.

Popular Posts