Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

NW6| Sabella -Fisseha| JS1 | Week4/Exercise #180

Open
wants to merge 6 commits 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
5 changes: 3 additions & 2 deletions week-1/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
This is just an instruction for the first activity - but it is just for human consumption
We don't want the computer to run these 2 lines - how can we solve this problem?
// This is just an instruction for the first activity - but it is just for human consumption
// We don't want the computer to run these 2 lines - how can we solve this problem?
// we can comment out , the thing we don't want he computer to run it.
2 changes: 1 addition & 1 deletion week-1/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// trying to create an age variable and then reassign the value by 1

const age = 33;
let age = 33;
age = age + 1;
6 changes: 3 additions & 3 deletions week-1/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Currently trying to print the string "I was born in Bolton" but it isn't working...
// what's the error ?

console.log(`I was born in ${cityOfBirth}`);
// what's the error ? it started by asking for print before initializing the variable
const cityOfBirth = "Bolton";
console.log(`I was born in ${cityOfBirth}`);

4 changes: 2 additions & 2 deletions week-1/errors/3.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const cardNumber = 4533787178994213;
let cardNumber = "4533787178994213";
const last4Digits = cardNumber.slice(-4);

console.log(last4Digits)
// The last4Digits variable should store the last 4 digits of cardNumber
// However, the code isn't working
// Make and explain a prediction about why the code won't work
Expand Down
5 changes: 3 additions & 2 deletions week-1/errors/4.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const 12HourClockTime = "20:53";
const 24hourClockTime = "08:53";
const twelveHourClockTime = "20:53";
const twentyfourHourClockTime = "08:53";
// variable names cannot start with a number.
1 change: 1 addition & 0 deletions week-1/exercises/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// it is reassigning the variable to increase by 1
7 changes: 6 additions & 1 deletion week-1/exercises/decimal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

const num = 56.5467;

const wholeNumberPart = Math.trunc(num);
const decimalPart = num % 1;
const roundedNum = Math.round(num);
console.log(decimalPart)
console.log(wholeNumberPart)
console.log(roundedNum)
// You should look up Math functions for this exercise https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math

// Create a variable called wholeNumberPart and assign to it an expression that evaluates to 56 ( the whole number part of num )
Expand Down
3 changes: 2 additions & 1 deletion week-1/exercises/initials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let firstName = "Creola";
let middleName = "Katherine";
let lastName = "Johnson";

const initials = firstName.charAt(0)+ middleName.charAt(0) + lastName.charAt(0)
console.log(initials)
// Declare a variable called initials that stores the first character of each string in upper case to form the user's initials
// Log the variable in each case
5 changes: 5 additions & 0 deletions week-1/exercises/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@

const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
// console.log(lastSlashIndex);
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
const dirPart = filePath.slice(0, lastSlashIndex);

const extPart = filePath.slice(filePath.lastIndexOf(".")+1);
console.log(`the ext part of ${filePath} ${extPart}`)
console.log(`the dir part of ${filePath} ${dirPart}`)
// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable
6 changes: 5 additions & 1 deletion week-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const minimum = 1;
const maximum = 100;

const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

console.log(num)
// maximum- minimum + 1= 100
// math.random * 100 = will be between [o, 100)
// Math.floor[0,100) = [0, 99]
// math.floor + minimum = [1,100]
// In this exercise, you will need to work out what num represents?
// Try breaking down the expression and using documentation to explain what it means
// It will help to think about the order in which expressions are evaluated
Expand Down
8 changes: 5 additions & 3 deletions week-1/explore/chrome.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ Just like the Node REPL, you can input JavaScript code into the Console tab and
Let's try an example.

In the Chrome console,
invoke the function `alert` with an input string of `"Hello world!"`;

invoke the function `alert`with an input string of `"Hello world!"`;
<!-- the chrome new tab page says "hello world" -->
What effect does calling the `alert` function have?

Now try invoking the function `prompt` with a string input of `"What is your name?"` - store the return value of your call to `prompt` in an variable called `myName`.

<!-- it gives me an input to add a name, when I add "my name" my name apperared on the console -->
What effect does calling the `prompt` function have?
<!-- it brings a user input -->
What is the return value of `prompt`?
<!-- the return value is the value you add on the input -->
16 changes: 13 additions & 3 deletions week-1/interpret/percentage-change.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,19 @@ console.log(`The percentage change is ${percentageChange}`);
// Read the code and then answer the questions below

// a) How many function calls are there in this file? Write down all the lines where a function call is made

// --> 5 function calls.
// --> Number
// --> ReplceAll
// --> Number
// --> ReplaceAll
// --> percentageChange
// b) Identify all the lines that are variable reassignment statements

