Skip to content

Instantly share code, notes, and snippets.

@vicsarbu
Last active August 25, 2016 00:12
Show Gist options
  • Save vicsarbu/1d5b3cb6df65b94c4ddae75453b18911 to your computer and use it in GitHub Desktop.
Save vicsarbu/1d5b3cb6df65b94c4ddae75453b18911 to your computer and use it in GitHub Desktop.
/* 1. Write a function average that takes two numbers as input (parameters), and returns the average of those numbers. */
var average = function(x , y){
return (x+y)/2;
};
/* 2. Write a function greeter that takes a name as an argument and greets that name by returning something along the lines of "Hello, <name>!" */
var greeter = function(name){
return "Hello, " + name + "!";
};
/* 3. Write the following functions that each accept a single number as an argument:
even: returns true if its argument is even, and false otherwise.
odd: the opposite of the above.
positive: returns true if its argument is positive, and false otherwise.
negative: the opposite of the above. */
var isEven = function(x){
if(x%2 == 0){
return true;
}else{
return false;
}
};
var isOdd = function(x){
if(x%3 == 1){
return true;
}else{
return false;
}
};
var isPositive = function(x){
if(x > 0){
return true;
}else{
return false;
}
};
var isNegative = function(x){
if(x < 0){
return true;
}else{
return false;
}
};
/* 4. Write a function sayHello that takes a language parameter, and returns "hello" in that language. Make the function work with at least three languages.*/
var sayHello = function(language){
switch(language){
case "Romanian":
return "Buna Ziua!";
case "German":
return "Hallo!";
case "Spanish":
return "Hola!";
default:
return "Sorry, don't know the greeting in that language";
}
};
/* 5. Write a function validCredentials that accepts two parameters: username and password, and returns true if both are long enough, and false otherwise. You can decide what constitutes "long enough". */
function validCredentials(username, password){
if(username.length && password.length == 20){
return true;
}else{
return false;
}
};
/* 6. Repeating a String n Times: Let's write a function called repeatString that takes two parameters: a string str, which is the string to be repeated, and count -- a number representing how many times the string str should be repeated, e.g.*/
function repeatString(str, count){
return str.repeat(count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment