Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COHORT_NAME | deborah ganedze| js1| WEEK2 #62

Open
wants to merge 18 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
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"recommendations": [
"esbenp.prettier-vscode",
"dbaeumer.vscode-eslint",
"streetsidesoftware.code-spell-checker",
"eamodio.gitlens",
"ritwickdey.LiveServer",
"vsliveshare.vsliveshare"
]
}
3 changes: 3 additions & 0 deletions week-1/exercises/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const maximum = 100;
const num = Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;

// In this exercise, you will need to work out what num represents?
//num represents a random interger generated within a specified range from minimun to maximum which can be from 1 to 100

// 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
// Try logging the value of num several times to build an idea of what the program is doing
4 changes: 3 additions & 1 deletion week-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...

function multiply(a, b) {
console.log(a * b);
return a * b;
}

console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);

//320 will be the result but it will be undifined unless you add return function
8 changes: 0 additions & 8 deletions week-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
// Predict and explain first...

function sum(a, b) {
return;
a + b;
}

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
5 changes: 3 additions & 2 deletions week-2/debug/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const num = 103;

function getLastDigit() {
return num.toString().slice(-1);
function getLastDigit(Number) {
return Number.toString().slice(-1);
}

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
Expand All @@ -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
//The issue lies in the num variable. num is declared as a constant (const num = 103) outside the function. Inside the getLastDigit function, it tries to use this constant num instead of the value passed to the function when it is called (like getLastDigit(42)).
18 changes: 16 additions & 2 deletions week-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
// Predict and explain first...

//The function takes a string str as input and attempts to capitalize the first character of the string using:
//Possible error: If str is empty ("") or not a string (e.g., null or undefined), then str[0] will either be undefined or nonexistent. Calling .toUpperCase() on undefined will result in a TypeError.

// 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



function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
if (typeof str !== "string" || str.length === 0) {
return ""; // Return an empty string for invalid input
}
let result = `${str[0].toUpperCase()}${str.slice(1)}`;
return result;
}
console.log(capitalise("hello")); // Outputs: "Hello"
console.log(capitalise("")); // Outputs: ""
console.log(capitalise(null)); // Outputs: ""
15 changes: 10 additions & 5 deletions week-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
// Predict and explain first...
//The variable decimalNumber is declared within the function scope (using const) and is not accessible outside of it. This creates a scope conflict when trying to access decimalNumber globally.

// Write down the error you predict will be raised
const decimalNumber = 0.5;
console.log(decimalNumber);


// Why will an error occur when this program runs?
// Play computer with the example to work out what is going on

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
const percentage = `${decimalNumber * 100}%`;

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

console.log(decimalNumber);
// Call the function and log the result
console.log(convertToPercentage(0.5)); // Outputs: "50%"
11 changes: 7 additions & 4 deletions week-2/errors/2.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@

// Predict and explain first...
//The code will throw a SyntaxError because parameter names in a function declaration cannot be literal values like 3. In JavaScript, function parameters must be variable names, not numbers, strings, or other literals.

// 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) {
return num * num;
function square(num) {
return num * num;
}


// Call the function with a valid input
console.log(square(3)); // Outputs: 9
17 changes: 17 additions & 0 deletions week-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@
// 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 calculateBMI(weight, height) {
// Validate inputs: weight and height must be positive numbers
if (weight <= 0 || height <= 0 || isNaN(weight) || isNaN(height)) {
throw new Error("Invalid input: Weight and height must be positive numbers.");
}

// Calculate BMI
const bmi = weight / (height * height);

// Return BMI rounded to 1 decimal place
return parseFloat(bmi.toFixed(1));
}

// Example usage
console.log(calculateBMI(70, 1.73)); // Expected: 23.4
console.log(calculateBMI(80, 1.8)); // Expected: 24.7
console.log(calculateBMI(50, 1.6)); // Expected: 19.5
15 changes: 15 additions & 0 deletions week-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@

// Come up with a clear, simple name for the function
// Use the string documentation to help you plan your solution
function toUpperSnakeCase(input) {
// Step 1: Convert the string to uppercase
const upperCaseString = input.toUpperCase();

// Step 2: Replace spaces with underscores
const snakeCaseString = upperCaseString.replace(/ /g, "_");

// Step 3: Return the result
return snakeCaseString;
}

// Test Cases
console.log(toUpperSnakeCase("lord of the rings")); // Expected: "LORD_OF_THE_RINGS"
console.log(toUpperSnakeCase("the great gatsby")); // Expected: "THE_GREAT_GATSBY"
console.log(toUpperSnakeCase("the da vinci code")); // Expected: "THE_DA_VINCI_CODE"
15 changes: 15 additions & 0 deletions week-2/implement/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@
// 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
// Step 1: Declare the function
function toPounds(kilograms) {
// Step 2: Convert kilograms to pounds using the conversion factor
const pounds = kilograms * 2.20462;

// Step 3: Return the result, rounded to 2 decimal places
return pounds.toFixed(2);
}

// Step 4: Test the function with different inputs
console.log(toPounds(1)); // Expected: 2.20
console.log(toPounds(5)); // Expected: 11.02
console.log(toPounds(10)); // Expected: 22.05
console.log(toPounds(50)); // Expected: 110.23
console.log(toPounds(0)); // Expected: 0.00
19 changes: 19 additions & 0 deletions week-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,22 @@
// Given a number,
// When I call this function with a number
// Then it returns the new price with VAT added on
// Function to calculate price with VAT
function calculatePriceWithVAT(price) {
// Check if the input is valid (price should not be negative or missing)
if (price < 0) {
return "Error: Price must be a positive number!";
}

// Multiply the price by 1.2 to add 20% VAT
const newPrice = price * 1.2;

// Round the result to 2 decimal places
return newPrice.toFixed(2);
}

// Test the function
console.log(calculatePriceWithVAT(50)); // Should print "60.00"
console.log(calculatePriceWithVAT(100)); // Should print "120.00"
console.log(calculatePriceWithVAT(0)); // Should print "0.00"
console.log(calculatePriceWithVAT(-10)); // Should print an error message
6 changes: 6 additions & 0 deletions week-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@ console.log(formatTimeDisplay(143));
// Questions

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

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

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

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

// d) What is the value assigned to the parameter num when pad
// is called for the last time in this program? Explain your answer

// 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
//Use .padStart() to pad numbers more concisely.