// --> carPrice = Number()
// --> priceAfterOneYear = Number()
// c) Identify all the lines that are variable declarations

// --> let carPrice = "10,000";
// --> let priceAfterOneYear = "8,543";
// --> const priceDifference = carPrice - priceAfterOneYear;
// --> const percentageChange = (priceDifference / carPrice) * 100;
// d) Describe what the expression Number(carPrice.replaceAll(",","")) is doing - what is the purpose of this expression?
// --> It replace all the commas to an empty string and the Number constructor is used to change the sting to numerical value
18 changes: 11 additions & 7 deletions week-1/interpret/time-format.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const movieLength = 8784; // length of movie in seconds

const remainingSeconds = movieLength % 60;
const remainingSeconds = movieLength % 1.30;
const totalMinutes = (movieLength - remainingSeconds) / 60;

const remainingMinutes = totalMinutes % 60;
Expand All @@ -13,17 +13,21 @@ console.log(result);

// For the piece of code above, read the code and then answer the following questions

// a) How many variable declarations are there in this program?
// a) How many variable declarations are there in this program? 7

// b) How many function calls are there?
// b) How many function calls are there?
// --> There is no function call.

// c) Using documentation on MDN, explain what the expression movieLength % 60 represents

// --> It is the reminder of 8784 divided by 60.
// d) Interpret line 4, what does the expression assigned to totalMinutes mean?

// e) What do you think the variable result represents? Can you think of a better name for this variable?

// --> Const totalMinutes = (movieLength - remainingSeconds) / 60; == it 8784 - 24(remaining of 8784/60) which is 8760/60 =146;
// e) What do you think the variable result represents? Can you think of a better name for this variable?
// --> totalResult,
// f) Think about whether this program will work for all values of movieLength.
// Think of what values may cause problems for the code.
// Decide the result should be for those values, then test it out.
// Can you find any values of movieLength which don't give you what you'd expect?
// --> Division by zero.
// --> Floating numbers (decimal).
// --> The result will be unknown.
14 changes: 13 additions & 1 deletion week-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,16 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with

// 1. const penceString = "399p": initialises a string variable with the value "399p"
// 1. const penceString = "399p": initializes a string variable with the value "399p"
// 2. penceString.length = 4 : total number of count of a string.
// 3. penceString.length - 1 = 3
// 4. penceString.substring(0,penceString.length - 1); -> penceString.substring(0,3)
// 5. penceString.substring(0,3); = 399: it selects character between (0,3) so it remove "p".
// 6. penceStringWithoutTrailingP.padStart(3, "0"); : 399 has already 3 character and the padStart ask to add "o" upto having three digit.
// 7. paddedPenceNumberString = 399;
// 8. paddedPenceNumberString.length - 2 = 1;
// 9. pound = paddedPenceNumberString.substring( 0,1) = 3;
// 10. paddedPenceNumberString.substring(paddedPenceNumberString.length - 2)--> paddedPenceNumberString.substring(3 - 2)
// 11. 399.paddedPenceNumberString.substring(3 - 2)= 99;
// 12. pence= 99.padEnd(2,"0")--> 99 has already 2 digit so no need to add "0";
// 13. console.log(`£${pounds}.${pence}`);= 3.99;
2 changes: 2 additions & 0 deletions week-2/debug/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ function multiply(a, b) {
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
// 320 and undefined because a and b are saved in the local scope not in global.
// If we put return instead of console inside the function it would work
2 changes: 2 additions & 0 deletions week-2/debug/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ function sum(a, b) {
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
// The sum will still be undefined.
// Because of the code error,we don't use semi-column after we wrote return,
1 change: 1 addition & 0 deletions week-2/debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ console.log(`The last digit of 806 is ${getLastDigit(806)}`);

// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem
// It will be all 3, because we use a global scope declaration in to our local scope, it always read to it,instead of using the numbers we gave on the console
6 changes: 5 additions & 1 deletion week-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
// write down the error you predict will be raised
// then call the function capitalise with a string input
// interpret the error message and figure out why it's happening, if your prediction was wrong
// answer
// It is not going to work.
// str should not be declared as a variable since it is already a parameter for capitalise function.

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}
console.log(capitalise("anna"));
7 changes: 5 additions & 2 deletions week-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
// Write down the error you predict will be raised
// Why will an error occur when this program runs?
// Play computer with the example to work out what is going on
// it doesn't work
// the code used decimal as a constant variable inside the function and again as a parameter.
// If we take out the variable decimal inside the local scope and put it as global will make it work.

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;

const percentage = `${decimalNumber * 100}%`;

return percentage;
}

