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

Glasgow_6-Najah_Farah-Js_3 #279

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
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,16 +12,18 @@
// Example 1
let a;
console.log(a);

// element a has no value

// Example 2
function sayHello() {
let message = "Hello";
}


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

// the function is missing from the callback

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

sayHelloToUser();


// the function is missing the callback variable
// Example 4
let arr = [1,2,3];
console.log(arr[3]);

// index 3 does not exist in the array
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","Douglas","Irina","Michael"] ;// Create an array with the names of the mentors: Daniel, Irina and Rares

/*
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.pop(); // complete this statement
}

/*
Expand Down
2 changes: 1 addition & 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,7 @@
*/

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

numbers.push(4);
/*
DO NOT EDIT BELOW THIS LINE
--------------------------- */
Expand Down
11 changes: 11 additions & 0 deletions 1-exercises/D-for-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,18 @@ const AGES = [
];

// TODO - Write for loop code here
function intro(){
let list = []
for (let i = 0;i < WRITERS.length;){
list.push(`${WRITERS[i]}is ${AGES[i]} years old`)
i++

}
return list

}

console.log(intro())
/*
The output should look something like this:

Expand Down
13 changes: 10 additions & 3 deletions 1-exercises/E-while-loop-with-array/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ const BIRTHDAYS = [
"November 15th"
];

function findFirstJulyBDay(birthdays) {
// TODO
function findFirstJulyBDay() {
let first = ' ';
let arr = 0
while(arr < BIRTHDAYS.length){ if (BIRTHDAYS[arr].includes('July 11th')){
first=BIRTHDAYS[arr];
}
arr ++
}
return first
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
console.log(findFirstJulyBDay()); // should output "July 11th"
25 changes: 22 additions & 3 deletions 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@
For example, "The temperature in London is 10 degrees"
- Hint: you can call the temperatureService function from your function
*/

let usersCities = [
"London",
"Paris",
"São Paulo",
"Barcelona",
"Dubai"
]
function getTemperatureReport(cities) {
// TODO
}
let report = [];

for (let i = 0; i < cities.length; i++) {
let city = cities[i];
let temperature = temperatureService(city);

report.push(`The temperature in ${city} is ${temperature} degrees`);
}

return report;
}






/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
100 changes: 81 additions & 19 deletions 2-mandatory/2-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,113 @@
The home page of the web site has a headline section, which only has space for article titles which are 65 characters or less.
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO

const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elon Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
];








function potentialHeadlines(ARTICLE_TITLES) {
let newArray= [];

for (let i=0;i < ARTICLE_TITLES.length; i++){
if ( ARTICLE_TITLES[i].length<= 65){
newArray.push(ARTICLE_TITLES[i])
}

}

return newArray
}

/*
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
function titleWithFewestWords(ARTICLE_TITLES) {
let smallestTitle= ARTICLE_TITLES[0];

for (let i = 1; i < ARTICLE_TITLES.length; i++) {
if (ARTICLE_TITLES[i].split(' ').length < smallestTitle.split(' ').length) {
smallestTitle = ARTICLE_TITLES[i];
}
}

return smallestTitle;
}


/*
The editor of the FT has realised that headlines which have numbers in them get more clicks!
Implement the function below to return a new array containing all the headlines which contain a number.
(Hint: remember that you can also loop through the characters of a string if you need to)
*/
function headlinesWithNumbers(allArticleTitles) {
// TODO
function headlinesWithNumbers(allArticleTitles) {
let headlines = [];

for (let i = 0; i < allArticleTitles.length; i++) {
let title = allArticleTitles[i];
let hasNumber = false;

for (let j = 0; j < title.length; j++) {
let char = title[j];

if (char >= '0' && char <= '9') {
hasNumber = true;
break;
}
}

if (hasNumber) {
headlines.push(title);
}
}

return headlines;
}

}

/*
The Financial Times wants to understand what the average number of characters in an article title is.
Implement the function below to return this number - rounded to the nearest integer.
*/
function averageNumberOfCharacters(allArticleTitles) {
// TODO
}
let totalCharacters = 0;

for (let i = 0; i < allArticleTitles.length; i++) {
let title = allArticleTitles[i];
totalCharacters += title.length;
}

let averageCharacters = totalCharacters / allArticleTitles.length;
let roundedAverageCharacters = Math.round(averageCharacters);

return roundedAverageCharacters;
}




/* ======= List of Articles - DO NOT MODIFY ===== */
const ARTICLE_TITLES = [
"Streaming wars drive media groups to spend more than $100bn on new content",
"Amazon Prime Video India country head: streaming is driving a TV revolution",
"Aerospace chiefs prepare for bumpy ride in recovery of long-haul flights",
"British companies look to muscle in on US retail investing boom",
"Libor to take firm step towards oblivion on New Year's Day",
"Audit profession unattractive to new recruits, says PwC boss",
"Chinese social media users blast Elon Musk over near miss in space",
"Companies raise over $12tn in 'blockbuster' year for global capital markets",
"The three questions that dominate investment",
"Brussels urges Chile's incoming president to endorse EU trade deal",
];


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

Expand Down
53 changes: 49 additions & 4 deletions 2-mandatory/3-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,26 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Functions can help with this!
*/
function getAveragePrices(closingPricesForAllStocks) {
// TODO
function getAveragePrices(closingPricesForAllStocks) {
let averages = [];

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let prices = closingPricesForAllStocks[i];
let total = 0;

for (let j = 0; j < prices.length; j++) {
total += prices[j];
}

let average = total / prices.length;
let roundedAverage = Number(average.toFixed(2));

averages.push(roundedAverage);
}

return averages;
}

}

/*
Expand All @@ -48,7 +67,22 @@ function getAveragePrices(closingPricesForAllStocks) {
The price change value should be rounded to 2 decimal places, and should be a number (not a string)
*/
function getPriceChanges(closingPricesForAllStocks) {
// TODO
function getPriceChanges(closingPricesForAllStocks) {
let changes = [];

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let prices = closingPricesForAllStocks[i];
let firstPrice = prices[0];
let lastPrice = prices[prices.length - 1];
let change = lastPrice - firstPrice;
let roundedChange = Number(change.toFixed(2));

changes.push(roundedChange);
}

return changes;
}

}

/*
Expand All @@ -64,8 +98,19 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
}
let descriptions = [];

for (let i = 0; i < closingPricesForAllStocks.length; i++) {
let prices = closingPricesForAllStocks[i];
let highestPrice = Math.max(...prices);
let stock = stocks[i].toUpperCase();
let description = `The highest price of ${stock} in the last 5 days was ${highestPrice.toFixed(2)}`;
descriptions.push(description);
}

return descriptions;
}



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