Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.

London 10 | Shahid Amin | JavaScript-Core-1-Coursework-Week3 #277

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
8 changes: 6 additions & 2 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Example 1
let a;
console.log(a);

// a is not assigned a value

// Example 2
function sayHello() {
Expand All @@ -21,7 +21,7 @@ function sayHello() {

let hello = sayHello();
console.log(hello);

//There is no value passed to the function sayHello

Choose a reason for hiding this comment

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

Good, but not every function will have parameters. There's something missing inside the function sayHello for it not to be undefined. Can you spot what it is?

Choose a reason for hiding this comment

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

A little hint


// Example 3
function sayHelloToUser(user) {
Expand All @@ -30,7 +30,11 @@ function sayHelloToUser(user) {

sayHelloToUser();

//no value passed to the function


// Example 4
let arr = [1,2,3];
console.log(arr[3]);

//There is no value at index 3
4 changes: 2 additions & 2 deletions 1-exercises/B-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
*/

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

Choose a reason for hiding this comment

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

Nice.

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
4 changes: 2 additions & 2 deletions 1-exercises/C-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

Choose a reason for hiding this comment

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

👍

}

/*
Expand Down
2 changes: 2 additions & 0 deletions 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

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

numbers[3] = numbers.push(4)
//The first value is 1???

Choose a reason for hiding this comment

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

Good spot I'll forward that observation. But if it wasn't a 1 how would you change its value?

/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
8 changes: 8 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ const AGES = [
49
];

AGES.forEach(k => {
WRITERS.forEach(i => {
console.log(i + " is " + k + " years old")
})
})
Comment on lines +29 to +33

Choose a reason for hiding this comment

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

This doesn't look correct. Currently, it will say that every writer is 59,40,41,63 and 49 years old.




// TODO - Write for loop code here

/*
Expand Down
6 changes: 6 additions & 0 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ const BIRTHDAYS = [

function findFirstJulyBDay(birthdays) {
// TODO
for (let dateOfBirth of BIRTHDAYS) {
let getMonth = dateOfBirth.split(' ')[0];
if (getMonth === "July") {
return dateOfBirth;
}
}
Comment on lines 20 to +26

Choose a reason for hiding this comment

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

Good job on this.

}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
7 changes: 6 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
*/

function getTemperatureReport(cities) {
// TODO
let report = [];

Choose a reason for hiding this comment

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

Indentation doesn't look correct.

for (let city of cities) {
let temparatureOfCity = temperatureService(city);
report.push(`The temperature in ${city} is ${temparatureOfCity} degrees`);
}
return report;
}


Expand Down
45 changes: 42 additions & 3 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
const shortArticleTitles = [];
for (let articleTitle of allArticleTitles) {

Choose a reason for hiding this comment

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

indentation seem off here too.

Choose a reason for hiding this comment

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

Good job overall code inside the function will work correct but it doesn't seem to output a value. You are definitely missing something here.

if (articleTitle.length <= 65) {
shortArticleTitles.push(articleTitle);
}
}
}

/*
Expand All @@ -14,7 +19,16 @@ function potentialHeadlines(allArticleTitles) {
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let fewestword = allArticleTitles[0].split(' ').length;

Choose a reason for hiding this comment

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

Well done.

let result;
for (let articleTitle of allArticleTitles) {
let articleWords = articleTitle.split(' ').length;
if (articleWords < fewestword ) {
fewestword = articleWords;
result = articleTitle;
}
}
return result;
}

/*
Expand All @@ -23,7 +37,17 @@ function titleWithFewestWords(allArticleTitles) {
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
const regex = /[0-9]/;
let result = [];
for (let articleTitle of allArticleTitles) {
for (let i = 0; i < articleTitle.length; i++) {
if(articleTitle[i].match(regex)){
result.push(articleTitle);
break;
}
}
}
return result;
}

/*
Expand All @@ -32,6 +56,21 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO

let arrayOfLengths = allArticleTitles.map(w => w.length);
console.log(arrayOfLengths)
let sumOfLengths = 0;
for (let i = 0; i < arrayOfLengths.length; i++) {
sumOfLengths += arrayOfLengths[i];
console.log(sumOfLengths)
}

let sumOfArrayLengths = arrayOfLengths.length
console.log(sumOfArrayLengths)

let result = sumOfLengths/sumOfArrayLengths;
console.log(result)
Comment on lines +60 to +72

Choose a reason for hiding this comment

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

Indentation is off here too.


}


Expand Down
85 changes: 85 additions & 0 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@
*/

/* ======= Stock data - DO NOT MODIFY ===== */

/* Thinking throught the steps

let keys = STOCKS

let arrayA = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[0]
let arrayM = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[1]
let arrayAZ = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[2]
let arrayG = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[3]
let arrayT = CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS[4]


let totalA = arrayA.reduce(function(a,b){ return +a + +b; });
console.log(totalA)

let totalM = arrayM.reduce(function(a,b){ return +a + +b; });
console.log(totalM)

let totalAZ = arrayAZ.reduce(function(a,b){ return +a + +b; });
console.log(totalAZ)

let totalG = arrayG.reduce(function(a,b){ return +a + +b; });
console.log(totalG)

let totalT = arrayT.reduce(function(a,b){ return +a + +b; });
console.log(totalT)


totalA = Math.round(totalA).toFixed(2)
totalM = Math.round(totalM).toFixed(2)
totalAZ = Math.round(totalT).toFixed(2)
totalG = Math.round(totalG).toFixed(2)
totalT = Math.round(totalT).toFixed(2)



let finalArray = [totalA/5, totalM/5, totalAZ/5, totalG/5, totalT/5]

console.log(finalArray)*/

const STOCKS = ["aapl", "msft", "amzn", "googl", "tsla"];

const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Expand All @@ -18,6 +58,8 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
[1101.30, 1093.94, 1067.00, 1008.87, 938.53] // TSLA
];

console.log(CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS [0])

/*
We want to understand what the average price over the last 5 days for each stock is.
Implement the below function, which
Expand All @@ -35,6 +77,21 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let averageprice=[];

for(const stocksline of closingPricesForAllStocks){
let sum = 0;
let average = 0;
for(const item of stocksline){
sum = sum + item;

}
average = sum / stocksline.length;
averageprice.push(Math.round(average * 100) / 100);

}
return averageprice;

Choose a reason for hiding this comment

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

Good effort. Please make sure you indent your code correctly.


}

/*
Expand All @@ -49,6 +106,13 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
const priceChange = [];
for(const stocksline of closingPricesForAllStocks){
let sub = 0;
sub = stocksline.slice(-1) - stocksline[0];
priceChange.push(Math.round(sub*100)/100);
}
return priceChange;
}

/*
Expand All @@ -65,6 +129,27 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const result = [];
let num = 0;
let Name;
for(const stocksline of closingPricesForAllStocks){


let FirstPrice = stocksline[0];
let i = 1;
while(i<stocksline.length){
let price = stocksline[i];
if(price>FirstPrice){
FirstPrice = price;
}
i=i+1;
}

Name = stocks[num].toUpperCase();
result.push(`The highest price of ${Name} in the last 5 days was ${FirstPrice.toFixed(2)}`);
num = num + 1;
}
return result;
}


Expand Down
14 changes: 14 additions & 0 deletions 3-extra/1-radio-stations.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@

// `getAllFrequencies` goes here

function getAllFrequencies() {
const frequencies = [];
for (let i = 87; i <= 108; i++) {
frequencies.push(i);
}
return frequencies;
}

/**
* Next, let's write a function that gives us only the frequencies that are radio stations.
* Call this function `getStations`.
Expand All @@ -26,6 +34,12 @@
*/
// `getStations` goes here

function getStations() {
const frequencies = getAllFrequencies();
const radioStations = frequencies.filter(isRadioStation);
return radioStations;
}

/*
* ======= TESTS - DO NOT MODIFY =======
* Note: You are not expected to understand everything below this comment!
Expand Down
Loading