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

Thursday, January 31, 2013

Codecademy: String Rotation


// Sample solution

// Write a string rotation method.
// It should pass all the tests given below.
String.prototype.rotate = function(number) {
    /* your code  */
    if(number===0 || number===this.length){
    return this;
    } else if(this.length>Math.abs(number)){
    if(number<0){
    number = Math.abs(number)+1;
    }
    } else if(this.length<Math.abs(number)){
    if(number>0){
    number = (Math.abs(number) % this.length)+2;
    } else {
    number = (Math.abs(number) % this.length)+1;
    }
    }
    var temp1 = this.substring(0,number-1);
    var temp2 = this.substring(number-1,this.length);
    return temp2+temp1; // return rotated string
};

/*********** Tests for your method ***********/
// The comments tell you the expected results.
// The correctness test will run all these tests, so even if you
// delete the lines below, you will still see error messages
// as long as your code doesn't pass all the tests.

console.log("ryEve".rotate(3));        // "Every"

// Zero rotation: no change
console.log("JavaScript".rotate(0));   // "JavaScript"

// Negative numbers: rotate the other way
console.log("mmerprogra".rotate(-4));  // "programmer"

// Rotate by length: no change
console.log("should".rotate(6));       // "should"
console.log("know".rotate( -4));       // "know"

// Larger than length: rotate by modulo
console.log("outab".rotate(22));       // "about"
// (actually the same as "outab".rotate(2); )
console.log("otypesprot".rotate(-36)); // "prototypes"
// (actually the same as "otypesprot".rotate(-6); )

Codecademy: String Reversal - Part 3


// Sample solution

// Change this function so as to reverse strings.
// When given argument "Hello", it should return "olleH".
String.prototype.reverse = function(){
    return this.split("").reverse().join("");
}

// test it:
str = "right to left";
console.log(str.reverse());

Codecademy: String Reversal - Part 2


// Sample solution

// Change this function so as to reverse strings.
// When given argument "Hello", it should return "olleH".
function reverse(str) {
    return str.split("").reverse().join("");
}

// test it:
console.log(reverse("right to left"));

Codecademy: Get Rid of the Man in the Middle


// Sample solution to Method Chaining

var date = "2012-03-16";

var converted_date = date.split('-').reverse().join('/');

Codecademy: String Reversal - Part 1


// Sample solution

var date = "2012-03-16";

// split the date by "-"
var date_as_array = date.split('-');

// reverse the order of elements in the array
var reversed_array = date_as_array.reverse();

// convert the reversed array back to a string using
// the join() method with "/" as separator
var converted_date = reversed_array.join('/');

Codecademy: What should I know?


// Sample solution

var phone = {};
phone.addZero = function(number){
var temp = number.toString();
return ('0'+temp);
}

phone.dialNumber = function(number){
var temp = this.addZero(number);
console.log("dialing "+temp);
}

// Let's tell our phone to call some number. If the number is
// "12345", it should print "dialing 012345" to the Console.
phone.dialNumber("12345");

Codecademy: What will I be learning?


// Sample solution

// Read this code
function makeTags(str) {
    return str
        .split(' ')
        .map(
            function(s) {return '<'+s+'>';}
        ).join('');
}

// Can you guess the result of  makeTags("p ul li")?
// Type in your guess, then hit "Run" to check
var myGuess = "<p><ul><li>";

This is an example of scrolling text using Javascript.

Popular Posts