const decimalNumber = 0.5;
console.log(decimalNumber);
12 changes: 8 additions & 4 deletions week-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// Predict and explain first...
// this function should square any number but instead we're going to get an error
// what is happening? How can we fix it?

function square(3) {
// answer
// We can not use numbers as a parameter.
// the function doesn't know 'num'
// If we place "num" inside the parameter.
// And place the constant variable outside the function, it will work.
function square(num) {
return num * num;
}


const result = square(3);
console.log(result);
7 changes: 7 additions & 0 deletions week-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
// Given someone's weight in kg and height in metres
// When we call this function with the weight and height
// Then it returns their Body Mass Index to 1 decimal place
function bmi(weight,height){
if (weight <= 0 || height <= 0) { return "it is a wrong input";}
str = height*height;
calculate = weight/str
return calculate.toFixed(1)
}
console.log(bmi(70,1.73));
9 changes: 9 additions & 0 deletions week-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@

// Come up with a clear, simple name for the function
// Use the string documentation to help you plan your solution
function capitalise(str){
words = str.split(" ");
camel = words.map(word => word.toUpperCase());
camelsnake = camel.join("_");
return camelsnake;
}
console.log(capitalise("lord of the rings"));
console.log(capitalise("the great gatsby"));
console.log(capitalise("the da vinci code"));
18 changes: 18 additions & 0 deletions week-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,21 @@
// Take this code and turn it into a reusable block of code.
// Declare a function called toPounds with an appropriately named parameter.
// Call this function a number of times to check it works for different inputs
const penceString = "399p";

const penceStringWithoutTrailingP = penceString.substring(
0,
penceString.length - 1
);

const paddedPenceNumberString = penceStringWithoutTrailingP.padStart(3, "0");
const pounds = paddedPenceNumberString.substring(
0,
paddedPenceNumberString.length - 2
);

const pence = paddedPenceNumberString
.substring(paddedPenceNumberString.length - 2)
.padEnd(2, "0");

console.log(`£${pounds}.${pence}`);
5 changes: 5 additions & 0 deletions week-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
// Given a number,
// When I call this function with a number
// Then it returns the new price with VAT added on
function vat(p,v){

return p*v ;
}
console.log(vat(50,1.2));
20 changes: 15 additions & 5 deletions week-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,28 @@ console.log(formatTimeDisplay(143));
// Questions

// a) When formatTimeDisplay is called how many times will pad be called?

// 3 times
// Call formatTimeDisplay with an input of 143, now answer the following:

// b) What value is assigned to the parameter num when pad is called for the first time?

// 0
// c) What is the return value of pad when it is called for the first time?

// Answer = 00
// function formatTimeDisplay(143) {
// const remainingSeconds = 143% 60; which is 23 remaining
// const totalMinutes = (143- 23) / 60; it will be 120/60 which is 2
// const remainingMinutes = 2% 60; which is 0
// const totalHours = (2- 0) / 60; which is 0.033
// const remainingHours = 0.033 % 24; which is 0

// return `${pad(0)}:${pad(0)}:${pad(
// 23
// )}`;
// d) What is the value assigned to the parameter num when pad
// is called for the last time in this program? Explain your answer

// 23
// e) What is the return value when pad is called
// for the last time in this program? Explain your answer

// 23
// f) Research an alternative way of padding the numbers in this code.
// Look up the string functions on mdn
36 changes: 36 additions & 0 deletions week-4/implement/card-validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Number must be 16 digits, all of them must be numbers.
// - You must have at least two different digits represented (all of the digits cannot be the same).
// - The final digit must be even.

Choose a reason for hiding this comment

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

Is there anywhere that the final digit gets checked for odd-/even-ness?

// - The sum of all the digits must be greater than 16.
function addingCard(card) {
// typeof card === 'number' ;
const num = String(card).split("").map(Number);

let add = 0;

for (let i = 0; i < num.length - 1; i += 2) {
add += num[i] + num[i + 1];
}
Comment on lines +11 to +13

Choose a reason for hiding this comment

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

I think this loop is slightly more complicated than it needs to be. If we go from i == 0 to i < num.length, we will already visit each number once. Then we only need to add the current element, instead of the current element and the one after that


return add;
}

function creditCard(card) {
const num = String(card).split("").map(Number);

if (num.length >= 16 && new Set(num).size >= 2 && addingCard(card) >= 16) {
return "card is valid";
} else {
return "card is not valid";
}
}

// console.log(creditCard('26287476788868563215'));
test("check if card is valid", function () {
const card= '26287476788868563215';
const currentOutput = creditCard(card);
const targetOutput = "card is valid";

expect(currentOutput).toBe(targetOutput);

});
Loading