DecodeMTL Application by Alan Nguy

node v0.12.18
version: 1.0.0
endpointsharetweet
Write a function called firstLetter that takes one string argument and returns the first letter of the string.
var firstLetter = function (word) { console.log(word.substring(0,1)); }; firstLetter("decode");
Write a function called lastLetter that takes one string argument and returns the last letter of the string.
var lastLetter = function (word) { console.log(word.slice(-1)); }; lastLetter("decode");
Write a function called crazySum that takes an array of numbers, and returns the sum of each number multiplied by its position in the array. For example, if you pass [4, 8, 15, 16], the program will calculate 4*1 + 8*2 + 15*3 + 16*4 and give you that back as an answer.
myArray = [4,8,15,16]; function sum(crazySum) { return crazySum.reduce(function(a, b) { return a + b; }); }; sum(myArray.map(function(elt, idx) { return elt * idx; }));
Write a function called fizzBuzz that doesn't take any arguments and doesn't return anything. This function should print all the numbers from 1 to 100. If the number is dividable by 3, then it should print FIZZ instead of the number. If the number is dividable by 5, then it should print BUZZ instead of the number. If the number is dividable by 3 AND 5, then it should print FIZZBUZZ instead of the number.
function fizzBuzz() { for (x=1; x <= 100; x++){ if(x%3 == 0) { console.log("FIZZ"); }; if(x%5 == 0) { console.log("BUZZ"); }; if((x%3 != 0) && (x%5 != 0)) { console.log(x); }; if((x%3 == 0) && (x%5 == 0)) { console.log("FIZZBUZZ"); }; }; }; fizzBuzz(100);
Write a function called factorial that takes a number as argument and returns the factorial of that number.
var factorial = function fac(n) { return n<2 ? 1 : n*fac(n-1); }; console.log(factorial(4));
Loading…

no comments

    sign in to comment