Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manchester | Zabihollah Namazi | Module-Data-Flows | challenges | unit-testing | passing-tests #125

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
27 changes: 26 additions & 1 deletion challenges/unit-testing/passing-tests/car-sales/car-sales.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
function sales(carsSold) {}
let carsSold = [
{ make: "Ford", model: "Fiesta", colour: "Red", price: 5999 },
{ make: "Land Rover", model: "Defender", colour: "Muddy", price: 12000 },
{ make: "Toyota", model: "Prius", colour: "Silver", price: 6500 },
{ make: "Honda", model: "Civic", colour: "Yellow", price: 8000 },
{ make: "Ford", model: "Fiesta", colour: "Red", price: 15000 },
{ make: "Land Rover", model: "Discovery", colour: "Blue", price: 9000 },
{ make: "Ford", model: "Fiesta", colour: "Green", price: 2000 },
];

function sales(carsSold) {
let totals = {
Ford: 0,
Honda: 0,
"Land Rover": 0,
Toyota: 0,
};
for(let key in totals){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you think of a better name for key? Hint: think what it will contain and how you are going to use its value (you will compare it to make from array).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think makeCar would be better

for(let obj in carsSold){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to rename obj to something more relevant here. Because for .. in will not iterate over objects in carsSold array. obj will not contain the whole object from this array. It will contain something else.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

car would be better in my opinion

if (key == carsSold[obj].make){
totals[key] += carsSold[obj].price;
}
}
}
return totals;
}
sales(carsSold);
module.exports = sales;
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ test("Car sales", function () {

expect(output).toEqual(totals);
});

11 changes: 9 additions & 2 deletions challenges/unit-testing/passing-tests/factorial/factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
// calculate and return the factorial of int
// note: factorial of 0 is 1

function factorial(int) {}

function factorial(int) {
let factoSum = 1;
for (let i = 1; i <= int; i++){
factoSum *= i ;
}
console.log(factoSum);
return factoSum;
}
factorial(5);
module.exports = factorial;
15 changes: 12 additions & 3 deletions challenges/unit-testing/passing-tests/get-average/get-average.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
// the input is an array of numbers and strings
// return the average of all the numbers
// be sure to exclude the strings

function average(numbers) {}

let numbers = [4, "-", 8, 11, "hello", "57", 0, 2, "hi", 22];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't agree that numbers variable contains array of numbers. Do you? Also, if you are not going to reassign this variable to a new array, what is a better variable type here instead of let?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated
thanks sir

function average(numbers) {
let numCount = 0;
for (let item in numbers){
if (typeof(numbers[item]) == "number"){
numCount += 1;
}
}
console.log(numCount);
return numCount;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks to me that you are returning counter (how many numbers you have found in the given array). I think your task is to return average.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed it

}
average(numbers);
module.exports = average;
18 changes: 18 additions & 0 deletions fetch/programmer-humour/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>XKCD Comic Viewer</title>
<script src="script.js" defer></script>
</head>
<body>
<!-- <section id="section"></section> -->





</body>
</html>
27 changes: 27 additions & 0 deletions fetch/programmer-humour/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

let myData;

async function fetchData() {
try{
const response = await fetch("https://xkcd.now.sh/?comic=latest")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, always close your expressions with a semicolon ;


if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}

myData = await response.json()
//console.log(`${JSON.stringify(myData)} this is api`)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, don't leave commented out code in your final work.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this commit is a mistake
sorry

let section = document.body;
const image = document.createElement("img");
image.src = myData.img;
image.alt = myData.alt;
section.appendChild(image);
}
catch(error) {
console.error("an error happened", error.message);
alert("Something went wrong. Please try again later.");
}

}

fetchData()
4 changes: 4 additions & 0 deletions fetch/programmer-humour/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
img{
width: 500px;
height: 500px;
}