-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
NW-6 | Zeliha Pala | JS1| [TECH ED] Complete week 2 exercises | WEEK-2 #165
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
function calculate(str) { | ||
//your code here... | ||
str = `+${str}`; | ||
str = str.replaceAll("plus", "+"); | ||
str = str.replaceAll("minus", "-"); | ||
let num; | ||
while (str.length > 0) { | ||
let lastMinus = str.lastIndexOf("-"); | ||
let lastPlus = str.lastIndexOf("+"); | ||
|
||
if (lastPlus > lastMinus) { | ||
num += Number(str.slice(lastPlus + 1)); | ||
str = str.slice(0, lastPlus); | ||
} else if (lastMinus > lastPlus) { | ||
num -= Number(str.slice(lastMinus + 1)); | ||
str = str.slice(0, lastMinus); | ||
} else { | ||
break; | ||
} | ||
} | ||
|
||
return num.toString(); | ||
} | ||
console.log(calculate("1plus2plus3plus4")); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
// 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)}`); | ||
//the console does not return any value without return , it will return undefined | ||
|
||
// If we want a function to return a value, we should use the return keyword. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
// Predict and explain first... | ||
//The semicolon (;) following the return statement causes an immediate termination of the function. This semicolon prevents the continuation of expressions within the block and disregards the rest of the function. | ||
|
||
function sum(a, b) { | ||
return; | ||
a + b; | ||
//return; a + b; } so it will be like this | ||
return a + b; | ||
} | ||
|
||
console.log(`The sum of 10 and 32 is ${sum(10, 32)}`); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,31 @@ | ||
// Predict and explain first... | ||
|
||
const num = 103; | ||
//Constant (const) declaration of 103 | ||
//return converts the value of the num variable to a string (using the toString() method), | ||
// console.log gives last digit of any number. | ||
|
||
// This program should tell the user the last digit of each number... | ||
// Explain why getLastDigit is not working properly - correct the problem... | ||
|
||
/* The issue is that the function is not using the parameter passed to it. It always returns the last digit of the constant num, which is set to 103. The function should take a parameter and return the last digit of that parameter. */ | ||
|
||
/* | ||
|
||
const num = 103; | ||
function getLastDigit() { | ||
return num.toString().slice(-1); | ||
} | ||
|
||
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)}`); | ||
console.log(`The last digit of 806 is ${getLastDigit(806)}`); */ | ||
|
||
//here is my solution ; | ||
|
||
// This program should tell the user the last digit of each number. | ||
// Explain why getLastDigit is not working properly - correct the problem | ||
function getLastDigit(number) { | ||
return number.toString().slice(-1); | ||
} | ||
|
||
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)}`); | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
/* Answer | ||
there's a syntax error in the function definition. Function parameters should be variables, not specific values or numbers. In this case, 3 is being used as a parameter, which isn't allowed in JavaScript function declarations. | ||
|
||
// 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) { | ||
return num * num; | ||
To fix this, we should declare the function with a parameter representing the number we want to square. */ | ||
function square(num) { | ||
return num * num; | ||
} | ||
|
||
|
||
console.log(square(4)); | ||
console.log(square(3)); | ||
console.log(square(5)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,12 @@ | |
// 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) { | ||
const bmi = weight / (height * height); | ||
return parseFloat(bmi.toFixed(1)); | ||
// Round to 1 decimal place (parseFloat(bmi.toFixed(1)) takes the calculated BMI, rounds it to one decimal place as a string, and then converts it back to a floating-point number, ensuring that the BMI returned by the function has only one decimal place. This helps keep the BMI value precise to the specified decimal point.) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain why you used parseFloat here? I have not used it and that is why I am wondering what parseFloat would do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your review @HadikaMalik |
||
} | ||
console.log(calculateBMI(70, 1.73)); // Example input: weight 70kg, height 1.73m | ||
console.log(calculateBMI(56, 1.65)); | ||
console.log(calculateBMI(87, 1.63)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,21 @@ | |
|
||
// Come up with a clear, simple name for the function | ||
// Use the string documentation to help you plan your solution | ||
|
||
///answer | ||
|
||
function toUpperCamelCase(inputString) { | ||
// Split the input string by spaces | ||
const words = inputString.split(" "); | ||
|
||
// Capitalize each word and join them with underscores | ||
const upperCamelCase = words.map((word) => word.toUpperCase()).join("_"); | ||
|
||
return upperCamelCase; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you explain what .split on line 23 and .map on line 26 does? I want to understand thhis better to know what you were doing. |
||
// Test cases | ||
console.log(toUpperCamelCase("lord of the rings")); // "LORD_OF_THE_RINGS" | ||
console.log(toUpperCamelCase("the great gatsby")); // "THE_GREAT_GATSBY" | ||
console.log(toUpperCamelCase("the da vinci code")); // "THE_DA_VINCI_CODE" | ||
console.log(toUpperCamelCase("darkness of the night")); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,23 @@ | |
// 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 | ||
|
||
function toPounds(str) { | ||
const num = str.slice(0, -1).padStart(3, "0"); | ||
const pounds = num.slice(0, -2); | ||
const pence = num.slice(-2); | ||
return `£${pounds}.${pence}`; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is so neat and easily understandable. |
||
// Test cases | ||
console.log(toPounds("399p")); // Output: £3.99 | ||
console.log(toPounds("5p")); // Output: £0.05 | ||
console.log(toPounds("1094p")); // Output: £10.24 | ||
|
||
/* The toPounds function: | ||
Takes a string str representing an amount in pence. | ||
Removes the 'p' from the end of the string and ensures it has at least three digits by adding leading zeros if necessary. | ||
Separates the first part as pounds and the last two digits as pence. | ||
Formats these values into the £X.YY (pounds and pence) format. | ||
Returns the result. | ||
The console.log statements demonstrate how the function converts different pence values into pounds and pence (£X.YY). Adjust the inputs to test other amounts! */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have explained this very well here. I understand what you wrote and did. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you