Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
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
10 changes: 5 additions & 5 deletions 1-exercises/A-undefined/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/

// Example 1
let a;
let a; // because we didn't declare any value to a
console.log(a);


// Example 2
// Example 2// because function not returning any values
function sayHello() {
let message = "Hello";
}
Expand All @@ -24,13 +24,13 @@ console.log(hello);


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

sayHelloToUser();
sayHelloToUser();// we need to declare user inside that function /we called function but its parameter is not declared


// Example 4
let arr = [1,2,3];
console.log(arr[3]);
console.log(arr[3]);// there is not third index in this array
10 changes: 8 additions & 2 deletions 1-exercises/B-while-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
*/

function evenNumbers(n) {
// TODO
let i=0
let newArray= []
while (i<n){
evenNumber= i*2
newArray.push(evenNumber)
i++
}
console.log(newArray.join());
}

evenNumbers(3); // should output 0,2,4
evenNumbers(0); // should output nothing
evenNumbers(10); // should output 0,2,4,6,8,10,12,14,16,18
9 changes: 8 additions & 1 deletion 1-exercises/C-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
let i = 0;
while (i < birthdays.length) {
if (birthdays[i].includes("July")) {
return birthdays[i];
}
i++;
}
}

console.log(findFirstJulyBDay(BIRTHDAYS)); // should output "July 11th"
11 changes: 9 additions & 2 deletions 1-exercises/D-do-while/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@

Using a do-while loop, write a function which returns the sum of the first n even numbers (starting from 0)
*/

function evenNumbersSum(n){
let sum=0;
let i=0;
do {
sum +=(i*2);
i++;
} while (i<n)

function evenNumbersSum(n) {
// TODO
return sum
}

console.log(evenNumbersSum(3)); // should output 6
Expand Down
6 changes: 3 additions & 3 deletions 1-exercises/E-for-loop/exercise1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@


// Change the below code to use a for loop instead of a while loop.
let i = 0;
while(i < 26) {

for( let i=0;i < 26;i++) {
console.log(String.fromCharCode(97 + i));
i++;

}
// The output shouldn't change.
3 changes: 3 additions & 0 deletions 1-exercises/E-for-loop/exercise2.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ const AGES = [
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
9 changes: 8 additions & 1 deletion 1-exercises/F-for-of-loop/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ let tubeStations = [
"Picadilly Circus",
"Oxford Street",
"Tottenham Court Road"
];

];
for (let station of tubeStations){
console.log(station)
}

// TODO Use a for-of loop to capitalise and output each letter in the string seperately.
let str = "codeyourfuture";
for ( let letter of str){

console.log(letter.toUpperCase())
}
11 changes: 10 additions & 1 deletion 2-mandatory/1-weather-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@
*/

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

for (let city of cities){
let temparature=temperatureService(city)
string=`The temperature in ${[city]} is ${temparature} degrees`
report.push(string)

}
return report
}


Expand Down
6 changes: 5 additions & 1 deletion 2-mandatory/2-retrying-random-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ function generateRandomNumber() {
}

function getRandomNumberGreaterThan50() {
// TODO - implement using a do-while loop
let randomNumber;
do{
randomNumber= generateRandomNumber()
}while (randomNumber<= 50)
return randomNumber
}

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
47 changes: 43 additions & 4 deletions 2-mandatory/3-financial-times.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,72 @@
Implement the function below, which will return a new array containing only article titles which will fit.
*/
function potentialHeadlines(allArticleTitles) {
// TODO
let newArray=[];

for(list of allArticleTitles){
if (list.length<=65){
newArray.push(list)
}
}
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
let fewestWordsSoFAr;
let titleWithFewestWords;
for( let title of allArticleTitles){
let numWords=title.split(" ").length
if (fewestWordsSoFAr===undefined||numWords<fewestWordsSoFAr){
fewestWordsSoFAr=numWords;
titleWithFewestWords=title
}
}
return titleWithFewestWords;
}

/*
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 doesTitleContainsNumber(title){
for (let character of title){
if(character>="0" && character<= "9"){
return true
}
}
return false
}


function headlinesWithNumbers(allArticleTitles) {
// TODO
let articleWithNumbers=[]
for(let title of allArticleTitles){
if (doesTitleContainsNumber(title)){
articleWithNumbers.push(title)
}
}
return articleWithNumbers
}

/*
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
let totalLength=allArticleTitles.length
for(title of allArticleTitles){
totalCharacters +=title.length
}
return Math.round(totalCharacters/totalLength)
}


Expand Down
66 changes: 61 additions & 5 deletions 2-mandatory/4-stocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,42 @@ const CLOSING_PRICES_LAST_5_DAYS_FOR_ALL_STOCKS = [
Solve the smaller problems, and then build those solutions back up to solve the larger problem.
Functions can help with this!
*/
/*
function getAveragePrices(closingPricesForAllStocks) {
// TODO
let average=[]
for (let pricesForStock of closingPricesForAllStocks){
sum=0
let avergeOfOneStock;
for (let price of pricesForStock){
sum +=price

}
sum= Math.round(sum*100)/100
avergeOfOneStock= sum/pricesForStock.length
return average.push(avergeOfOneStock)
}
return average


}

*/
function getAveragePrices(closingPricesForAllStocks) {
let averages=[]
for (let pricesForStock of closingPricesForAllStocks){

averages.push(getAveragePricesForStock(pricesForStock))
}
return averages
}

function getAveragePricesForStock(pricesForStock){
let total=0
for (let price of pricesForStock){
total +=price
}
let averge=total/pricesForStock.length
return Math.round(averge*100)/100;
}

/*
Expand All @@ -48,9 +82,18 @@ 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
let changes=[]
for (let pricesForStock of closingPricesForAllStocks){
changes.push(getPriceChangesForStock(pricesForStock))
}

return changes
}


function getPriceChangesForStock(pricesForStock){
let priceChange=pricesForStock[pricesForStock.length-1]-pricesForStock[0]
return Math.round(priceChange*100)/100
}
/*
As part of a financial report, we want to see what the highest price was for each stock in the last 5 days.
Implement the below function, which
Expand All @@ -64,9 +107,22 @@ function getPriceChanges(closingPricesForAllStocks) {
The price should be shown with exactly 2 decimal places.
*/
function highestPriceDescriptions(closingPricesForAllStocks, stocks) {
// TODO
let description=[]
for(let i=0 ;i<closingPricesForAllStocks.length;i++){
let highestPrice=getHighPricesForStock(closingPricesForAllStocks[i])
description.push(`The highest price of ${stocks[i].toUpperCase()} in the last 5 days was ${highestPrice.toFixed(2)}`)
}
return description;
}
function getHighPricesForStock(stockPrice){
let heightestPricesSoFar=0;
for (price of stockPrice){
if (price > heightestPricesSoFar){
heightestPricesSoFar=price
}
}
return heightestPricesSoFar
}


/* ======= TESTS - DO NOT MODIFY ===== */
test("should return the average price for each stock", () => {
Expand Down
2 changes: 1 addition & 1 deletion 3-extra/1-factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/

function factorial(input) {
// TODO

}

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