Skip to content

Instantly share code, notes, and snippets.

@harshpatel
Last active August 29, 2015 14:10
Show Gist options
  • Save harshpatel/c9dd5c8ec784cdce9337 to your computer and use it in GitHub Desktop.
Save harshpatel/c9dd5c8ec784cdce9337 to your computer and use it in GitHub Desktop.

Coding Challenge #1

When you've completed all exercises, email a .js file with all your answers to admissions@makersquare.com.


Exercise #1.1

Write a function yell that takes a string and console logs that string in all caps.

var yell = function (string) {
  // TODO
}

Exercise #1.2

Write a function addFive such that the following code works:

var addFive = function (x) {
  // TODO
}

console.log("addFive(3) should be 8:", addFive(3));
console.log("addFive(6) should be 11:", addFive(6));

Exercise #1.3

Write a function divideBy such that the following code works:

var divideBy = ???? // TODO

var result = divideBy(50, 2);
console.log('Result should be 25:', result);

result = divideBy(40, 10);
console.log('Result should be 4:', result);

result = divideBy(99, 3);
console.log('Result should be 33:', result);

Exercise #2.1

Fix the following code (1 syntax error) so that the function returns 'SLAP':

var slap = function {
  return 'SLAP';
};
slap();

Exercise #2.2

Fix the following code (1 syntax error) so that the function runs:

var poke = function (name) {
  return name ' reproaches your behavior.';
};
poke('Billy');

Exercise #2.3

Fix the following code (2 logic errors) so that the function returns a sensible message:

var doubleIt = function (x) {
  return X + ' times two is ' + x;
};
doubleIt(8);

Exercise #2.4

Fix the following code (1 logic error) so that the function runs:

var greet = function () {
  return "Welcome, " + name;
};
greet('Bob');

Exercise #2.5

Fix the following code (1 syntax error, 1 logic error) so that the function runs:

var poke = fuction () {
  console.log('ow');
};
poke;

Exercise #2.6

Fix the following code (1 syntax error, 1 logic error) so that the code alert the correct message:

var askify = function (request) {
  "Can you please " request + "?";
};

var result = askify('fix me');
console.log('Result should be "Can you please fix me?":', result);

Exercise #2.7

Fix the following code (2 syntax errors) so that the function runs:

var multiplyString = function (string, times) {
  if (times === 0) {
    return '';
  }
  else {
    return string + multiplyString(string times - 1);
  }
};

var result = multiplyString('Mike' 5);
console.log(result);

Exercise #3.1

Write a JavaScript program that prompts the user for a number:

  • If the number is divisible by 7, alert a lucky message
  • If the number is even, alert that they are an even steven.

Exercise #3.2

Write a JavaScript program that prompts the user for a password of your choice:

  • If correct, it alerts an access granted message
  • If not correct, it alerts an access denied message
  • Only allow the user to try up to three times.

Exercise #3.3

Write a JavaScript program that prompts three times, and then shows a single alert with all three strings in the opposite order they were prompted.


Exercise #4

Write a function welcome that:

  • Takes two parameters name1 and name2
  • If two parameters are present, returns "Welcome, #{name1} and #{name2}!"
  • If only one parameter is present, returns "Welcome, #{name1}!"

Remember JavaScript does not have string interprolation, so you can't just use "#{name1}".

var welcome = ???? // TODO

var result = welcome('Alice', 'Bob');
console.log('Result should be "Welcome, Alice and Bob!"', result);

var result = welcome('Alice');
console.log('Result should be "Welcome, Alice!"', result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment