Skip to content
This repository was archived by the owner on Oct 26, 2020. It is now read-only.

Js week3 martin #90

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
week 2 homework
  • Loading branch information
MartinBoylan committed Jun 26, 2020
commit efef2521b458dab6fe65ca42528768f7e1a6688e
Binary file modified .DS_Store
Binary file not shown.
Binary file added week-1/.DS_Store
Binary file not shown.
Binary file added week-1/1-exercises/.DS_Store
Binary file not shown.
Binary file added week-2/.DS_Store
Binary file not shown.
Binary file added week-2/1-exercises/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions week-2/1-exercises/B-boolean-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/

var codeYourFutureIsGreat = true;
var mozafarIsCool = false;
var calculationCorrect = true;
var moreThan10Students = false;

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
6 changes: 3 additions & 3 deletions week-2/1-exercises/C-comparison-operators/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

var studentCount = 16;
var mentorCount = 9;
var moreStudentsThanMentors; // finish this statement
var moreStudentsThanMentors = true; // finish this statement

var roomMaxCapacity = 25;
var enoughSpaceInRoom; // finish this statement
var enoughSpaceInRoom = false; // finish this statement

var personA = "Daniel";
var personB = "Irina";
var sameName; // finish this statement
var sameName = false; // finish this statement

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions week-2/1-exercises/D-predicates/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

// Finish the predicate function to test if the passed number is negative (less than zero)
function isNegative(number) {

return number < 0;
}

// Finish the predicate function to test if the passed number is between 0 and 10
function isBetweenZeroAnd10(number) {

return number > 0 && number < 10;
}

/*
Expand Down
6 changes: 5 additions & 1 deletion week-2/1-exercises/E-conditionals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

var name = "Daniel";
var danielsRole = "mentor";

if (danielsRole === "mentor") {
console.log("Hi, I'm Daniel, I'm a mentor")
} else if (danielsRole === "student") {
console.log("Hi, I'm Daniel, I'm a student")
}
/*
EXPECTED RESULT
---------------
Expand Down
21 changes: 19 additions & 2 deletions week-2/1-exercises/F-logical-operators/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,33 @@ var cssLevel = 4;

// Finish the statement to check whether HTML, CSS knowledge are above 5
// (hint: use the comparison operator from before)
var htmlLevelAbove5;
var cssLevelAbove5;

if (htmlLevel > 5) {
var htmlLevelAbove5 = true;
} else {false
}
if (cssLevel > 5) {
var cssLevelAbove5 = true;
} else {var cssLevelAbove5 = false;}


// Finish the next two statement
// Use the previous variables and logical operators
// Do not "hardcode" the answers
var cssAndHtmlAbove5;
var cssOrHtmlAbove5;
if (htmlLevel > 5 && cssLevel > 5) {
cssAndHtmlAbove5 = true;
} else {cssAndHtmlAbove5 = false}

if (htmlLevel > 5 || cssLevel > 5) {
cssOrHtmlAbove5 = true;
} else {
cssOrHtmlAbove5 = false;
}

/*

DO NOT EDIT BELOW THIS LINE
--------------------------- */

Expand Down
17 changes: 16 additions & 1 deletion week-2/1-exercises/F-logical-operators/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@
Update the code so that you get the expected result.
*/

function isNegative() {}
function isNegative(number) {
return number < 0;
}

function isBetween5and10 (number) {
return number >= 5 && number <= 10;
}

function isShortName (name) {
let stringLength = name.length;
return stringLength < 8;

}

function startsWithD (name) {
return name[0] === "D";
}
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
4 changes: 3 additions & 1 deletion week-2/1-exercises/G-conditionals-2/exercise-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/

function negativeOrPositive(number) {

if (number < 0) {
return "negative";
} else {return "positive"}
}

/*
Expand Down
4 changes: 3 additions & 1 deletion week-2/1-exercises/G-conditionals-2/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/

function studentPassed(grade) {

if (grade < 50) {
return "failed";
} else {return "passed"}
}

/*
Expand Down
11 changes: 9 additions & 2 deletions week-2/1-exercises/G-conditionals-2/exercise-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@
*/

function calculateGrade(mark) {

}
if (mark >= 80) {
return "A";
} else if (mark < 80 && mark > 60) {
return "B"
} else if (mark < 60 && mark >= 50){
return "C"
}
else {return "F"}
}

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
3 changes: 3 additions & 0 deletions week-2/1-exercises/G-conditionals-2/exercise-4.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/

function containsCode(sentence) {
if (sentence.includes("code") === true) {
return true
} else {return false}

}

