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

Karla Grajales | Sprint-2 - Module-Structuring-and-Testing-Data | SPRINT 2 #237

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
571227a
doe 0.js
Grajales-K Nov 10, 2024
716d278
done 0.js
Grajales-K Nov 15, 2024
e307d6b
done 1.js
Grajales-K Nov 15, 2024
71fe9f0
done 2.js
Grajales-K Nov 16, 2024
555b848
done 0.js
Grajales-K Nov 27, 2024
4a48765
done 1.js
Grajales-K Nov 27, 2024
f77a0be
done 2.js
Grajales-K Nov 27, 2024
0f98c63
done format-time.js
Grajales-K Dec 19, 2024
6df687f
done bmi.js
Grajales-K Dec 19, 2024
4d95fee
update format-time.js
Grajales-K Dec 19, 2024
5de407c
removed space
Grajales-K Dec 20, 2024
c9f3f14
removed an empty line
Grajales-K Dec 20, 2024
bf67d4d
added the code from sprint1
Grajales-K Dec 20, 2024
055e578
done tp-pounds.js
Grajales-K Dec 20, 2024
12467cc
done tp-pounds.js
Grajales-K Dec 21, 2024
a40458c
Revert "done tp-pounds.js"
Grajales-K Dec 21, 2024
0a8af10
done vat.js
Grajales-K Dec 21, 2024
120c3f7
done cases.js
Grajales-K Dec 23, 2024
53b3219
Merge branch 'CodeYourFuture:main' into sprint-2
Grajales-K Dec 23, 2024
c0e52cb
done time-format.js
Grajales-K Dec 23, 2024
fb18a5f
Merge branch 'sprint-2' of github.com:Grajales-K/Module-Structuring-a…
Grajales-K Dec 23, 2024
e2929c2
update the response on my prediction on 2.js
Grajales-K Dec 23, 2024
a28e020
updated my prediction on errors, 0.js
Grajales-K Dec 23, 2024
21765d4
Fix bug in line 62 of cases.js that caused incorrect results in certa…
Grajales-K Dec 23, 2024
da212f6
Fix the variable money on line 56 to iterate with the function
Grajales-K Dec 23, 2024
639ccdb
removed line 50 empty space
Grajales-K Dec 23, 2024
ed9ed8f
addid some format to the comments lines 3 to 13
Grajales-K Dec 23, 2024
288cac7
Removed the the var num because cause and error reasingned another va…
Grajales-K Dec 23, 2024
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
3 changes: 3 additions & 0 deletions Sprint-1/interpret/to-pounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ console.log(`£${pounds}.${pence}`);

// To begin, we can start with
// 1. const penceString = "399p": initialises a string variable with the value "399p"



15 changes: 14 additions & 1 deletion Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
// Predict and explain first...

/*
the function won't return anything because we did not have any return so this will be undefine
it will print what is in the console but no in the second console because we are callin the function which will return undefine



function multiply(a, b) {
console.log(a * b);
}
*/
// ==================== this will return the arguments sent in the second console ====================

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


console.log(`The result of multiplying 10 and 32 is ${multiply(10, 32)}`);
17 changes: 16 additions & 1 deletion Sprint-2/debug/1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
// Predict and explain first...

function sum(a, b) {
/*
the reason why is returning undefined is because as soon the function find the return
will stop reading the rest. also the addition is ignoring because we have the statement
one line break after the return

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

*/



//=================== this will work ==============

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

console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);
39 changes: 36 additions & 3 deletions Sprint-2/debug/2.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
// Predict and explain first...

/*
- If we run this code. nothing will happen because the function is not called.

-once we invoke the function this will take the number and converted into string to manipulate and take the las digit and return the new string 42 => will return 2.

const num = 103;

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

//=========== This will work ==============
// This program should tell the user the last digit of each number.
// Explain why getLastDigit is not working properly - correct the problem


const num = 103;

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

console.log(`The last digit of 42 is ${getLastDigit(num)}`);
console.log(`The last digit of 42 is ${getLastDigit(42)}`);
console.log(`The last digit of 105 is ${getLastDigit(105)}`);
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


//=========== finding the middle digit ==============
// Index: 0 1 2 3 4 5 6 7 8 9 10 11 12
// Value: 2 0 5 5 5 6 4 2 3 2 5 6 9

function getMiddleDigit(num) {
const str = num.toString();
// console.log(str);

const middleIndex = Math.floor(str.length / 2); // 13/2 = 6.5 math.floor round the number to the nearest integer 6.
// console.log(middleIndex);

return str[middleIndex]; //we can access to the position 6 which contains the value '4'
}

console.log(`The middle of 105 is ${getMiddleDigit(2055564232569)}`)
40 changes: 37 additions & 3 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
// Predict and explain first...

// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring
// interpret the error message and figure out why an error occurring

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;

/*
- Occur SyntaxError because the var str has been declared in the in the parameter, so to fix this we
just need to assign the new value. and remove the let.

- Then call the function
*/


// function capitalize(str) {
// let str = `${str[0].toUpperCase()}${str.slice(1)}`;
// return str;
// }

// console.log(capitalize("alejandra"));



/*================== fixed =======================*/

function capitalize(str) {
str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
}

console.log(capitalize("alejandra"));


/*
In this function, we are taking a string parameter, "alejandra", as input.
Inside the function, we modify the string by capitalizing the first character.
- First, we access the first character of the string using `str[0]` and use the `toUpperCase()` method
to convert it to a capital letter.
- Then, we use `str.slice(1)` to extract the rest of the string, starting from the second character.
- Finally, we combine the capitalized first letter and the rest of the string using a template literal.

The result is for the input "alejandra", the output is "Alejandra".
*/
32 changes: 27 additions & 5 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
// Predict and explain first...

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

