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

zubeda-week3(extra+mandatory)home-work #73

Open
wants to merge 20 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
6 changes: 2 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Your Details

Your Name:
Your City:
Your Name:zubeda
Your City:birminghan
Your Slack Name:

# Homework Details

Module:
Week:
22 changes: 18 additions & 4 deletions week-1/2-mandatory/1-syntax-errors.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
// There are syntax errors in this code - can you fix it to pass the tests?

function addNumbers(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";
function introduceMe(name, age){
return "Hello, my name is " + name + " and I am " + age + " years old";
}

function getRemainder(a, b) {


function getTotal(a, b) {
total = a ++ b;


// Use string interpolation here
return "The total is %{total}"
let Reminder=a%b;
return `The remainder is ${a%b}`;
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -24,6 +30,10 @@ const util = require('util');

function test(test_name, actual, expected) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
if (actual === expected) {
status = "PASSED";
} else {
Expand All @@ -33,6 +43,10 @@ function test(test_name, actual, expected) {
console.log(`${test_name}: ${status}`);
}

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 getRemainder function", getRemainder(23,5) === "The remainder is 3");
=======
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");
22 changes: 18 additions & 4 deletions week-1/2-mandatory/2-logic-error.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// The syntax for this function is valid but it has an error, find it and fix it.

function trimWord(word) {
return wordtrim();
return word.trim();
}

function getWordLength(word) {
return "word".length()
return word.length;
}

function multiply(a, b, c) {
a * b * c;
return;

return a * b * c;
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -19,6 +19,20 @@ There are some Tests in this file that will help you work out if your code is wo
To run these tests type `node 2-logic-error` into your terminal
*/

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}

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

test("fixed trimWord function", trimWord(" CodeYourFuture ") === "CodeYourFuture");
test("fixed wordLength function", getWordLength("A wild sentence appeared!") === 25);
test("fixed multiply function", multiply(2,3,6) === 36);
const util = require('util');

function test(test_name, actual, expected) {
Expand Down
9 changes: 7 additions & 2 deletions week-1/2-mandatory/3-function-output.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Add comments to explain what this function does. You're meant to use Google!
function getNumber() {
function getNumber() {//MAth.random()*10 return random numbers between 1 and 10
return Math.random() * 10;
}

// Add comments to explain what this function does. You're meant to use Google!
function s(w1, w2) {
function s(w1, w2) {//w1.concat(w2) return a new string containing the text of joined string
return w1.concat(w2);
}

function concatenate(firstWord, secondWord, thirdWord) {

console.log(firstWord.concat(" ").concat(secondWord).concat(" ").concat(thirdWord));
return firstWord.concat(" ").concat(secondWord).concat(" ").concat(thirdWord);
// Write the body of this function to concatenate three words together.
// Look at the test case below to understand what this function is expected to return.
}
Expand Down Expand Up @@ -36,11 +39,13 @@ test(
concatenate('code', 'your', 'future'),
"code your future"
);

test(
"concatenate function - case 2 works",
concatenate('I', 'like', 'pizza'),
"I like pizza"
);

test(
"concatenate function - case 3 works",
concatenate('I', 'am', 13),
Expand Down
12 changes: 11 additions & 1 deletion week-1/2-mandatory/4-tax.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
Sales tax is 20% of the price of the product
*/

function calculateSalesTax() {}
function calculateSalesTax(priceOfProduct) {

let salesTax=(20/100*priceOfProduct)+priceOfProduct;
return salesTax;
}

/*
CURRENCY FORMATTING
Expand All @@ -17,6 +21,12 @@ function calculateSalesTax() {}
Remember that the prices must include the sales tax (hint: you already wrote a function for this!)
*/

function formatCurrency(a) {
var price=calculateSalesTax(a);

return `£${price.toFixed(2)}`;
}
=======
function addTaxAndFormatCurrency() {}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
12 changes: 10 additions & 2 deletions week-1/3-extra/1-currency-conversion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
Write a function that converts a price to USD (exchange rate is 1.4 $ to £)
*/

function convertToUSD() {}
function convertToUSD(price) {
let convertToUs=1.4*price;;
return convertToUs;
}

/*
CURRENCY FORMATTING
Expand All @@ -15,7 +18,12 @@ function convertToUSD() {}
They have also decided that they should add a 1% fee to all foreign transactions, which means you only convert 99% of the £ to BRL.
*/

function convertToBRL() {}
function convertToBRL(price) {
let percentOfPrice=(1/100)*(5.7*price);
let convertToBrazil=5.7*price;
let total=percentOfPrice+convertToBrazil;
return total;
}

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.
Expand Down
35 changes: 27 additions & 8 deletions week-1/3-extra/2-piping.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,52 @@
the final result to the variable goodCode
*/

function add() {
function add(a,b) {
return a+b;

}

function multiply() {

function multiply(a,b) {
return a*b;
}

function format() {

function format(a) {
return `£${a}`;
}

const startingValue = 2
const startingValue = 2;

// Why can this code be seen as bad practice? Comment your answer.
let badCode =
let badCode = format((startingValue+10)*2);//no use of variables

/* BETTER PRACTICE */

let goodCode =
let goodCode = badCode;//fisrt put result in variable and the assign to 2nd variable

/* ======= TESTS - DO NOT MODIFY =====
There are some Tests in this file that will help you work out if your code is working.

To run these tests type `node 2-piping.js` into your terminal
*/

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}

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

test('add function - case 1 works', add(1,3) === 4);
test('add function - case 2 works', add(2.4,5.3) === 7.7);
test('multiply function works', multiply(2,3) === 6);
test('format function works', format(16) === "£16");
test('badCode variable correctly assigned', badCode === "£24");
test('goodCode variable correctly assigned', goodCode === "£24");
=======
const util = require('util');

function test(test_name, actual, expected) {
Expand Down
66 changes: 65 additions & 1 deletion week-1/3-extra/3-magic-8-ball.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Better not tell you now.
Cannot predict now.
Concentrate and ask again.

## Very negative
## Negative
Don't count on it.
My reply is no.
My sources say no.
Expand All @@ -46,13 +46,74 @@ Very doubtful.
// This should log "The ball has shaken!"
// and return the answer.
function shakeBall() {

logged="The ball has shaken!";
answers=["Very positive","Positive","Negative","Negative"]
//
//answers[Math.floor(Math.random()*answers.length)]
//console.log(Math.floor(Math.random()*4));
answer=Math.floor(Math.random()*answers.length)
return answer.toString();
}
//answers[Math.floor(Math.random()*answers.length)]
// The answer should come from shaking the ball
//let answer;
}

// This function should say whether the answer it is given is
// - very positive
// - positive
// - negative
// - very negative
function checkAnswer() {
// console.log("ans from main"+answer);
let subAns;

//let no=shakeBall();
//console.log("answer again from checkS"+no);
if(answer==0){
// ");
subAns=["It is certain."
,"It is decidedly so."
,"Without a doubt"
,"Yes - definitely."
,"You may rely on it."];
console.log("very positive");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "very positive";
}
if(answer==1){
subAns=["As I see it, yes."
,"Most likely."
,"Outlook good."
,"Yes."
,"Signs point to yes."];
console.log("positive");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "positive";
}
if(answer==2){
subAns=["Reply hazy, try again."
,"Ask again later."
,"Better not tell you now."
,"Cannot predict now."
,"Concentrate and ask again."];
console.log("negative");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "negative";
}
if(answer==3){
subAns=["Don't count on it."
,"My reply is no."
,"My sources say no."
,"Outlook not so good."
,"Very doubtful."];
console.log("very nagative");
console.log(subAns[Math.floor(Math.random()*subAns.length)]);
return "very negative";
}

=======
// This function should expect to be called with any value which was returned by the shakeBall function.
function checkAnswer(answer) {
}
Expand Down Expand Up @@ -89,6 +150,9 @@ function testAll() {
`shakeBall logs "The ball has shaken!"`,
logged === "The ball has shaken!"
);

test(`shakeBall returns an string answer"`, typeof answer === "string");

test(`shakeBall returns an string answer`, typeof answer === "string");

test(
Expand Down
8 changes: 8 additions & 0 deletions week-3/1-exercises/A-array-find/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@
Using .find(), we'd like to find the first name which starts with A and is longer than 7 letters.
*/


// write your code here
function findLongNameThatStartsWithA(names){
let output=names.find(function(name){
return name.charAt(0)==='A' && name.length>7;
})
return output;
}

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

Expand All @@ -13,3 +20,4 @@ console.log(longNameThatStartsWithA);

/* EXPECTED OUTPUT */
// "Alexandra"

12 changes: 11 additions & 1 deletion week-3/1-exercises/B-array-some/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@
- 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]];
let test=pairsByIndex.some(function(value){
return value===null;
})
if(test){
console.log("array contain null value");
process.exit(1)
}
else{
// 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
// process.exit(1);


var students = ["Islam", "Lesley", "Harun", "Rukmini"];
var mentors = ["Daniel", "Irina", "Mozafar", "Luke"];

Expand All @@ -22,3 +31,4 @@ var pairs = pairsByIndex.map(function(indexes) {
});

console.log(pairs);
}
Loading