Skip to content

Commit f20d6a1

Browse files
authored
Merge pull request #3 from SueMsOl/MSATD-sprint2
sprint 3 exercises
2 parents 8453583 + 557660c commit f20d6a1

28 files changed

+7850
-36
lines changed

Sprint-2/debug/0.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Predict and explain first...
22

33
function multiply(a, b) {
4-
console.log(a * b);
4+
let multiplyOutput = Math.abs(a*b);
5+
return multiplyOutput;
56
}
67

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

Sprint-2/debug/1.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Predict and explain first...
22

33
function sum(a, b) {
4-
return;
5-
a + b;
4+
return a+b;
65
}
76

87
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`);

Sprint-2/debug/2.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Predict and explain first...
22

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

5-
function getLastDigit() {
6-
return num.toString().slice(-1);
5+
function getLastDigit(num) {
6+
let lastDigit = num.toString().slice(-1);
7+
return lastDigit;
78
}
89

910
console.log(`The last digit of 42 is ${getLastDigit(42)}`);

Sprint-2/errors/0.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// call the function capitalise with a string input
44
// interpret the error message and figure out why an error is occurring
55

6-
function capitalise(str) {
7-
let str = `${str[0].toUpperCase()}${str.slice(1)}`;
8-
return str;
6+
function capitalise(x) {
7+
let z = `${x[0].toUpperCase()}${x.slice(1)}`; //inja age + bezaram toye output mishe + lol
8+
return z;
99
}
10+
/*khob error chi bood? i gave variable x to be my parameter. so now inside the function, if i want to create a new
11+
variable, it needs a new name! */
12+
console.log(capitalise("moein"));

Sprint-2/errors/1.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@
33
// Why will an error occur when this program runs?
44
// Try playing computer with the example to work out what is going on
55

6-
function convertToPercentage(decimalNumber) {
7-
const decimalNumber = 0.5;
6+
/* function convertToPercentage(decimalNumber) {
7+
decimalNumber = 0.5; //remove this!
88
const percentage = `${decimalNumber * 100}%`;
99
1010
return percentage;
1111
}
1212
13-
console.log(decimalNumber);
13+
console.log(decimalNumber); //it must become console.log(convertToPercentage(0.25)); */
14+
//correct version is written down:
15+
16+
function convertToPercentage(decimalNumber) {
17+
18+
const percentage = `${decimalNumber * 100}%`;
19+
20+
return percentage;
21+
}
22+
23+
console.log(convertToPercentage(0.8));

Sprint-2/errors/2.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

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

6-
function square(3) {
6+
/*function square(3) { //put a variable here for parameter! in this case, name in 'num'
7+
return num * num;
8+
} */
9+
//correct version is written below:
10+
function square(num) {
711
return num * num;
812
}
9-
13+
console.log(square(87));
1014

Sprint-2/extend/format-time.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
// This is the latest solution to the problem from the prep.
22
// 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.
33

4+
/* this function does not cover edge cases and minutes.
45
function formatAs12HourClock(time) {
56
const hours = Number(time.slice(0, 2));
67
if (hours > 12) {
78
return `${hours - 12}:00 pm`;
89
}
910
return `${time} am`;
1011
}
12+
*/
13+
//this function below is an updated, advanced version of the function above.
14+
function formatAs12HourClock(time) {
15+
let converted = Number(time.slice(0,2));
16+
let minutes = time.slice(-3);
17+
//consider special cases first.
18+
if (converted === 0 ) {
19+
return "00:00 midnight";
20+
}
21+
else if (converted === 24) {
22+
let specialCase24 = 24-12;
23+
return `${specialCase24}${minutes} am`
24+
25+
}
26+
else if (converted === 12) {
27+
return `${converted}${minutes} pm`;
28+
}
29+
else if (converted>12){
30+
let above12 = converted-12;
31+
return `${above12}${minutes} pm`;
32+
}
33+
else{
34+
let below12 = converted.toString().padStart(2,"0");
35+
return `${below12}${minutes} am`;}
36+
}
1137

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

Sprint-2/implement/bmi.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Below are the steps for how BMI is calculated
22

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

55
// 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:
66

@@ -13,3 +13,27 @@
1313
// Given someone's weight in kg and height in metres
1414
// Then when we call this function with the weight and height
1515
// It should return their Body Mass Index to 1 decimal place
16+
function BMI(weight,height){
17+
let heightSquared = height*height;
18+
let BMICalc = (weight/heightSquared).toFixed(1);
19+
return `Your BMI is:${BMICalc}`;
20+
}
21+
console.log(BMI(65,1.72));
22+
//what if i wanted to ask questions first? prompt is not working here. i need something more complex named readline ...
23+
function printManyTimes(str) {
24+
"use strict";
25+
26+
const SENTENCE = str + " is cool!";
27+
let output = ""; // Store the full output here
28+
29+
for (let i = 0; i < str.length; i += 2) {
30+
output += SENTENCE + "<br>"; // Adds each sentence with a line break
31+
}
32+
33+
// Find the element with id 'output' and set its HTML content
34+
document.getElementById("output").innerHTML = output;
35+
}
36+
37+
// Call the function
38+
printManyTimes("freeCodeCamp");
39+

Sprint-2/implement/cases.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,30 @@
1313

1414
// You will need to come up with an appropriate name for the function
1515
// Use the string documentation to help you find a solution
16+
/*function convertToUpperSnakeCase(str){
17+
let x = str[0].toUpperCase()+str.slice(1);
18+
return x;
19+
}
20+
console.log(convertToUpperSnakeCase("sue and moein the best older brother")); */
21+
/* function convertToUpperSnakeCase(str){
22+
let x = str.split(" ");
23+
let upperX = x[0].charAt(0).toUpperCase();
24+
return upperX; // output: S
25+
}
26+
console.log(convertToUpperSnakeCase("sue and moein the best older brother")); */
27+
function convertToUpperSnakeCase(str){
28+
let x = str.replaceAll(" ","_");
29+
let upperX = x.toUpperCase();
30+
return upperX;
31+
}
32+
console.log(convertToUpperSnakeCase("sue and moein the best older brother"));
33+
//a function to turn "sue and moein the best older brother" to "Sue And Moein The Best Brother In The World":
34+
function capitaliseFirstLetter(sentence){
35+
let x = sentence.split(" "); // Split the sentence into an array of words
36+
let y= x.map(word => {
37+
return word[0].toUpperCase() + word.slice(1).toLowerCase();
38+
})
39+
let z = y.join(" ") // Join the words back into a single string
40+
return z;
41+
}
42+
console.log(capitaliseFirstLetter("sue and moein the best older brother"));

Sprint-2/implement/pounds.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// In Sprint-1, there is a program written in interpret/to-pounds.js
2+
3+
// You will need to take this code and turn it into a reusable block of code.
4+
// You will need to declare a function called toPounds with an appropriately named parameter.
5+
6+
// You should call this function a number of times to check it works for different inputs.
7+
/* function toPounds(penceString) { //example: 123p
8+
let removeP = penceString.slice(0,penceString.length-1); // output: 123
9+
let getPoundPart = removeP.slice(0,removeP.length-2); //output: 1
10+
let getPencePart = removeP.slice(-2); //23
11+
return `${getPoundPart}.${getPencePart} pound`;
12+
}
13+
console.log(toPounds("123p"));
14+
// bug for 23p!. needs padding
15+
function toPounds2(penceString) { //example: 23p
16+
let removeP = penceString.slice(0,penceString.length-1); // output: 23
17+
let getPoundPart = removeP.slice(0,removeP.length-2).padStart(1,"0"); //it was output: empty before padding. after: 0
18+
let getPencePart = removeP.slice(-2); //23
19+
return `${getPoundPart}.${getPencePart} pound`;
20+
21+
}
22+
console.log(toPounds("23p")); */
23+
24+
//most updated version with fixed bugs:
25+
function toPounds3(penceString) { //example: 1p
26+
let removeP = penceString.slice(0,penceString.length-1); // output: 1
27+
let getPoundPart = removeP.slice(0,removeP.length-2).padStart(1,"0"); //output: 0
28+
let getPencePart = removeP.slice(-2).padStart(2,"0"); //it was output:1 before padding. after: 01
29+
return `${getPoundPart}.${getPencePart} pound`;
30+
31+
32+
}
33+
console.log(toPounds3("1p"));

Sprint-2/implement/to-pounds.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

Sprint-2/implement/vat.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@
88
// Given a number,
99
// When I call this function with a number
1010
// it returns the new price with VAT added on
11+
function priceWithVat(price){
12+
let vatPrice = price*1.2;
13+
return ${vatPrice}`;
14+
}
15+
const targetGoal = "£60";
16+
const currentGoal = priceWithVat(50);
17+
console.assert(targetGoal===currentGoal, "try again!");

