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

glasgow class 6-siver omar-javascript 1-week3 #274

Open
wants to merge 1 commit into
base: main
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
11 changes: 7 additions & 4 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,28 @@
// Example 1
let a;
console.log(a);

//let does not have any value

// Example 2
function sayHello() {
let message = "Hello";
// sayHello inside is empty,doesnt return any value
}

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

//

// Example 3
function sayHelloToUser(user) {
function sayHelloToUser(user) {
console.log(`Hello ${user}`);
}

sayHelloToUser();
sayHelloToUser(); //this function is called but no parameter has been passed
//so the variable user has no value


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
//3 is not an item of arr
5 changes: 3 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,9 @@
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

/*
DO NOT EDIT BELOW THIS LINE
Expand Down
5 changes: 3 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
}

/*
Expand All @@ -21,6 +21,7 @@ let names = ["Irina", "Ashleigh", "Mozafar", "Joe"];
console.log(first(numbers));
console.log(last(numbers));
console.log(last(names));
console.log(first(names));

/*
EXPECTED RESULT
Expand Down
3 changes: 2 additions & 1 deletion 1-exercises/C-array-get-set/exercises2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

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

numbers[0]=1;
numbers.push(4);
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
3 changes: 3 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ const AGES = [
63,
49
];
for(let i =0; i<WRITERS.length;i++){
console.log(`${WRITERS[i]} is ${AGES[i]} years old.`)
}

// TODO - Write for loop code here

Expand Down
8 changes: 7 additions & 1 deletion 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
let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) {
return birthdays[i];
}
i++;
}
}

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

function getTemperatureReport(cities) {
// TODO
}
let newArray = [];
for (let i = 0; i < cities.length; i++) {
let degrees = temperatureService(cities[i]);
const statement = `The temperature in ${cities[i]} is ${degrees} degrees`;
newArray.push(statement);
}
return newArray;
}


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

function temperatureService(city) {
let temparatureMap = new Map();

temparatureMap.set('London', 10);
temparatureMap.set('Paris', 12);
temparatureMap.set('Barcelona', 17);
temparatureMap.set('Dubai', 27);
temparatureMap.set('Mumbai', 29);
temparatureMap.set('São Paulo', 23);
temparatureMap.set('Lagos', 33);
let temparatureMap = new Map();

temparatureMap.set("London", 10);
temparatureMap.set("Paris", 12);
temparatureMap.set("Barcelona", 17);
temparatureMap.set("Dubai", 27);
temparatureMap.set("Mumbai", 29);
temparatureMap.set("São Paulo", 23);
temparatureMap.set("Lagos", 33);

return temparatureMap.get(city);
}

test("should return a temperature report for the user's cities", () => {
let usersCities = [
"London",
"Paris",
"São Paulo"
]
let usersCities = ["London", "Paris", "São Paulo"];


expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in London is 10 degrees",
Expand All @@ -47,10 +49,7 @@ test("should return a temperature report for the user's cities", () => {
});

test("should return a temperature report for the user's cities (alternate input)", () => {
let usersCities = [
"Barcelona",
"Dubai"
]
let usersCities = ["Barcelona", "Dubai"];

expect(getTemperatureReport(usersCities)).toEqual([
"The temperature in Barcelona is 17 degrees",
Expand All @@ -60,4 +59,4 @@ test("should return a temperature report for the user's cities (alternate input)

test("should return an empty array if the user hasn't selected any cities", () => {
expect(getTemperatureReport([])).toEqual([]);
});
});
38 changes: 35 additions & 3 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let returnedArray = [];
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length <= 65) {
returnedArray.push(allArticleTitles[i]);
}
}
return returnedArray;
}

// npm test -- --testPathPattern 2-financial-times
/*
The editor of the FT likes short headlines with only a few words!
Implement the function below, which returns the title with the fewest words.
(you can assume words will always be seperated by a space)
*/
function titleWithFewestWords(allArticleTitles) {
// TODO
let min = Infinity;
let tempIndex;
for (let i = 0; i < allArticleTitles.length; i++) {
if (allArticleTitles[i].length < min) {
min = allArticleTitles[i].length;
tempIndex = i;
}
}
return allArticleTitles[tempIndex];
}

/*
Expand All @@ -24,6 +38,18 @@ function titleWithFewestWords(allArticleTitles) {
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
let headlinesWithNumber = [];

for (let i = 0; i < allArticleTitles.length; i++) {
for (let j = 0; j < allArticleTitles[i].length; j++) {
if (!isNaN(parseInt(allArticleTitles[i][j]))) {
headlinesWithNumber.push(allArticleTitles[i]);
break;
}
}
}
return headlinesWithNumber;

}

/*
Expand All @@ -32,6 +58,12 @@ function headlinesWithNumbers(allArticleTitles) {
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
let totalChars = 0;
for (let i = 0; i < allArticleTitles.length; i++) {
totalChars += allArticleTitles[i].length;
}
let avgChars = Math.round(totalChars / allArticleTitles.length);
return avgChars;
}


Expand Down
33 changes: 33 additions & 0 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let newAr = [];
for (const element of closingPricesForAllStocks) {
let sum = 0;
let average = 0;
for (let i = 0; i < element.length; i++) {
sum += element[i];
}
average = sum / element.length;
newAr.push(Math.round(average * 100) / 100);
}

return newAr;
}

/*
Expand All @@ -49,6 +61,13 @@ function getAveragePrices(closingPricesForAllStocks) {
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
let returnedArray = [];
for (let element of closingPricesForAllStocks) {
let [...newArr] = element;
let changes = newArr[newArr.length - 1] - newArr[0];
returnedArray.push(Math.round(changes * 100) / 100);
}
return returnedArray;
}

/*
Expand All @@ -65,6 +84,20 @@ function getPriceChanges(closingPricesForAllStocks) {
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
const hightestPrice=[];
let maxPrice=0;
for(let k=0; k<stocks.length;){
for(let i=0; i< closingPricesForAllStocks.length; i++){

maxPrice=Math.max(...closingPricesForAllStocks[i]);
hightestPrice.push(`The highest price of ${stocks[k].toUpperCase()} in the last 5 days was ${maxPrice.toFixed(2)}`)
maxPrice=0;
k++;

}

return hightestPrice;
}
}


Expand Down
Loading