From dc5f6495eca886c32324e7532cb29a4b3a33b989 Mon Sep 17 00:00:00 2001 From: Walter Tanaka Chifamba <63348127+chifambawalter@users.noreply.github.com> Date: Thu, 14 Jul 2022 00:26:09 +0200 Subject: [PATCH 1/2] za2-chifambawalter-javascript-core-1-coursework-week-1 --- .vscode/settings.json | 5 ++ exercises/B-hello-world/exercise.js | 2 + exercises/C-variables/exercise.js | 4 +- exercises/D-strings/exercise.js | 3 +- exercises/E-strings-concatenation/exercise.js | 4 +- exercises/F-strings-methods/exercise.js | 6 ++- exercises/F-strings-methods/exercise2.js | 4 +- exercises/G-numbers/exercise.js | 4 ++ exercises/I-floats/exercise.js | 5 ++ exercises/J-functions/exercise.js | 2 + exercises/J-functions/exercise2.js | 1 + exercises/K-functions-parameters/exercise.js | 3 +- exercises/K-functions-parameters/exercise2.js | 4 +- exercises/K-functions-parameters/exercise3.js | 4 +- exercises/K-functions-parameters/exercise4.js | 6 ++- exercises/K-functions-parameters/exercise5.js | 4 +- exercises/L-functions-nested/exercise.js | 25 ++++++++++ mandatory/1-syntax-errors.js | 49 ++++++++++--------- 18 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..9072ab25c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "editor.formatOnSave": true, + "editor.formatOnType": true, + "editor.formatOnPaste": true +} \ No newline at end of file diff --git a/exercises/B-hello-world/exercise.js b/exercises/B-hello-world/exercise.js index b179ee953..37420ecf7 100644 --- a/exercises/B-hello-world/exercise.js +++ b/exercises/B-hello-world/exercise.js @@ -1 +1,3 @@ console.log("Hello world"); +console.log("Hello world"); +console.log("Hello world"); diff --git a/exercises/C-variables/exercise.js b/exercises/C-variables/exercise.js index a6bbb9786..a2972ed7b 100644 --- a/exercises/C-variables/exercise.js +++ b/exercises/C-variables/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `greeting` - +var greeting = "Hello Walter"; +console.log(greeting); +console.log(greeting); console.log(greeting); diff --git a/exercises/D-strings/exercise.js b/exercises/D-strings/exercise.js index 2cffa6a81..369440c3f 100644 --- a/exercises/D-strings/exercise.js +++ b/exercises/D-strings/exercise.js @@ -1,3 +1,4 @@ // Start by creating a variable `message` - +let message = "Hello Walter"; console.log(message); +console.log(typeof "hello walter"); diff --git a/exercises/E-strings-concatenation/exercise.js b/exercises/E-strings-concatenation/exercise.js index 2cffa6a81..5ec3b11ec 100644 --- a/exercises/E-strings-concatenation/exercise.js +++ b/exercises/E-strings-concatenation/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - +let greeting = "Hello my name is"; +let _name = "Walter"; +message = greeting + "" + _name; console.log(message); diff --git a/exercises/F-strings-methods/exercise.js b/exercises/F-strings-methods/exercise.js index 2cffa6a81..74c80961a 100644 --- a/exercises/F-strings-methods/exercise.js +++ b/exercises/F-strings-methods/exercise.js @@ -1,3 +1,5 @@ // Start by creating a variable `message` - -console.log(message); +var user_name = "Walter"; +var characters = user_name.length; +var output = "My name is "+user_name+" and is "+characters+" characters long." +console.log(output); diff --git a/exercises/F-strings-methods/exercise2.js b/exercises/F-strings-methods/exercise2.js index b4b46943d..2b6f0fc43 100644 --- a/exercises/F-strings-methods/exercise2.js +++ b/exercises/F-strings-methods/exercise2.js @@ -1,3 +1,3 @@ -const name = " Daniel "; - +const myName = " Daniel "; +let message = myName.trim console.log(message); diff --git a/exercises/G-numbers/exercise.js b/exercises/G-numbers/exercise.js index 49e7bc00b..39e264bdd 100644 --- a/exercises/G-numbers/exercise.js +++ b/exercises/G-numbers/exercise.js @@ -1 +1,5 @@ // Start by creating a variables `numberOfStudents` and `numberOfMentors` +let numberOfStudents=15; +let numberOfMentors = 8; +let result = numberOfMentors + numberOfStudents +console.log("The total number of students and mentors is" + (result)); \ No newline at end of file diff --git a/exercises/I-floats/exercise.js b/exercises/I-floats/exercise.js index a5bbcd852..2e263f1e6 100644 --- a/exercises/I-floats/exercise.js +++ b/exercises/I-floats/exercise.js @@ -1,2 +1,7 @@ var numberOfStudents = 15; var numberOfMentors = 8; +let total = numberOfStudents + numberOfMentors; +let student_percentage = numberOfStudents / total * 100; +let mentor_percentage = numberOfMentors / total * 100 +console.log("Percentage students: "+Math.round(student_percentage)+"%"); +console.log("Mentor students: "+Math.round(mentor_percentage)+"%"); \ No newline at end of file diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index 0ae5850e5..f3e1d8ef4 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -1,7 +1,9 @@ function halve(number) { // complete the function here + return number * 0.5; } var result = halve(12); console.log(result); +console.log(halve(9)); diff --git a/exercises/J-functions/exercise2.js b/exercises/J-functions/exercise2.js index 82ef5e780..468d45596 100644 --- a/exercises/J-functions/exercise2.js +++ b/exercises/J-functions/exercise2.js @@ -1,5 +1,6 @@ function triple(number) { // complete function here + return number * 3; } var result = triple(12); diff --git a/exercises/K-functions-parameters/exercise.js b/exercises/K-functions-parameters/exercise.js index 8d5db5e69..a9754224e 100644 --- a/exercises/K-functions-parameters/exercise.js +++ b/exercises/K-functions-parameters/exercise.js @@ -1,6 +1,7 @@ // Complete the function so that it takes input parameters -function multiply() { +function multiply(a,b) { // Calculate the result of the function and return it + return a * b; } // Assign the result of calling the function the variable `result` diff --git a/exercises/K-functions-parameters/exercise2.js b/exercises/K-functions-parameters/exercise2.js index db7a8904b..6839e8684 100644 --- a/exercises/K-functions-parameters/exercise2.js +++ b/exercises/K-functions-parameters/exercise2.js @@ -1,5 +1,7 @@ // Declare your function first - +function divide(a, b) { + return a / b; +} var result = divide(3, 4); console.log(result); diff --git a/exercises/K-functions-parameters/exercise3.js b/exercises/K-functions-parameters/exercise3.js index 537e9f4ec..9ba16787a 100644 --- a/exercises/K-functions-parameters/exercise3.js +++ b/exercises/K-functions-parameters/exercise3.js @@ -1,5 +1,7 @@ // Write your function here - +function createGreeting(myName) { + return "hello, my name is" + myName; +} var greeting = createGreeting("Daniel"); console.log(greeting); diff --git a/exercises/K-functions-parameters/exercise4.js b/exercises/K-functions-parameters/exercise4.js index 7ab44589e..ed0c209fb 100644 --- a/exercises/K-functions-parameters/exercise4.js +++ b/exercises/K-functions-parameters/exercise4.js @@ -1,5 +1,7 @@ // Declare your function first - +function add_two_numbers(a, b) { + return a + b; + } // Call the function and assign to a variable `sum` - +let sum = add_two_numbers(124, 13); console.log(sum); diff --git a/exercises/K-functions-parameters/exercise5.js b/exercises/K-functions-parameters/exercise5.js index 7c5bcd605..6c83bba93 100644 --- a/exercises/K-functions-parameters/exercise5.js +++ b/exercises/K-functions-parameters/exercise5.js @@ -1,5 +1,7 @@ // Declare your function here - +function createLongGreeting(myName, age) { + return "Hello, my name is " + myName + " and I'm " + age + " years old."; +} const greeting = createLongGreeting("Daniel", 30); console.log(greeting); diff --git a/exercises/L-functions-nested/exercise.js b/exercises/L-functions-nested/exercise.js index a5d377442..9d59feead 100644 --- a/exercises/L-functions-nested/exercise.js +++ b/exercises/L-functions-nested/exercise.js @@ -3,3 +3,28 @@ var mentor2 = "Irina"; var mentor3 = "Mimi"; var mentor4 = "Rob"; var mentor5 = "Yohannes"; + +/***************Exercise 1***********************/ + +function percentageCalculator(people,num, total){ + let percent=(num/total)*100; +function message(){ + return "Percentage "+people+": "+percent+"%"; +} +return message(); +} +console.log(percentageCalculator("Mentors", 8, 16)); + +/***************Exercise 2***********************/ +function greetings(name){ + var up=name.toUpperCase(); +function toUppercase(){ + return up; +} +return "HELLO "+up; +} +console.log(greetings(mentor1)); +console.log(greetings(mentor2)); +console.log(greetings(mentor3)); +console.log(greetings(mentor4)); +console.log(greetings(mentor5)); diff --git a/mandatory/1-syntax-errors.js b/mandatory/1-syntax-errors.js index a10cc9ac2..a898b732c 100644 --- a/mandatory/1-syntax-errors.js +++ b/mandatory/1-syntax-errors.js @@ -1,40 +1,41 @@ // There are syntax errors in this code - can you fix it to pass the tests? -function addNumbers(a b c) { - return a + b + c; +function addNumbers(a ,b, c) { + return a + b + c; } - -function introduceMe(name, age) - return "Hello, my name is " + name "and I am " age + "years old"; - +addNumbers(3, 4 ,6) +function introduceMe(name, age){ +return "Hello, my name is " + name +" and I am "+ age + " years old"; +} +introduceMe("Sonjide ", 27) function getTotal(a, b) { - total = a ++ b; + total = a + b; - return "The total is total"; + return "The total is "+total; } +getTotal(23,5) /* =================================================== ======= TESTS - DO NOT MODIFY BELOW THIS LINE ===== - There are some Tests in this file that will help you work out if your code is working. - -To run the tests for just this one file, type `npm test -- --testPathPattern 1-syntax-errors` into your terminal -(Reminder: You must have run `npm install` one time before this will work!) - +To run these tests type `node 1-syntax-errors.js` into your terminal =================================================== */ -test("addNumbers adds numbers correctly", () => { - expect(addNumbers(3, 4, 6)).toEqual(13); -}); +const util = require('util'); -test("introduceMe function returns the correct string", () => { - expect(introduceMe("Sonjide", 27)).toEqual( - "Hello, my name is Sonjide and I am 27 years old" - ); -}); +function test(test_name, actual, expected) { + let status; + if (actual === expected) { + status = "PASSED"; + } else { + status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`; + } + + console.log(`${test_name}: ${status}`); +} -test("getTotal returns a string describing the total", () => { - expect(getTotal(23, 5)).toEqual("The total is 28"); -}); +test("fixed addNumbers function - case 1", addNumbers(3, 4, 6), 13); +test("fixed introduceMe function", introduceMe("Sonjide", 27), "Hello, my name is Sonjide and I am 27 years old"); +test("fixed getTotal function", getTotal(23, 5), "The total is 28"); \ No newline at end of file From a38cfd4cba22daf920b2c45fbfa2e7233b8f5d82 Mon Sep 17 00:00:00 2001 From: Walter Tanaka Chifamba <63348127+chifambawalter@users.noreply.github.com> Date: Fri, 15 Jul 2022 13:10:27 +0200 Subject: [PATCH 2/2] Update exercise.js --- exercises/J-functions/exercise.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/J-functions/exercise.js b/exercises/J-functions/exercise.js index f3e1d8ef4..9f80d6b8c 100644 --- a/exercises/J-functions/exercise.js +++ b/exercises/J-functions/exercise.js @@ -3,7 +3,7 @@ function halve(number) { return number * 0.5; } -var result = halve(12); +let result = halve(12); console.log(result); console.log(halve(9));