Sprint-2/interpret/time-format.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ function formatTimeDisplay(seconds) {
1818

1919
// Questions
2020

21-
// a) When formatTimeDisplay is called how many times will pad be called?
21+
// a) When formatTimeDisplay is called how many times will pad be called? 3
2222

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

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

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

29-
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
29+
// 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
3030

31-
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
31+
// 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
32+
//on which padStart is executed and as a result, a 0 is added to its start.

Sprint-3/implement/example.test.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//output testing for get-angle-type function:
2+
function getAngleType(angle) {
3+
let angleConverted = Number(angle.slice(0,-8));
4+
if (angleConverted === 90 ){
5+
return "Right angle";
6+
}
7+
else if (angleConverted === 180) {
8+
return "Straight angle";
9+
}
10+
else if (angleConverted < 90 ) {
11+
return "Acute angle";
12+
}
13+
else if (angleConverted > 90 && angleConverted < 180){
14+
return "Obtuse angle";
15+
}
16+
else if (angleConverted > 180 && angleConverted < 360){
17+
return "Reflex angle";
18+
}
19+
}
20+
test ('obtuse angle', () => {
21+
expect(getAngleType("120 degrees")).toBe('Obtuse angle');
22+
});
23+
//assertion check for get-card-value function:
24+
function getCardValue(rankSuit) {
25+
let rankPart = rankSuit.slice(0,rankSuit.length-1);
26+
if (rankPart >= 2 && rankPart <= 9){
27+
return rankPart;
28+
}
29+
else if(["10", "J", "Q", "K"].includes(rankPart)){
30+
return 10;
31+
}
32+
else if(rankPart === "A"){
33+
return 11;
34+
}
35+
else{
36+
return "Invalid card rank";
37+
}
38+
}
39+
test (`expected output is 10`, () => {
40+
expect(getCardValue("10♠")).toBe(10);
41+
});
42+
// assertion check for is-proper-fraction function:
43+
function isProperFraction(fraction) {
44+
let fractionArray = fraction.split("/");
45+
//position special case at the beginning of condition.
46+
if (fractionArray[1] == 0){
47+
return "Error";
48+
}
49+
else if (Math.abs(fractionArray[0]) < fractionArray[1]){
50+
return true;
51+
}
52+
else if (Math.abs(fractionArray[0]) >= fractionArray[1] ){
53+
return false;
54+
}
55+
}
56+
test ('expected output is error', () => {
57+
expect(isProperFraction("5/0")).toBe('Error');
58+
});
59+
// assertion check for is-valid-triangle:
60+
function isValidTriangle(a,b,c){
61+
let sum_V1 = a+b;
62+
let sum_V2 = a+c;
63+
let sum_v3 = b+c;
64+
if (sum_V1 > c && sum_V2 > b && sum_v3 > a) {
65+
return true;
66+
}
67+
else {
68+
return false
69+
}
70+
}
71+
test ('expected output is true', () =>{
72+
expect(isValidTriangle(1,7,7)).toBe(true);
73+
})
74+
test ('expected output is false', () =>{
75+
expect(isValidTriangle(1,2,7)).toBe(false);
76+
})
77+
//assertion check for rotate-char:
78+
function rotateCharacter(letter,shift){
79+
if (letter >= 'a' && letter <= 'z'){
80+
let lowerCase = ((letter.charCodeAt(0) - 'a'.charCodeAt(0) + shift) % 26) + 'a'.charCodeAt(0);
81+
return String.fromCharCode(lowerCase);
82+
}
83+
else if (letter >= 'A' && letter <= 'Z'){
84+
let upperCase = ((letter.charCodeAt(0) - 'A'.charCodeAt(0) + shift) % 26) + 'A'.charCodeAt(0);
85+
return String.fromCharCode(upperCase);
86+
}
87+
else {
88+
return letter;
89+
}
90+
}
91+
test ('expected output is f', () => {
92+
expect(rotateCharacter('b',4)).toBe('f');
93+
})
94+
test ('expected output is K', () => {
95+
expect(rotateCharacter('H',3)).toBe('K');
96+
})
97+
test ('expected output is E', () => {
98+
expect(rotateCharacter('Z',5)).toBe('E');
99+
})

Sprint-3/implement/get-angle-type.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,22 @@
2525
// Identify Reflex Angles:
2626
// When the angle is greater than 180 degrees and less than 360 degrees,
2727
// Then the function should return "Reflex angle"
28+
function getAngleType(angle) {
29+
let angleConverted = Number(angle.slice(0,-8));
30+
if (angleConverted === 90 ){
31+
return "Right angle";
32+
}
33+
else if (angleConverted === 180) {
34+
return "Straight angle";
35+
}
36+
else if (angleConverted < 90 ) {
37+
return "Acute angle";
38+
}
39+
else if (angleConverted > 90 && angleConverted < 180){
40+
return "Obtuse angle";
41+
}
42+
else if (angleConverted > 180 && angleConverted < 360){
43+
return "Reflex angle";
44+
}
45+
}
46+
console.log(getAngleType("120 degrees"));

0 commit comments

Comments
 (0)