function convertToPercentage(decimalNumber) {
const decimalNumber = 0.5;
Why will an error occur when this program runs?
Try playing computer with the example to work out what is going on

1. The function won't run because we are not invoking the function,
2. we have been declared one variable with const and this can not reassigned after, const decimalNumber also has a fixed value
3. To work efficiently we should assign the parameter =+ inside the function and declare the var decimalNumber
4. we call the function correctly with its respective argument

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

return percentage;
}

console.log(decimalNumber);
*/


/*================== fixed =======================*/

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

return percentage;
}

console.log(decimalNumber);
console.log(convertToPercentage(.2)); //20%


24 changes: 23 additions & 1 deletion Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,30 @@

// this function should square any number but instead we're going to get an error

function square(3) {

/*
parameter are in the function, in this example we are not receiving any parameter and this we can not write the
argument directly,

arguments are in the console.log and we are not calling the function so that's why we are not having any response.

function square(3) {
return num * num;

}

*/

/*================== fixed =======================*/


function square(num) {
return num * num;
}

console.log(square(7))





109 changes: 98 additions & 11 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,111 @@
// This is the latest solution to the problem from the prep.
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
/*
This is the latest solution to the problem from the prep.
Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.

function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
console.log(hours);
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}

*/

// ====================== function and cases =======================


// time.slice(0, 2): method that return the positions 0 and 1 of the string,
// const hours = Number(time.slice(0, 2)) will convert the substring in numbers to manipulate and performs calculations.


function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
const hours = Number(time.slice(0, 2)); //Extracts characters from position 0 to 2 (not including the character at index 2)
const minutes = time.slice(3, 5); // extract characters from position 3 to 5 (for minutes part)

if (hours === 0) {
return `12:${minutes} am`;
} else if (hours === 12) {
return `12:${minutes} pm`;
} else if (hours > 12) {
return `${hours - 12}:${minutes} pm`;
} else {
return `${hours}:${minutes} am`;
}
return `${time} am`;
//the previous conditions always lead to a return
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";

// ============================= test ===============================

const currentOutput = formatAs12HourClock("00:42");
const targetOutput = "12:42 am";
console.assert(
currentOutput === targetOutput,
`current output: ${currentOutput}, target output: ${targetOutput}`
);

const currentOutput2 = formatAs12HourClock("23:00");
const targetOutput2 = "11:00 pm";

// ============================= test 1 =============================

const currentOutput1 = formatAs12HourClock("00:00");
const targetOutput1 = "12:00 am";
console.assert(
currentOutput1 === targetOutput1,
`current output: ${currentOutput1}, target output: ${targetOutput1}`
);


// ============================= test 2 failed ======================

const currentOutput2 = formatAs12HourClock("26:00"); //26 > 14; go in the second else if, 26-12=14
const targetOutput2 = "10:00 pm";

console.assert(
currentOutput2 === targetOutput2,
`current output: ${currentOutput2}, target output: ${targetOutput2}`
`current output time: ${currentOutput2}, target output: ${targetOutput2}`
//will print Assertion failed: current output time: 14:00 pm, target output: 10:00 pm
);



const testCases = [
{ input: "00:00", expected: "12:00 am" },
{ input: "12:00", expected: "12:00 pm" },
{ input: "15:30", expected: "3:30 pm" },
{ input: "08:05", expected: "8:05 am" },
{ input: "23:59", expected: "11:59 pm" },
];

for (const { input, expected } of testCases) {
const result = formatAs12HourClock(input);
console.assert(
result === expected,
`Input: ${input} | Expected: ${expected}, but got: ${result}`
);
}

// ================ icons test with console/log ========================

const testCasesIcons = [
{ input: "25:00", expected: "12:00 am" }, //test failed
{ input: "12:00", expected: "12:00 pm" },
{ input: "15:30", expected: "3:30 pm" },
{ input: "08:05", expected: "8:05 am" },
{ input: "23:59", expected: "11:59 pm" },
];


for (const { input, expected } of testCasesIcons) {
const result = formatAs12HourClock(input);
if (result === expected) {
console.log(`✅ Test passed! Input: ${input} | Output: ${result}`);
} else {
console.error(
`❌ Test failed! Input: ${input} | Expected: ${expected}, but got: ${result}`
);
}
}


34 changes: 34 additions & 0 deletions Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,37 @@
// Given someone's weight in kg and height in metres
// Then when we call this function with the weight and height
// It should return their Body Mass Index to 1 decimal place

// ============================= BMI TEST ===============================

function bodyMassIndex(weight, height) {
const kilograms = Number(weight);
const metres = Number(height);

if (kilograms > 0 && metres > 0) {
const bmi = kilograms / metres ** 2;
return Math.round(bmi * 10) / 10;
}
return "Invalid input. Please enter valid weight and height.";
}

//Avoid .toFixed() if we need a number (as .toFixed() returns a string).
console.log(bodyMassIndex(70, 0));


/*
Dynamic Precision with Math.round()
Combining Math.pow() with Math.round() offers a dynamic solution for those
seeking a more flexible approach that can adapt to rounding to various decimal
places.

function roundTo(num, precision) {
const factor = Math.pow(10, precision)
return Math.round(num * factor) / factor
}
console.log(roundTo(4.687, 0)); // Output: 5
console.log(roundTo(4.687, 1)); //one decimal place
console.log(roundTo(4.687, 2)); //two decimal places
console.log(roundTo(4.687, 3)); // Output: 4.687

*/
Loading