Skip to content

Instantly share code, notes, and snippets.

@ncrohn
Created September 26, 2012 16:53
Show Gist options
  • Save ncrohn/3789151 to your computer and use it in GitHub Desktop.
Save ncrohn/3789151 to your computer and use it in GitHub Desktop.
{
indexOf: function(arr, item) {
var ret = -1, i;
for(i=0; i<arr.length; i++) {
if(arr[i] === item) {
ret = i;
break;
}
}
return ret;
},
square : function(arr) {
var x, i;
for(i=0; i<arr.length; i++) {
x = parseFloat(arr[i]);
arr[i] = x * x;
}
return arr;
},
fizzBuzz : function(num) {
// write a function that receives a number as its argument;
// if the number is divisible by 3, the function should return 'fizz';
// if the number is divisible by 5, the function should return 'buzz';
// if the number is divisible the 3 and 5, the function should return
// 'fizzbuzz';
//
// otherwise the function should return the number, or false if no number
// was provided
// This is a case when multiple returns is okay, it immediately returns us if we don't meet the requirements of this function
// saving us from performing any additional computation.
if(typeof num !== 'number') return false;
var ret = '';
if(num % 3 === 0) {
ret += 'fizz';
}
if(num % 5 === 0) {
ret += 'buzz';
}
if(ret === '') {
ret = num;
}
return ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment