You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.8 KiB
2.8 KiB
WDI-PANTHALASSA
Title: Let's Look at the Differences w010d02
Type: Morning Exercise
Duration: "0:45"
Creator:
Original creators: WDI-Archer & WDI-Funke
Adapted by: Kristyn Bryan
Course: WDIr Panthalassa
Competencies: Ruby
Prerequisites: Javascript & Ruby
Morning Exercise
##Let's Look at the Differences
Rewrite the following Javascript functions in Ruby
Get Name
var getName = function () {
var name = prompt("what is your name?");
return name;
};
###Reverse It
var reverseIt = function () {
var string = "a man, a plan, a canal, frenemies!";
var reverse = "";
for (var i=0; i < string.length; i++) {
reverse += string[string.length - (i+1)];
};
alert(reverse);
};
###Swap Em
var swapEm = function () {
var a = 10;
var b = 30;
var temp;
temp = b;
b = a;
a = temp;
alert("a is now " + a + ", and b is now " + b);
};
###Multiply Array
var multiplyArray = function (ary) {
if (ary.length == 0) { return 1; };
var total = 1;
// var total = ary[0];
for (var i=0; i < ary.length; i++) {
total = total * ary[i];
};
return total;
};
Fizz Buzzer
var fizzbuzzer = function(x){
if((x%5 === 0) & (x%3 === 0 )) {
return 'fizzbuzz'
} else if( x%3 == 0 ) {
return 'fizz'
} else if ( x%5 == 0 ) {
return 'buzz'
} else {
return 'Panthalassa'
}
}
###Nth Fibonacci
var nthFibonacciNumber = function () {
var fibs = [1, 1];
var num = prompt("which fibonacci number do you want?");
while (fibs.length < parseInt(num)) {
var length = fibs.length;
var nextFib = fibs[length - 2] + fibs[length - 1];
fibs.push(nextFib);
}
alert(fibs[fibs.length - 1] + " is the fibonacci number at position " + num);
};
Search Array
var searchArray = function(array,value) {
for(var i = 0; i < array.length-1; i++) {
if(array[i] == value) {
return true;
break;
}
}
return -1;
};
Palindrome
Write a method that checks whether or not a string is a palindrome. Here is the javascript:
var isPalindrome = function(str) {
for(var i = 0; i < str.length/2; i++){
if(str[i] != str[str.length-i-1]){
return false;
break;
}
return true;
}
};
hasDupes
Write a method that checks whether or not an array has any duplicates. Here is the javascript:
var hasDupes = function(arr){
var obj = {};
for (i = 0; i < arr.length; i++) {
if(obj[arr[i]]) {
return true;
}
else {
obj[arr[i]] = true;
}
}
return false;
};

