Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nehalkadyan/1ca2e32aafef1261df9c586c3d42201b to your computer and use it in GitHub Desktop.
Save nehalkadyan/1ca2e32aafef1261df9c586c3d42201b to your computer and use it in GitHub Desktop.
End Course Capstone Project - Module 1

End Course Capstone Project (Module 1)

Capstone Project Submission Instructions

  1. Login/Sign Up to your GitHub account.
  2. Fork this Gist from the Top-Right corner.
  3. Edit your Gist and paste/write the code solutions inside the respective functions.
  4. Share your Gist link in the submission form as per the instructions given.

- Do not make any changes in the first 3 lines

End.Course.Capstone.Project.Submission.Video.mp4
function calculateBMI(weight, height) {
// Write your code here
const bmi = weight / (height * height);
if (bmi < 18.5) {
return "Underweight";
} else if (bmi >= 18.5 && bmi < 24.9) {
return "Normal weight";
} else if (bmi >= 25 && bmi < 29.9) {
return "Overweight";
} else {
return "Obese";
}
}
// "&&" operator should be used here instead of "||"
// Do not modify the below lines
module.exports = { calculateBMI };
function convertTemperature(temperature, unit) {
// Write your code here
if (unit === "C") {
const fahrenheit = temperature * 9 / 5 + 32;
return fahrenheit.toFixed(2) + " F";
} else if (unit === "F") {
const celsius = (temperature - 32) * 5 / 9;
return celsius.toFixed(2) + " C";
} else {
return "Invalid unit. Use 'C' for Celsius or 'F' for Fahrenheit.";
}
}
// "/" sign should be used here instead of "%"
// Do not modify the below lines
module.exports = { convertTemperature };
function calculateTip(billAmount, tipPercentage) {
// Write your code here
const tipAmount = billAmount * tipPercentage;
const totalAmount = billAmount + tipAmount;
return Number(totalAmount.toFixed(2));
}
// Number() javaScript function starts with uppercase N instead of lowercase n.
// Do not modify the below lines
module.exports = { calculateTip };
function isPalindrome(str) {
// Write your code here
const cleanedStr = str.replace(/[^a-zA-Z0-9]/g, "");
const reversedStr = cleanedStr.split("").reverse().join("");
return cleanedStr.toLowerCase() === reversedStr.toLowerCase();
}
// "A-Z" was missing in the regex pattern, in the end we have used toLowerCase() method to lowercase all the letters of the string
// Do not modify the below lines
module.exports = { isPalindrome };
function countVowels(str) {
// Write your code here
const vowels = ['a', 'e', 'i', 'o', 'u'];
const lowerStr = str.toLowerCase();
let count = 0;
for (let i = 0; i < lowerStr.length; i++) {
if (vowels.includes(lowerStr[i])) {
count++;
}
}
return count;
}
// by replacing "<=" with "<", here we are saving one unnecessary iteration
// Do not modify the below lines
module.exports = { isPalindrome };
function findLongestWord(sentence) {
// Write your code here
const words = sentence.split(' ');
let maxLength = 0;
for (let word of words) {
const length = word.length;
if (length > maxLength) {
maxLength = length;
}
}
return maxLength;
}
// Do not modify the below lines
module.exports = { findLongestWord };
function titleCase(sentence) {
// write your code here
const result = [];
const words = sentence.toLowerCase().split(" ");
words.map((word) => {
word = word[0].toUpperCase() + word.substring(1)
result.push(word)
} )
return result.join(" ")
}
// Do not modify the below lines
module.exports = { titleCase };
function countOccurrences(str, char) {
// Write your code here
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) === char) {
count++;
}
}
return count;
}
// Do not modify the below lines
module.exports = { countOccurrences };
function calculateTotal(cart) {
// Write your code here
let total = cart.reduce((accumulator, currentElem) => {
return accumulator += currentElem.price * currentElem.quantity
}, 0)
return total;
}
// Do not modify the below lines
module.exports = { calculateTotal };
function fizzBuzz(n) {
const result = [];
for (let i = 1; i <= n; i++) {
switch (true) {
case i % 3 == 0 && i % 4 == 0:
result.push("FizzBuzz");
break;
case i % 3 == 0:
result.push("Fizz");
break;
case i % 4 == 0:
result.push("Buzz");
break;
default:
result.push(i.toString());
break;
}
}
return result;
}
// Do not modify the below lines
module.exports = { fizzBuzz };
module.exports = { findPrimes };
function findPrimes(n) {
// Write your code here
const result = [];
const isPrime = (num) => {
for(let i = 0; i < result.length && result[i] <= Math.sqrt(num); i++){
if(num % result[i] === 0){
return false;
}
}
return true
}
for(let i = 2; i < n; i++){
if(isPrime(i)){
result.push(i)
}
}
return result;
}
//Write a JavaScript function reverseString that takes a string as input and returns the string with its characters reversed.
module.exports = { reverseString };
function reverseString(str) {
// Write your code inside this function only.
return str.split("").reverse().join("");
}
/*Write a JavaScript function signOfProduct that takes an array of integers as input and returns the sign of the product of all the
integers in the array.
The function should return one of the following values:
1 if the product is positive.
-1 if the product is negative.
0 if the product is zero.
Implement the function efficiently, without actually computing the product.
*/
module.exports = { signOfProduct };
function signOfProduct(nums) {
// Write your code inside this function only.
let hasZero = false;
let negativeCount = 0;
for (let i = 0; i < nums.length; i++) {
if (nums[i] === 0) {
hasZero = true;
break;
} else if (nums[i] < 0) {
negativeCount++;
}
}
if (hasZero) {
return 0;
} else if (negativeCount === 0 || negativeCount % 2 === 0) {
return 1;
} else {
return -1;
}
}
module.exports = { checkSign };
function checkSign(num1, num2, num3){
if(num1 > 0 && num2 > 0 && num3 > 0){
return "+++";
}else if(num1 > 0 && num2 > 0 && num3 < 0 || num1 > 0 && num2 < 0 && num3 > 0 || num1 < 0 && num2 > 0 && num3 > 0){
return "++-"
}else if(num1 > 0 && num2 < 0 && num3 < 0 || num1 < 0 && num2 > 0 && num3 < 0 || num1 < 0 && num2 < 0 && num3 > 0){
return "+--"
}else if(num1 < 0 && num2 < 0 && num3 < 0){
return "---"
}
}
module.exports = { generateSlug };
function generateSlug(title) {
// Write your code inside this function only.
let result = ""
for(let i = 0; i < title.length; i++){
if(title[i] === " "){
result += "-";
}else{
result += title[i];
}
}
return result.toLowerCase() + ".com";
}
module.exports = { shortestDistance };
function shortestDistance(wordsDict, word1, word2) {
// Write your code inside this function only.
let min = Infinity;
let index1 = undefined;
let index2 = undefined;
for(let i = 0; i < wordsDict.length; i++){
if(wordsDict[i] === word1){
index1 = i;
}
if(wordsDict[i] === word2){
index2 = i;
}
if(index1 !== undefined && index2 !== undefined){
min = Math.min(min, Math.abs(index1 - index2))
}
}
return min;
}
module.exports = { findMove };
function findMove(s) {
// Write your code inside this function only.
const result = []
for(let i = 0; i < s.length-1; i++) { // ++-++
if(s[i] === "+" && s[i+1] ==="+") {
const str = s.slice(0, i) + "--" + s.slice(i+2)
result.push(str)
}
}
return result
}
/*Write a JavaScript function swapConsecutiveCharacters that takes a string as input and rearranges the consecutive characters
within the string in a swapped manner and returns a swapped string.
*/
module.exports = { swapConsecutiveCharacters };
function swapConsecutiveCharacters(str) {
// Write your code inside this function only.
let result = ""
for(let i = 0; i < str.length; i+= 2){ // "abcdef"
let temp = str[i];
result += str[i + 1];
result += temp;
}
return result;
}
/*Write a JavaScript function arrayIntersection that takes two arrays as input and returns a new array containing the elements
that are common in both arrays, without any duplicates.
*/
module.exports = { arrayIntersection };
function arrayIntersection(array1, array2) {
// Write your code inside this function only.
const numSet = new Set(array1);
const result = [];
for(let num of array2){
if(numSet.has(num) && !result.includes(num)){
result.push(num)
}
}
return result;
}
/*You are given a string consisting of characters, including spaces. Your task is to insert the character "7" immediately after every set of 6 characters from the start of the string, while ignoring spaces. The spaces should be skipped while counting the characters.
Write a JavaScript function insert7 that takes a string as input and returns the modified string after performing the required insertion.
*/
module.exports = { insert7 };
function insert7(inputString) {
// Write your code inside this function only.
let result = "";
let count = 0;
for(let i = 0; i < inputString.length; i++){
if(inputString[i] !== " "){
count += 1
result += inputString[i]
}else{
result += inputString[i]
}
if(count === 6){
result += "7"
count = 0;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment