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

Ebenezer wk3 #92

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions week-1/1-exercises/J-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function halve(number) {
// complete the function here
return number / 2;
}

var result = halve(12);
Expand Down
10 changes: 5 additions & 5 deletions week-1/3-extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
the final result to the variable goodCode
*/

function add() {

function add(num1, num2) {
return num1 + num2;
}

function multiply() {

function multiply(num1, num3) {
return num1 * num2;
}

function format() {
function format(num) {

}

Expand Down
5 changes: 4 additions & 1 deletion week-2/1-exercises/B-boolean-literals/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Add the required variables with the correct boolean values assigned.
*/

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

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
15 changes: 5 additions & 10 deletions week-2/1-exercises/D-predicates/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,17 @@
*/

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

}
function isNegative(number) {}

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

}
function isBetweenZeroAnd10(number) {}

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
var number = 5;
var numberNegative = isNegative(number);
var numberBetweenZeroAnd10 = isBetweenZeroAnd10(number);
const number = 5;
const numberNegative = isNegative(number);
const numberBetweenZeroAnd10 = isBetweenZeroAnd10(number);
console.log("The number in test is " + number);
console.log("Is the number negative? " + numberNegative);
console.log("Is the number between 0 and 10? " + numberBetweenZeroAnd10);
Expand All @@ -32,4 +28,3 @@ console.log("Is the number between 0 and 10? " + numberBetweenZeroAnd10);
Is the number negative? false
Is the number between 0 and 10? true
*/

27 changes: 12 additions & 15 deletions week-2/2-mandatory/1-fix-functions.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
// 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;

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

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

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

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

sortedLetters = letters.sort();
return sortedLetters;
}

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

sliced = numbers.slice(0, 5);
return sliced;
}

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

element = arr[index];
return element;
}

Expand Down
37 changes: 30 additions & 7 deletions week-2/2-mandatory/2-function-creation.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Write a function that:
- removes any forward slashes (/) in the strings
- makes the string all lowercase
*/
function tidyUpString(strArr) {}
function tidyUpString(strArr) {

}

/*
Complete the function to check if the variable `num` satisfies the following requirements:
Expand All @@ -15,7 +17,16 @@ Complete the function to check if the variable `num` satisfies the following req
Tip: use logical operators
*/

function validate(num) {}
function validate(num) {
if ((typeof num === 'number') && (num % 2 === 0) && (num <= 100))
{
return true;
}
else
{
return false;
}
}

/*
Write a function that removes an element from an array
Expand All @@ -26,7 +37,8 @@ The function must:
*/

function remove(arr, index) {
return; // complete this statement
const removed = arr.splice(index, 1);
return arr;
}

/*
Expand All @@ -38,6 +50,17 @@ Write a function that:
*/

function formatPercentage(arr) {
const percentage = arr.map(function(n) {
if (n < 100)
{
console.log(`${Number(n.toFixed(2))}%`);
}
else
{
console.log("100%");
}

})

}

Expand Down Expand Up @@ -72,7 +95,7 @@ test(
"daniel",
"irina",
"gordon",
"ashleigh"
"ashleigh",
])
);
test(
Expand Down Expand Up @@ -101,7 +124,7 @@ test(
"c",
"d",
"e",
"f"
"f",
])
);

Expand All @@ -111,6 +134,6 @@ test(
"23%",
"18%",
"100%",
"0.37%"
"0.37%",
])
);
);
17 changes: 9 additions & 8 deletions week-2/2-mandatory/3-playing-computer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

Answer the following questions:

1. This program throws an error. Why? (If you can't find it, try executing it).
2. Remove the line that throws the error.
3. What is printed to the console?
4. How many times is "f1" called?
5. How many times is "f2" called?
6. What value does the "a" parameter take in the first "f1" call?
7. What is the value of the "a" outer variable when "f1" is called for the first time?
1. This program throws an error. Why? (If you can't find it, try executing it). Ans: ReferenceError: b is not defined.
Trying to console out b which is not a defined variable outside the function block.
2. Remove the line that throws the error. Ans: console.log(b) is removed.
3. What is printed to the console? Ans: console prints out 2 6 4 9 6 13 8
4. How many times is "f1" called? Ans: f1 is the else which is 9 and 13 ( twice)
5. How many times is "f2" called? Ans: f2 even numbers are 2 , 6, 4, 6, 8 (5 times)
6. What value does the "a" parameter take in the first "f1" call? Ans: 1st parameter call a = 6
7. What is the value of the "a" outer variable when "f1" is called for the first time? Ans: a + 1 = 7
*/

let x = 2;
Expand All @@ -28,7 +29,7 @@ const f2 = function(a, b) {

console.log(x);
console.log(a);
console.log(b);
// console.log(b);

for (let i = 0; i < 5; ++i) {
a = a + 1;
Expand Down
10 changes: 9 additions & 1 deletion week-2/2-mandatory/4-sorting-algorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ You don't have to worry about making this algorithm work fast! The idea is to ge
"think" like a computer and practice your knowledge of basic JavaScript.
*/

function sortAges(arr) {}
function sortAges(arr) {
const sorted = arr.filter(function(value) {
return typeof value === "number"
});
const ascendSort = sorted.sort(function(a, b) {
return a - b;
})
return ascendSort;
}

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

Expand Down
4 changes: 4 additions & 0 deletions week-3/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

var names = ["Rakesh", "Antonio", "Alexandra", "Andronicus", "Annam", "Mikey", "Anastasia", "Karim", "Ahmed"];

var findName = names.find(name => {
return name.charAt(0) || name.length > 7;
})

var longNameThatStartsWithA = findLongNameThatStartsWithA(names);

console.log(longNameThatStartsWithA);
Expand Down
11 changes: 9 additions & 2 deletions week-3/1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- Do not edit any of the existing code
*/

var pairsByIndex = [[0, 3], [1, 2], [2, 1], null, [3, 0]];
var pairsByIndex = [[0, 3], [1, 2], [2, 1],null,[3, 0]];

// If there is a null value in the array exit the program with the error code
// https://nodejs.org/api/process.html#process_process_exit_code
Expand All @@ -18,7 +18,14 @@ var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];
var pairs = pairsByIndex.map(function(indexes) {
var student = students[indexes[0]];
var mentor = mentors[indexes[1]];
return [student, mentor];
const nullCheck = (element) => element === null;
if(pairsByIndex.some(nullCheck) === true) {
process.exit(1)
}
else
{return [student, mentor];}

});


console.log(pairs);
43 changes: 31 additions & 12 deletions week-3/2-mandatory/1-oxygen-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,51 @@ To be safe, they need to land on the first unamed planet that has Oxygen levels
Write a function that finds the oxygen level of the first safe planet - Oxygen between 19.5% and 23.5%
*/

function safeLevels() {

function safeLevels(arr) {
const convert = arr.map(function(level) {
return (parseFloat(level))
})
console.log(convert);
const levels = convert.filter(function(n) {
return n > 19.5 && n < 23.5;
})
return `${levels[0]}%`
}


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

const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"]
const oxygenLevels2 = ["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"]
const oxygenLevels1 = ["24.2%", "11.3%", "19.9%", "23.1%", "29.3%", "20.2%"];
const oxygenLevels2 = ["30.8%", "23.5%", "18.8%", "19.5%", "20.2%", "31.6%"];
const oxygenLevels3 = ["200%", "21.1%"];

const util = require('util');

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

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

test(
"safeLevels function works - case 2",
safeLevels(oxygenLevels1) === "19.9%"
"safeLevels function works - case 1",
safeLevels(oxygenLevels1),
"19.9%"
);

test(
"safeLevels function works - case 2",
safeLevels(oxygenLevels2) === "20.2%"
safeLevels(oxygenLevels2),
"20.2%"
);

test(
"safeLevels function works - case 3",
safeLevels(oxygenLevels3),
"21.1%"
);
Loading