Skip to content

sprint 3 exercises #3

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

Merged
merged 33 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
17c0a54
fixed 0.js error in errors dir
SueMsOl Nov 1, 2024
7a5a029
fixed error in 1.js file in errors dir
SueMsOl Nov 1, 2024
0110569
fixed error in 2.js file in errors dir
SueMsOl Nov 1, 2024
f56ab0a
fixed code in 0.js file in debug dir
SueMsOl Nov 1, 2024
bd89557
debugged 1.js file in debug dir
SueMsOl Nov 1, 2024
4ddc809
debugged 2.js file on debug dir
SueMsOl Nov 1, 2024
7937b18
debugged format-time.js file on extend dir
SueMsOl Nov 1, 2024
415d9bd
created a function for bmi.js in implement dir
SueMsOl Nov 1, 2024
576cf92
finished bmi function
SueMsOl Nov 3, 2024
f3e6bd0
finished cases function in cases.js
SueMsOl Nov 4, 2024
9e91758
finished toPounds function in pounds.js
SueMsOl Nov 4, 2024
45405ca
made to-Pounds function code DRYER
SueMsOl Nov 4, 2024
78d023c
finished writing function in vat.js
SueMsOl Nov 4, 2024
33f05f9
finished time-formats.js exercise
SueMsOl Nov 4, 2024
b55b233
fixed toPounds function bug in pounds.js
SueMsOl Nov 4, 2024
5092bca
fixed bug in pounds.js
SueMsOl Nov 4, 2024
87cdd24
fixed all bugs on pounds.js
SueMsOl Nov 4, 2024
b7695a7
made cases.js code dryer
SueMsOl Nov 6, 2024
3ce6b53
updated toPounds3 function in pounds.js
SueMsOl Nov 6, 2024
df206b2
I have installed jest framework, and ran example.test.js to see if it…
SueMsOl Nov 7, 2024
56f1cf1
wrote function in get-angle-type file and tested it using JIST framework
SueMsOl Nov 7, 2024
2ac16f3
wrote function in get-card-value file and tested it using JIST framework
SueMsOl Nov 7, 2024
fef5b77
wrote function in is-proper-fraction file and tested it using JIST fr…
SueMsOl Nov 7, 2024
0e7e294
wrote function in is-valid-triangle file and tested it using JIST fra…
SueMsOl Nov 7, 2024
a063e7d
wrote function in rotate.char.js file and tested it using JIST framework
SueMsOl Nov 8, 2024
fca2b8d
installed JIST framework in implement directory inside revise directory
SueMsOl Nov 8, 2024
518b44d
wrote 6 different functions in password-validator.test.js for each co…
SueMsOl Nov 8, 2024
7876ea0
an unresolved bug in passValidChecker function in password-validator.…
SueMsOl Nov 8, 2024
0dbd853
wrote functions required in 3 files inside revise/implement directory
SueMsOl Nov 9, 2024
f2d4b55
wrote functions required in repeat.test.js file inside revise/impleme…
SueMsOl Nov 9, 2024
b78e1aa
wrote functions required in is-prime.test.js file inside revise/imple…
SueMsOl Nov 9, 2024
7324324
wrote functions required in password-validator.test.js file inside re…
SueMsOl Nov 9, 2024
557660c
wrote functions required in password-validator.test.js file inside re…
SueMsOl Nov 9, 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
5 changes: 3 additions & 2 deletions Sprint-2/debug/0.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Predict and explain first...

function multiply(a, b) {
console.log(a * b);
let multiplyOutput = Math.abs(a*b);
return multiplyOutput;
}

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

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

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

const num = 103;
// const num = 103; this variable is a universal one. function needs its local variable.

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

console.log(`The last digit of 42 is ${getLastDigit(42)}`);
Expand Down
9 changes: 6 additions & 3 deletions Sprint-2/errors/0.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// call the function capitalise with a string input
// interpret the error message and figure out why an error is occurring