Expand Down
4 changes: 2 additions & 2 deletions week-2/1-exercises/H-array-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Declare some variables assigned to arrays of values
*/

var numbers = []; // add numbers from 1 to 10 into this array
var mentors; // Create an array with the names of the mentors: Daniel, Irina and Rares
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // add numbers from 1 to 10 into this array
var mentors = ["Daniel", "Irina", "Rares"]; // Create an array with the names of the mentors: Daniel, Irina and Rares

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
5 changes: 3 additions & 2 deletions week-2/1-exercises/I-array-properties/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
*/

function isEmpty(arr) {
return; // complete this statement
if (arr.length > 0)
{return false;} // complete this statement
else {return true}
}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
4 changes: 2 additions & 2 deletions week-2/1-exercises/J-array-get-set/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
*/

function first(arr) {
return; // complete this statement
return arr[0]; // complete this statement
}

function last(arr) {
return; // complete this statement
return arr[arr.length - 1]; // complete this statement
}

/*
Expand Down
3 changes: 2 additions & 1 deletion week-2/1-exercises/J-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

var numbers = [1, 2, 3]; // Don't change this array literal declaration

numbers.push(4);
numbers[0] = 1;
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
90 changes: 55 additions & 35 deletions week-2/2-mandatory/1-fix-functions.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,110 @@
// The below functions are syntactically correct but not outputting the right results.
// Look at the tests and see how you can fix them.

function mood() {
let isHappy = true;

if (isHappy) {
function mood(isHappy) {
if (isHappy === true) {
return "I am happy";
} else {
return "I am not happy";
}
}

function greaterThan10() {
let num = 10;
let isBigEnough;

if (isBigEnough) {
return "num is greater than or equal to 10";
function greaterThan10(num) {
let isBigEnough = 10;
if (num > isBigEnough) {
return "num is greater than 10";
} else {
return "num is not big enough";
}
}

function sortArray() {
let letters = ["a", "n", "c", "e", "z", "f"];
let sortedLetters;
function sortArray(letters) {

let sortedLetters = letters.sort();

return sortedLetters;
}

function first5() {
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
let sliced;

numbers.length = 5;
let sliced = numbers;
return sliced;
}

function get3rdIndex(arr) {
let index = 3;
let element;
let index = arr[3];
let element = index;

return element;
}

/* ======= TESTS - DO NOT MODIFY ===== */

function test(test_name, expr) {
const util = require("util");
function test(test_name, actual, expected) {
let status;
if (expr) {
let isEqual;
if (Array.isArray(expected)) {
isEqual = arraysEqual(actual, expected);
} else {
isEqual = actual === expected;
}
if (isEqual) {
status = "PASSED";
} else {
status = "FAILED";
status = `FAILED: expected: ${util.inspect(
expected
)} but your function returned: ${util.inspect(actual)}`;
}

console.log(`${test_name}: ${status}`);
}

function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;

for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}

return true;
}

test("mood function works", mood() === "I am not happy");
test("mood function works for true", mood(true), "I am happy");
test("mood function works for false", mood(false), "I am not happy");
test(
"greaterThanTen function works",
greaterThan10() === "num is greater than or equal to 10"
"greaterThanTen function works for 11",
greaterThan10(11),
"num is greater than 10"
);
test(
"sortArray function works",
arraysEqual(sortArray(), ["a", "c", "e", "f", "n", "z"])
"greaterThanTen function works for 10",
greaterThan10(10),
"num is not big enough"
);
test("first5 function works", arraysEqual(first5(), [1, 2, 3, 4, 5]));

test(
"greaterThanTen function works for 9",
greaterThan10(9),
"num is not big enough"
);
test("sortArray function works", sortArray(["a", "n", "c", "e", "z", "f"]), [
"a",
"c",
"e",
"f",
"n",
"z",
]);
let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
test("first5 function works", first5(numbers), [1, 2, 3, 4, 5]);
if (!arraysEqual(numbers, [1, 2, 3, 4, 5, 6, 7, 8])) {
console.log("PROBLEM: first5 changed its input array - it shouldn't!");
}
test(
"get3rdIndex function works - case 1",
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]) ===
"strawberry"
get3rdIndex(["fruit", "banana", "apple", "strawberry", "raspberry"]),
"strawberry"
);
test(
"get3rdIndex function works - case 2",
get3rdIndex([11, 37, 62, 18, 19, 3, 30]) === 18
get3rdIndex([11, 37, 62, 18, 19, 3, 30]),
18
);
Loading