The document provides 19 coding questions as warm-up exercises. Each question includes the code template to solve the problem, test cases to verify the solution, and a description of what the code should do. The questions range from easy to very difficult and cover topics like conditionals, strings, arrays, and more. The goal is to replace the ???? with code that will pass all the test cases provided for each question.
The document provides 19 coding questions as warm-up exercises. Each question includes the code template to solve the problem, test cases to verify the solution, and a description of what the code should do. The questions range from easy to very difficult and cover topics like conditionals, strings, arrays, and more. The goal is to replace the ???? with code that will pass all the test cases provided for each question.
The document provides 19 coding questions as warm-up exercises. Each question includes the code template to solve the problem, test cases to verify the solution, and a description of what the code should do. The questions range from easy to very difficult and cover topics like conditionals, strings, arrays, and more. The goal is to replace the ???? with code that will pass all the test cases provided for each question.
The document provides 19 coding questions as warm-up exercises. Each question includes the code template to solve the problem, test cases to verify the solution, and a description of what the code should do. The questions range from easy to very difficult and cover topics like conditionals, strings, arrays, and more. The goal is to replace the ???? with code that will pass all the test cases provided for each question.
The document discusses different coding questions being asked to warm up coding skills. It provides code templates to test the answers and expected outputs to verify if the answers are correct. Short coding questions ranging from basic logic to more complex string/array manipulations are provided along with hints/comments.
Questions about basic logic involving conditions with keywords like AND, OR are asked. Questions on arrays, strings with expected manipulations are also included.
Code templates with placeholders for answers are provided. Comments show the expected outputs to test the answers.
CODING QUESTIONS - WARM UP
(adapted from http://www.codingbat.com)
INSTRUCTIONS There is a code template for all the questions.
1. Replace ????? with your answer.
2. To test, copy the code template with your answer to http://repl.it/ and run your program. To avoid problems, refresh your htt://repl.it page after each answer.
3. Read the comments included with each code template to see if you have got the answer correct.
Q1 Sleep In The parameter weekday is true if it is a weekday, and the parameter vacation is true if we are on a vacation. We sleep in if it is not a weekday or were on a vacation. Return true if we sleep in.
// should return true console.log(sleepIn(false, false));
// should return false console.log(sleepIn(true, false));
// should return true console.log(sleepIn(false, true));
Q2 Monkey Trouble We have two monkeys, a and b, and the parameters aSmile and bSmile indicate if each is smiling. We are in trouble if they are both smiling or if neither of them is smiling. Return true if we are in trouble.
function sumDouble(a, b) { if ( ?????? ) { return ????? } else { return ???? } }
// should return 3 console.log(sumDouble(1,2))
// should return 5 console.log(sumDouble(3,2));
// should return 8 console.log(sumDouble(2,2));
Q4 Parrot Trouble We have a loud talking parrot. The "hour" parameter is the current hour time in the range 0..23. We are in trouble if the parrot is talking and the hour is before 7 or after 20. Return true if we are in trouble.
// should return true console.log(nearHundred(93))
// should return true console.log(nearHundred(90));
// should return false console.log(nearHundred(89));
Q7 PosNeg
Given 2 int values, return true if one is negative and one is positive. Except if the parameter "negative" is true, then return true only if both are negative.
// should return true console.log(posNeg(1, -1, false))
// should return true console.log(posNeg(-1, 1, false));
// should return true console.log(posNeg(-4, -5, true)); Q8 notString
Given a string, return a new string where "not " has been added to the front. However, if the string already begins with "not", return the string unchanged. Note: use .equals() to compare 2 strings.
// should return "not candy" console.log(notString(candy)
// should return "not x" console.log(notString(x));
// should return "not bad" console.log(notString(not bad));
Q9 Missing Char [DIFFICULT]
Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0..str.length()-1 inclusive). Hint: use substring().
// should return "ktten" console.log(missingChar("kitten", 1));
// should return "itten console.log(missingChar("kitten", 0));
// should return kittn console.log(missingChar("kitten", 4));
Q10 frontBack [DIFFICULT}
Given a string, return a new string where the first and last chars have been exchanged.
frontBack("code") "eodc" frontBack("a") "a" frontBack("ab") "ba"
function frontBack(str) { ???? }
// should return "eodc" console.log(frontBack("code"));
// should return "a console.log(frontBack("a"));
// should return ba" console.log(frontBack("ab"));
Q11 front3
Given a string, we'll say that the front is the first 3 chars of the string. So for the string "Code", the first three chars will be "Cod". If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
// should return "JavJavJav" console.log(front3("JavaScript"));
// should return "ChoChoCho console.log(frontBack("Chocolate"));
// should return "abcabcabc" console.log(frontBack("abc"));
Q12 Back Around [DIFFICULT] Given a string, take the last char and return a new string with the last char added at the front and back, so "cat" yields "tcatt". The original string will be length 1 or more.
// should return "tcatt" console.log(backAround("cat"));
// should return "oHelloo console.log(backAround("Hello"));
// should return "aaa" console.log(backAround("a"));
Q13 or35 Return true if the given non-negative number is a multiple of 3 or a multiple of 5. Use the % "modulus" operator -- see an introduction to modulus here: http://www.java2s.com/Tutorial/JavaScript/0040__Operators/Modulus.htm
or35(3) true or35(10) true or35(8) false
function or35(n) { ???? }
// should return true console.log(or35(3));
// should return true console.log(or35(10));
// should return false console.log(or35(8));
Q14 LoneTween We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 2 int values, return true if one or the other is teen, but not both.
Given 2 int values, return whichever value is nearest to the value 10, or return 0 in the event of a tie. Note that Math.abs(n) returns the absolute value of a number.
// should return "true" console.log(stringE("hello"));
// should return "true" console.log(stringE("heelle"));
// should return "false" console.log(backAround("heelele"));
// should return "false" console.log(backAround("hallo"));
Q18 End Up Given a string, return a new string where the last 3 chars are now in upper case. If the string has less than 3 chars, uppercase whatever is there. Note that str.toUpperCase() returns the uppercase version of a string.
// should return "HeLLO" console.log(endUp("Hello"));
// should return "hi thERE" console.log(endUp("hi there"));
// should return "HI" console.log(endUp("hi"));
Q19 everyNth [ULTRA-DIFFICULT]
Given a non-empty string and an int N, return the string made starting with char 0, and then every Nth char of the string. So if N is 3, use char 0, 3, 6, ... and so on. N is 1 or more.