function capitalise(str) {
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
return str;
function capitalise(x) {
let z = `${x[0].toUpperCase()}${x.slice(1)}`; //inja age + bezaram toye output mishe + lol
return z;
}
/*khob error chi bood? i gave variable x to be my parameter. so now inside the function, if i want to create a new
variable, it needs a new name! */
console.log(capitalise("moein"));
16 changes: 13 additions & 3 deletions Sprint-2/errors/1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
// 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;
/* function convertToPercentage(decimalNumber) {
decimalNumber = 0.5; //remove this!
const percentage = `${decimalNumber * 100}%`;

return percentage;
}

console.log(decimalNumber);
console.log(decimalNumber); //it must become console.log(convertToPercentage(0.25)); */
//correct version is written down:

function convertToPercentage(decimalNumber) {

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

return percentage;
}

console.log(convertToPercentage(0.8));
8 changes: 6 additions & 2 deletions Sprint-2/errors/2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

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

function square(3) {
/*function square(3) { //put a variable here for parameter! in this case, name in 'num'
return num * num;
} */
//correct version is written below:
function square(num) {
return num * num;
}

console.log(square(87));

26 changes: 26 additions & 0 deletions Sprint-2/extend/format-time.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
// 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 function does not cover edge cases and minutes.
function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
if (hours > 12) {
return `${hours - 12}:00 pm`;
}
return `${time} am`;
}
*/
//this function below is an updated, advanced version of the function above.
function formatAs12HourClock(time) {
let converted = Number(time.slice(0,2));
let minutes = time.slice(-3);
//consider special cases first.
if (converted === 0 ) {
return "00:00 midnight";
}
else if (converted === 24) {
let specialCase24 = 24-12;
return `${specialCase24}${minutes} am`

}
else if (converted === 12) {
return `${converted}${minutes} pm`;
}
else if (converted>12){
let above12 = converted-12;
return `${above12}${minutes} pm`;
}
else{
let below12 = converted.toString().padStart(2,"0");
return `${below12}${minutes} am`;}
}

const currentOutput = formatAs12HourClock("08:00");
const targetOutput = "08:00 am";
Expand Down
26 changes: 25 additions & 1 deletion Sprint-2/implement/bmi.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Below are the steps for how BMI is calculated

// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) squared.
// The BMI calculation divides an adult's weight in kilograms (kg) by their height in metres (m) .

// For example, if you weigh 70kg (around 11 stone) and are 1.73m (around 5 feet 8 inches) tall, you work out your BMI by:

Expand All @@ -13,3 +13,27 @@
// 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
function BMI(weight,height){
let heightSquared = height*height;
let BMICalc = (weight/heightSquared).toFixed(1);
return `Your BMI is:${BMICalc}`;
}
console.log(BMI(65,1.72));
//what if i wanted to ask questions first? prompt is not working here. i need something more complex named readline ...
function printManyTimes(str) {
"use strict";

const SENTENCE = str + " is cool!";
let output = ""; // Store the full output here

for (let i = 0; i < str.length; i += 2) {
output += SENTENCE + "<br>"; // Adds each sentence with a line break
}

// Find the element with id 'output' and set its HTML content
document.getElementById("output").innerHTML = output;
}

// Call the function
printManyTimes("freeCodeCamp");

27 changes: 27 additions & 0 deletions Sprint-2/implement/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,30 @@

// You will need to come up with an appropriate name for the function
// Use the string documentation to help you find a solution
/*function convertToUpperSnakeCase(str){
let x = str[0].toUpperCase()+str.slice(1);
return x;
}
console.log(convertToUpperSnakeCase("sue and moein the best older brother")); */
/* function convertToUpperSnakeCase(str){
let x = str.split(" ");
let upperX = x[0].charAt(0).toUpperCase();
return upperX; // output: S
}
console.log(convertToUpperSnakeCase("sue and moein the best older brother")); */
function convertToUpperSnakeCase(str){
let x = str.replaceAll(" ","_");
let upperX = x.toUpperCase();
return upperX;
}
console.log(convertToUpperSnakeCase("sue and moein the best older brother"));
//a function to turn "sue and moein the best older brother" to "Sue And Moein The Best Brother In The World":
function capitaliseFirstLetter(sentence){
let x = sentence.split(" "); // Split the sentence into an array of words
let y= x.map(word => {
return word[0].toUpperCase() + word.slice(1).toLowerCase();
})
let z = y.join(" ") // Join the words back into a single string
return z;
}
console.log(capitaliseFirstLetter("sue and moein the best older brother"));
33 changes: 33 additions & 0 deletions Sprint-2/implement/pounds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// In Sprint-1, there is a program written in interpret/to-pounds.js

// You will need to take this code and turn it into a reusable block of code.
// You will need to declare a function called toPounds with an appropriately named parameter.

// You should call this function a number of times to check it works for different inputs.
/* function toPounds(penceString) { //example: 123p
let removeP = penceString.slice(0,penceString.length-1); // output: 123
let getPoundPart = removeP.slice(0,removeP.length-2); //output: 1
let getPencePart = removeP.slice(-2); //23
return `${getPoundPart}.${getPencePart} pound`;
}
console.log(toPounds("123p"));
// bug for 23p!. needs padding
function toPounds2(penceString) { //example: 23p
let removeP = penceString.slice(0,penceString.length-1); // output: 23
let getPoundPart = removeP.slice(0,removeP.length-2).padStart(1,"0"); //it was output: empty before padding. after: 0
let getPencePart = removeP.slice(-2); //23
return `${getPoundPart}.${getPencePart} pound`;

}
console.log(toPounds("23p")); */

//most updated version with fixed bugs:
function toPounds3(penceString) { //example: 1p
let removeP = penceString.slice(0,penceString.length-1); // output: 1
let getPoundPart = removeP.slice(0,removeP.length-2).padStart(1,"0"); //output: 0
let getPencePart = removeP.slice(-2).padStart(2,"0"); //it was output:1 before padding. after: 01
return `${getPoundPart}.${getPencePart} pound`;


}
console.log(toPounds3("1p"));
6 changes: 0 additions & 6 deletions Sprint-2/implement/to-pounds.js

This file was deleted.

7 changes: 7 additions & 0 deletions Sprint-2/implement/vat.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@
// Given a number,
// When I call this function with a number
// it returns the new price with VAT added on
function priceWithVat(price){
let vatPrice = price*1.2;
return `£${vatPrice}`;
}
const targetGoal = "£60";
const currentGoal = priceWithVat(50);
console.assert(targetGoal===currentGoal, "try again!");
11 changes: 6 additions & 5 deletions Sprint-2/interpret/time-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ function formatTimeDisplay(seconds) {

// Questions

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

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

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

// c) What is the return value of pad is called for the first time?
// c) What is the return value of pad is called for the first time? 00

// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
// d) What is the value assigned to num when pad is called for the last time in this program? 1, it shows th remaining second

// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
// e) What is the return value assigned to num when pad is called for the last time in this program? 01, it is the remaining second
//on which padStart is executed and as a result, a 0 is added to its start.
99 changes: 99 additions & 0 deletions Sprint-3/implement/example.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//output testing for get-angle-type function:
function getAngleType(angle) {
let angleConverted = Number(angle.slice(0,-8));
if (angleConverted === 90 ){
return "Right angle";
}
else if (angleConverted === 180) {
return "Straight angle";
}
else if (angleConverted < 90 ) {
return "Acute angle";
}
else if (angleConverted > 90 && angleConverted < 180){
return "Obtuse angle";
}
else if (angleConverted > 180 && angleConverted < 360){
return "Reflex angle";
}
}
test ('obtuse angle', () => {
expect(getAngleType("120 degrees")).toBe('Obtuse angle');
});
//assertion check for get-card-value function:
function getCardValue(rankSuit) {
let rankPart = rankSuit.slice(0,rankSuit.length-1);
if (rankPart >= 2 && rankPart <= 9){
return rankPart;
}
else if(["10", "J", "Q", "K"].includes(rankPart)){
return 10;
}
else if(rankPart === "A"){
return 11;
}
else{
return "Invalid card rank";
}
}
test (`expected output is 10`, () => {
expect(getCardValue("10♠")).toBe(10);
});
// assertion check for is-proper-fraction function:
function isProperFraction(fraction) {
let fractionArray = fraction.split("/");
//position special case at the beginning of condition.
if (fractionArray[1] == 0){
return "Error";
}
else if (Math.abs(fractionArray[0]) < fractionArray[1]){
return true;
}
else if (Math.abs(fractionArray[0]) >= fractionArray[1] ){
return false;
}
}
test ('expected output is error', () => {
expect(isProperFraction("5/0")).toBe('Error');
});
// assertion check for is-valid-triangle:
function isValidTriangle(a,b,c){
let sum_V1 = a+b;
let sum_V2 = a+c;
let sum_v3 = b+c;
if (sum_V1 > c && sum_V2 > b && sum_v3 > a) {
return true;
}
else {
return false
}
}
test ('expected output is true', () =>{
expect(isValidTriangle(1,7,7)).toBe(true);
})
test ('expected output is false', () =>{
expect(isValidTriangle(1,2,7)).toBe(false);
})
//assertion check for rotate-char:
function rotateCharacter(letter,shift){
if (letter >= 'a' && letter <= 'z'){
let lowerCase = ((letter.charCodeAt(0) - 'a'.charCodeAt(0) + shift) % 26) + 'a'.charCodeAt(0);
return String.fromCharCode(lowerCase);
}
else if (letter >= 'A' && letter <= 'Z'){
let upperCase = ((letter.charCodeAt(0) - 'A'.charCodeAt(0) + shift) % 26) + 'A'.charCodeAt(0);
return String.fromCharCode(upperCase);
}
else {
return letter;
}
}
test ('expected output is f', () => {
expect(rotateCharacter('b',4)).toBe('f');
})
test ('expected output is K', () => {
expect(rotateCharacter('H',3)).toBe('K');
})
test ('expected output is E', () => {
expect(rotateCharacter('Z',5)).toBe('E');
})
19 changes: 19 additions & 0 deletions Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,22 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
function getAngleType(angle) {
let angleConverted = Number(angle.slice(0,-8));
if (angleConverted === 90 ){
return "Right angle";
}
else if (angleConverted === 180) {
return "Straight angle";
}
else if (angleConverted < 90 ) {
return "Acute angle";
}
else if (angleConverted > 90 && angleConverted < 180){
return "Obtuse angle";
}
else if (angleConverted > 180 && angleConverted < 360){
return "Reflex angle";
}
}
console.log(getAngleType("120 degrees"));
Loading