Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions problems/problem1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ var assert = require('assert');

// we need 5 test cases. I provided 1 input
let inputs = [
""
"",
"abc",
"12345",
"hello",
"This is a string"
]

let outputs = [

undefined,
"a",
"1",
"h",
"T"
]

// Make this function return the first letter of the string that is passed to it. If the string does not have a first letter, return undefined
function f(str) {

if (str.length > 0){
return str.charAt(0);
};
}

function runTest(i) {
Expand Down
23 changes: 20 additions & 3 deletions problems/problem10.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ var assert = require('assert');

// we need 5 test cases.
let inputs = [

"hey listen",
"ALL YOUR BASE ARE BELONG TO US",
"the cake is a lie",
"sTAY a wHILE aND lISTEN",
"FOR THE EMPEROR"
]

let outputs = [

"Hey Listen",
"All Your Base Are Belong To Us",
"The Cake Is A Lie",
"Stay A While And Listen",
"For The Emperor"
]

/*
Expand All @@ -17,7 +25,16 @@ f("ALL YOUR BASE ARE BELONG"); // All Your Base Are Belong

*/
function f(str) {

var words = str.split(" ");
var answer = [];

for (var i = 0; i < words.length; i++) {
var capitalizedWord = words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
answer.push(capitalizedWord);
}

return answer.join(" ");

}

function runTest(i) {
Expand Down
21 changes: 18 additions & 3 deletions problems/problem11.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,33 @@ var assert = require('assert');

// we need 5 test cases.
let inputs = [

[1, 2, 3, 4, 5],
[40, 2],
["a", 13],
["x", "y"],
[]
]

let outputs = [

15,
42,
13,
0,
0
]

/*
Make this function return the sum of all the numbers in the input array. If any element in the array is not a number, skip it. If the array is empty, return zero.
*/
function f(arr) {

var answer = 0;
for (var i = 0; i < arr.length; i++) {
if (typeof arr[i] == 'number') {
answer = answer + arr[i];
}
}

return answer;
}

function runTest(i) {
Expand Down
46 changes: 43 additions & 3 deletions problems/problem12.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ var assert = require('assert');

// we need 5 test cases.
let inputs = [

[[0,1,2,3], [1,3,4,5]],
[[1,2,3], [1,2,3]],
[[1,2,42], [1,2,13]],
[[1], [1]],
["a"]
]

let outputs = [

[0,2,4,5],
[],
[42,13],
[],
undefined
]

/*
Expand All @@ -19,8 +27,40 @@ uniqueElements([0,1,2,3], [1,3,4,5]); // [0,4,5]
uniqueElements([1,2,3], [1,2,3]); // []
uniqueElements(2,3); // undefined, not arrays
*/
function f(arr1, arr2) {

function isPartOfArray(x, list){
for (var i = 0; i < list.length; i++) {
if (x == list[i]) {
return true;
}
}
return false;
}

function f(arr) {
var firstArray = arr[0];
var secondArray = arr[1];
var answer = [];

if (Array.isArray(arr) && Array.isArray(firstArray) && Array.isArray(secondArray)) {
for (var i = 0; i < firstArray.length; i++) {
if (!isPartOfArray(firstArray[i], secondArray)
&& !isPartOfArray(firstArray[i], answer)) {
answer.push(firstArray[i]);
}
}

for (var i = 0; i < secondArray.length; i++) {
if (!isPartOfArray(secondArray[i], firstArray)
&& !isPartOfArray(secondArray[i], answer)) {
answer.push(secondArray[i]);
}
}

return answer;
}


}

function runTest(i) {
Expand Down
23 changes: 20 additions & 3 deletions problems/problem13.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ var assert = require('assert');

// we need 5 test cases.
let inputs = [

"kayak",
"radar",
"notakayak",
"hello",
"worldlrow"
]

let outputs = [

true,
true,
false,
false,
true
]

/*
Expand All @@ -16,7 +24,16 @@ RADAR -> Yes
JAVASCRIPT -> No
*/
function f(str) {

var arr = str.split("");
var rev = arr.reverse("");
var ans = rev.join("");

if (str == ans) {
return true;
}
else {
return false
}
}

function runTest(i) {
Expand Down
35 changes: 32 additions & 3 deletions problems/problem14.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ var assert = require('assert');

// we need 5 test cases.
let inputs = [

"Lorem ipsumos dolor sit amet consectetur adipisicing elit. Magni quisquam",
"Handshake backing conversion android traction angel investor analytics low hanging fruit ecosystem",
"a b c d e f g h i j k l m n o p q r s t u v w x y z",
"0123456789012345678901234567890123456789",
"This string is too short."
]

/*To make sure I had the right position of \n in the expected output*/
/*var z = "Handshake backing conversion android traction angel investor analytics low hanging fruit ecosystem";
console.log("39eme = " + z.charAt(38));
console.log("40eme = " + z.charAt(39));
console.log("41eme = " + z.charAt(40));
console.log("79eme = " + z.charAt(78));
console.log("80eme = " + z.charAt(79));
console.log("81eme = " + z.charAt(80)); */

let outputs = [

"Lorem ipsumos dolor sit amet consectetur\nadipisicing elit. Magni quisquam",
"Handshake backing conversion android tra\nction angel investor analytics low hangi\nng fruit ecosystem",
"a b c d e f g h i j k l m n o p q r s t \nu v w x y z",
"0123456789012345678901234567890123456789",
"This string is too short."
]

/*
Expand All @@ -31,7 +48,19 @@ Lorem ipsumos dolor sit amet consectetur
even though there is a space before the a in adipisicing
*/
function f(str) {

var myString = str;
ans = [];
while (myString.length > 40) {
ans.push(myString.slice(0,40) + "\n");
myString = myString.slice(40);

if (myString.charAt(0) == " ") {
myString = myString.slice(1);
}
}

ans.push(myString);
return ans.join("");
}

function runTest(i) {
Expand Down
18 changes: 14 additions & 4 deletions problems/problem2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@ var assert = require('assert');

// we need 5 test cases.
let inputs = [

"",
"abc",
"12345",
"hello",
"This is a string"
]

let outputs = [

let outputs = [
undefined,
"c",
"5",
"o",
"g"
]

// Make this function return the last letter of the string that is passed to it. If the string does not have a last letter, return undefined
function f(str) {

if (str.length > 0) {
return str.charAt(str.length - 1);
}
}

function runTest(i) {
Expand Down
21 changes: 17 additions & 4 deletions problems/problem3.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ var assert = require('assert');
// we need 7 test cases. I've provided 2.
let inputs = [
[2, 4],
[-3, 3]
[-3, 3],
["a", 8],
["x", "z"],
[21, 21],
[3, 39],
[8, 5]
]

let outputs = [
6,
0
0,
undefined,
undefined,
42,
42,
13
]

/*
Make this function return the sum of the two numbers that are passed to it. If one of the numbers is not passed, or if anything other than numbers are passed, return undefined.
*/
function f(x, y) {

function f(x) {
console.log(x)
if (!isNaN(x[0]) & !isNaN(x[1])) {
return x[0] + x[1];
}
}

function runTest(i) {
Expand Down
30 changes: 21 additions & 9 deletions problems/problem4.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@ var assert = require('assert');
// we need 8 test cases. I've provided the first 2
let inputs = [
["hello", 4],
["", 2]
["", 2],
["Hi", 0],
["Lorem Ipsum", 3],
["1orem", 0],
["Lorem lipsum", 6],
["20346127", 1],
["World", 0]
]

let outputs = [
"o",
undefined
undefined,
"H",
"e",
"1",
"l",
"0",
"W"
]

/*
Make this function return the letter at the specified position in the string. If no such letter exists, it should return undefined.

For example:
f("hello", 1); // e
f("", 4); // undefined
f("abc", 0); // a

f(["hello", 1]); // e
f(["", 4]); // undefined
f(["abc", 0]); // a
*/
function f(str, index) {

if (str.length > 0 && str.length > index ){
return str.charAt(index);
};
}

function runTest(i) {
Expand All @@ -38,4 +50,4 @@ runTest(3);
runTest(4);
runTest(5);
runTest(6);
runTest(7);
runTest(7);
Loading