From 36870c56a73c06d01cf9cbdc0e09025c7b9bc087 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Fri, 11 Aug 2023 17:29:57 +0300 Subject: [PATCH 01/23] Finished '3or5' from easy --- src/easy/3or5/plain.md | 4 ++++ src/easy/3or5/pseudo.md | 7 +++++++ src/easy/3or5/solution.js | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/easy/3or5/plain.md b/src/easy/3or5/plain.md index 73370f1..ae39bcc 100644 --- a/src/easy/3or5/plain.md +++ b/src/easy/3or5/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Starting from 1 going up to 1000, check if said number is multiple of 3 or 5. +If this is true then add this number to a sum. +When the process is done, return this sum. diff --git a/src/easy/3or5/pseudo.md b/src/easy/3or5/pseudo.md index 520841f..902563f 100644 --- a/src/easy/3or5/pseudo.md +++ b/src/easy/3or5/pseudo.md @@ -1 +1,8 @@ # Pseudo Code Solution + +```text +for each number from 1 to 1000 + if number is multiple of 3 or multiple of 5 + then increase sum by this number +return the sum +``` diff --git a/src/easy/3or5/solution.js b/src/easy/3or5/solution.js index e69de29..6282ec6 100644 --- a/src/easy/3or5/solution.js +++ b/src/easy/3or5/solution.js @@ -0,0 +1,11 @@ +function multOfThreeOrFive() { + let sum = 0 + for (let i = 0; i < 1000; i++) { + if (i % 3 === 0 || i % 5 === 0) { + sum += i + } + } + return sum +} + +console.log("The sum of all the multiples of 3 or 5 below 1000 is:", multOfThreeOrFive()) \ No newline at end of file From 6950d540d03471d3a69bf5a01ffd58e4a44af598 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Fri, 11 Aug 2023 17:48:07 +0300 Subject: [PATCH 02/23] Finished 'digitCount' from easy --- src/easy/digitCount/plain.md | 3 +++ src/easy/digitCount/pseudo.md | 6 ++++++ src/easy/digitCount/solution.js | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/src/easy/digitCount/plain.md b/src/easy/digitCount/plain.md index 73370f1..9ff1259 100644 --- a/src/easy/digitCount/plain.md +++ b/src/easy/digitCount/plain.md @@ -1 +1,4 @@ # Plain English Solution + +Keep dividing the number with 10 until it gets smaller than 1. +Digits of number are equal to the number of divisions we did. diff --git a/src/easy/digitCount/pseudo.md b/src/easy/digitCount/pseudo.md index 520841f..3e7eb48 100644 --- a/src/easy/digitCount/pseudo.md +++ b/src/easy/digitCount/pseudo.md @@ -1 +1,7 @@ # Pseudo Code Solution + +```text +while number is not less than 1 + divide number by 10 + increase digit count by 1 +``` diff --git a/src/easy/digitCount/solution.js b/src/easy/digitCount/solution.js index e69de29..4472a6b 100644 --- a/src/easy/digitCount/solution.js +++ b/src/easy/digitCount/solution.js @@ -0,0 +1,11 @@ +function digitCounter(num) { + let count = 0 + while(num >= 1){ + num /= 10 + count++ + } + return count +} + +const givenNum = 314890 +console.log('The number', givenNum, "has", digitCounter(givenNum), 'digits.') \ No newline at end of file From ae5a5342415f7de6847f180be800451f3fe51243 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Fri, 11 Aug 2023 17:58:27 +0300 Subject: [PATCH 03/23] Finished 'factorial' from easy --- src/easy/factorial/plain.md | 4 ++++ src/easy/factorial/pseudo.md | 8 ++++++++ src/easy/factorial/solution.js | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/easy/factorial/plain.md b/src/easy/factorial/plain.md index 73370f1..b3d3b33 100644 --- a/src/easy/factorial/plain.md +++ b/src/easy/factorial/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Recursively call a function to calculate the factorial. +Each time build up the product starting from the number and going down. +Stop when you reach the number 1, which is also the base case. diff --git a/src/easy/factorial/pseudo.md b/src/easy/factorial/pseudo.md index 520841f..5206356 100644 --- a/src/easy/factorial/pseudo.md +++ b/src/easy/factorial/pseudo.md @@ -1 +1,9 @@ # Pseudo Code Solution + +```text +Call function to calculate factorial of number + if number is 1 + then return 1 + else + return number * The factorial function with arguement number - 1 +``` diff --git a/src/easy/factorial/solution.js b/src/easy/factorial/solution.js index e69de29..537bba6 100644 --- a/src/easy/factorial/solution.js +++ b/src/easy/factorial/solution.js @@ -0,0 +1,10 @@ +function factorial(number) { + if (number === 1) { + return 1 + } else { + return number * factorial(number - 1) + } +} + +const givenNum = 5 +console.log('The factorial of', givenNum, 'is', factorial(givenNum)) \ No newline at end of file From ac6d2c84c79bb65a93f9df366601521da51dab2d Mon Sep 17 00:00:00 2001 From: iliask796 Date: Fri, 11 Aug 2023 18:24:41 +0300 Subject: [PATCH 04/23] Finished 'makeSentence' from easy --- src/easy/makeSentence/plain.md | 4 ++++ src/easy/makeSentence/pseudo.md | 7 +++++++ src/easy/makeSentence/solution.js | 22 ++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/easy/makeSentence/plain.md b/src/easy/makeSentence/plain.md index 73370f1..c61c577 100644 --- a/src/easy/makeSentence/plain.md +++ b/src/easy/makeSentence/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Get the first character of the sentence string and make it capital. +Check the last character of the sentence string. +If it is not some form of punctuation then add a full stop. diff --git a/src/easy/makeSentence/pseudo.md b/src/easy/makeSentence/pseudo.md index 520841f..defb03a 100644 --- a/src/easy/makeSentence/pseudo.md +++ b/src/easy/makeSentence/pseudo.md @@ -1 +1,8 @@ # Pseudo Code Solution + +```text +Get the first character of string +Capitalize it and join it with the string again +If string's last character is a letter + then add a full stop +``` diff --git a/src/easy/makeSentence/solution.js b/src/easy/makeSentence/solution.js index e69de29..2a94213 100644 --- a/src/easy/makeSentence/solution.js +++ b/src/easy/makeSentence/solution.js @@ -0,0 +1,22 @@ +function isLetter(char){ + //If more things are considered as punctuation, they should be added. + const nonLetters = ['.', ',', '?', '!', ';'] + for (let i = 0; i < nonLetters.length; i++) { + if (char === nonLetters[i]) { + return false + } + } + return true +} + +function makeSentence(str) { + //Considering given sentence always starts with a letter. + str = str[0].toUpperCase() + str.slice(1) + if (isLetter(str[str.length-1])) { + str += '.' + } + return str +} + +givenStr = 'hello mate' +console.log('Turned', givenStr,'to',makeSentence(givenStr)) \ No newline at end of file From e1c9bf94d36d60b1a874974dd3d64e794e63375a Mon Sep 17 00:00:00 2001 From: iliask796 Date: Fri, 11 Aug 2023 18:46:17 +0300 Subject: [PATCH 05/23] Finished 'miles' from easy --- src/easy/miles/plain.md | 3 +++ src/easy/miles/pseudo.md | 9 +++++++++ src/easy/miles/solution.js | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/easy/miles/plain.md b/src/easy/miles/plain.md index 73370f1..ee78f17 100644 --- a/src/easy/miles/plain.md +++ b/src/easy/miles/plain.md @@ -1 +1,4 @@ # Plain English Solution + +Use the 1.6:1 ratio to convert kilometers to miles. +Use Math built-in functions floor and ceiling to round the result. diff --git a/src/easy/miles/pseudo.md b/src/easy/miles/pseudo.md index 520841f..df3eac7 100644 --- a/src/easy/miles/pseudo.md +++ b/src/easy/miles/pseudo.md @@ -1 +1,10 @@ # Pseudo Code Solution + +```test +Divide kilometers by 1.6 to get result in miles +Calculate the remainder of miles with 1 +If the remainder is below 0.5 + then use Math.floor to round the result +Else + use Math.ceil to round the result +``` diff --git a/src/easy/miles/solution.js b/src/easy/miles/solution.js index e69de29..705db81 100644 --- a/src/easy/miles/solution.js +++ b/src/easy/miles/solution.js @@ -0,0 +1,13 @@ +function toMiles(kilometers) { + const miles = kilometers / 1.6 + remainder = miles % 1 + if (remainder < 0.5) { + return Math.floor(miles) + } else { + return Math.ceil(miles) + } + +} + +const givenNum = 101 +console.log(givenNum, 'kilometers equal to', toMiles(givenNum), 'miles.') \ No newline at end of file From f147394a4bfd65274ff7cbe5ac3f30118ceb21e3 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 00:08:27 +0300 Subject: [PATCH 06/23] Finished 'makeSentence' from easy --- src/easy/makeSentence/solution.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/easy/makeSentence/solution.js b/src/easy/makeSentence/solution.js index 2a94213..6defc43 100644 --- a/src/easy/makeSentence/solution.js +++ b/src/easy/makeSentence/solution.js @@ -18,5 +18,5 @@ function makeSentence(str) { return str } -givenStr = 'hello mate' +const givenStr = 'hello mate' console.log('Turned', givenStr,'to',makeSentence(givenStr)) \ No newline at end of file From 0dee01f0b9a50a41607d4a131e5d11575fab80d6 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 00:13:01 +0300 Subject: [PATCH 07/23] Finished 'milesTravelled' from easy --- src/easy/milesTravelled/plain.md | 4 ++++ src/easy/milesTravelled/pseudo.md | 10 ++++++++++ src/easy/milesTravelled/solution.js | 15 +++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/easy/milesTravelled/plain.md b/src/easy/milesTravelled/plain.md index 73370f1..91a96ee 100644 --- a/src/easy/milesTravelled/plain.md +++ b/src/easy/milesTravelled/plain.md @@ -1 +1,5 @@ # Plain English Solution + +We are going to convert the given minutes to hours by dividing them by 60. +Then we are going to multiply that with the speed of journey (miles/hour). +Lastly we are going to round that and return the answer. diff --git a/src/easy/milesTravelled/pseudo.md b/src/easy/milesTravelled/pseudo.md index 520841f..cdf7ece 100644 --- a/src/easy/milesTravelled/pseudo.md +++ b/src/easy/milesTravelled/pseudo.md @@ -1 +1,11 @@ # Pseudo Code Solution + +```text + Divide minutes by 60 to get hours + Multiply hours with speed + If the decimal part of the above result is lower than 0.5 + then use Math.floor to round + else + use Math.ceil to round + Return the result +``` diff --git a/src/easy/milesTravelled/solution.js b/src/easy/milesTravelled/solution.js index e69de29..7f3baf0 100644 --- a/src/easy/milesTravelled/solution.js +++ b/src/easy/milesTravelled/solution.js @@ -0,0 +1,15 @@ +function milesTravelled(minutes, speed) { + const hours = minutes / 60 + const distance = hours * speed + const tmp = distance % 1 + if (tmp < 0.5) { + return Math.floor(distance) + } + else { + return Math.ceil(distance) + } +} + +const givenDuration = 140 +const givenSpeed = 85 +console.log("Travelled distance equals to", milesTravelled(givenDuration,givenSpeed), 'miles.') \ No newline at end of file From 43f85355e4f3ea08ea0df1789b6dbe36b825853c Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 00:26:16 +0300 Subject: [PATCH 08/23] Finished 'multiples' from easy --- src/easy/multiples/plain.md | 3 +++ src/easy/multiples/pseudo.md | 9 +++++++++ src/easy/multiples/solution.js | 13 +++++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/easy/multiples/plain.md b/src/easy/multiples/plain.md index 73370f1..69bb6c3 100644 --- a/src/easy/multiples/plain.md +++ b/src/easy/multiples/plain.md @@ -1 +1,4 @@ # Plain English Solution + +For as many times as length equals to, start by pushing given number in an array. +Every next time add the number to itself to create the next multiple. diff --git a/src/easy/multiples/pseudo.md b/src/easy/multiples/pseudo.md index 520841f..578ada0 100644 --- a/src/easy/multiples/pseudo.md +++ b/src/easy/multiples/pseudo.md @@ -1 +1,10 @@ # Pseudo Code Solution + +```text + initialize tmp with zero + initialize an empty array + for i = 1 to length going up by 1 + add number to tmp + push into array the above result + return the array +``` diff --git a/src/easy/multiples/solution.js b/src/easy/multiples/solution.js index e69de29..8f54888 100644 --- a/src/easy/multiples/solution.js +++ b/src/easy/multiples/solution.js @@ -0,0 +1,13 @@ +function multiples(number, len) { + let tmp = 0 + const myArray = [] + for (let i = 1; i <= len; i++) { + tmp += number + myArray.push(tmp) + } + return myArray +} + +const givenNum = 12 +const givenLen = 10 +console.log('(', givenNum, ',', givenLen, ') -> ', multiples(givenNum, givenLen)) \ No newline at end of file From 707dd963dc48ccfc7781ba07a7442def16a58327 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 00:58:11 +0300 Subject: [PATCH 09/23] Finished 'random' from easy --- src/easy/random/plain.md | 5 +++++ src/easy/random/pseudo.md | 7 +++++++ src/easy/random/solution.js | 10 ++++++++++ 3 files changed, 22 insertions(+) diff --git a/src/easy/random/plain.md b/src/easy/random/plain.md index 73370f1..df3d5e2 100644 --- a/src/easy/random/plain.md +++ b/src/easy/random/plain.md @@ -1 +1,6 @@ # Plain English Solution + +We are going to use the computer's time to get the current milisecond. +Then we are going to calculate the remainder of that with the first given +number and add to that the difference of our two given numbers. +This way the random number is gonna be between the two given numbers' range. diff --git a/src/easy/random/pseudo.md b/src/easy/random/pseudo.md index 520841f..c902d58 100644 --- a/src/easy/random/pseudo.md +++ b/src/easy/random/pseudo.md @@ -1 +1,8 @@ # Pseudo Code Solution + +```text + Get current milisecond + Divide it with the first number and get the remainder + Add the difference of the two given numbers to the remainder + Return the remainder +``` diff --git a/src/easy/random/solution.js b/src/easy/random/solution.js index e69de29..c668dfa 100644 --- a/src/easy/random/solution.js +++ b/src/easy/random/solution.js @@ -0,0 +1,10 @@ +//Supposing num1 is the low threshold and num2 is the high threshold +function random(num1, num2) { + let ms = new Date() + ms = ms.getMilliseconds() % num1 + return ms + num2 - num1 +} + +const lowThreshold = 40 +const highThreshold = 100 +console.log('Random number between', lowThreshold, 'and', highThreshold, 'is', random(40, 100)) \ No newline at end of file From 5ba5211805fa8e36613336528142de6375ab99b3 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 01:58:41 +0300 Subject: [PATCH 10/23] Finished range from easy --- src/easy/range/plain.md | 5 +++++ src/easy/range/pseudo.md | 10 ++++++++++ src/easy/range/solution.js | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/easy/range/plain.md b/src/easy/range/plain.md index 73370f1..4b30632 100644 --- a/src/easy/range/plain.md +++ b/src/easy/range/plain.md @@ -1 +1,6 @@ # Plain English Solution + +We are going to set the first item of the list as both the highest and +the lowest number. +Then we are going to iterate the list and update the highest and lowest value. +Lastly we are going to return the difference between them. diff --git a/src/easy/range/pseudo.md b/src/easy/range/pseudo.md index 520841f..892a690 100644 --- a/src/easy/range/pseudo.md +++ b/src/easy/range/pseudo.md @@ -1 +1,11 @@ # Pseudo Code Solution + +```text + Set the first item of list to min and max + For i = 0 to list.length + if item > max + then max = item + if item < min + then min = item + Return max - min +``` diff --git a/src/easy/range/solution.js b/src/easy/range/solution.js index e69de29..dfab603 100644 --- a/src/easy/range/solution.js +++ b/src/easy/range/solution.js @@ -0,0 +1,18 @@ +function range(numList) { + let min = numList[0] + let max = numList[0] + numList.forEach( + (listItem) => { + if (listItem > max) { + max = listItem + } + if (listItem < min) { + min = listItem + } + } + ) + return max - min +} + +const givenList = [10, 5, 7, 20, 3, 69, 22, -5, 75] +console.log('The range of given list is', range(givenList)) \ No newline at end of file From ee145dccc238a097885f6fcb7fec46ace6ba54aa Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 16:53:37 +0300 Subject: [PATCH 11/23] Finished 'repeat' from easy --- src/easy/repeat/plain.md | 3 +++ src/easy/repeat/pseudo.md | 8 ++++++++ src/easy/repeat/solution.js | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/easy/repeat/plain.md b/src/easy/repeat/plain.md index 73370f1..1e66f4c 100644 --- a/src/easy/repeat/plain.md +++ b/src/easy/repeat/plain.md @@ -1 +1,4 @@ # Plain English Solution + +Use the current string to build a new one. Traverse each letter of the string. +Use this letter twice and concentate them to create the required string. diff --git a/src/easy/repeat/pseudo.md b/src/easy/repeat/pseudo.md index 520841f..15881d2 100644 --- a/src/easy/repeat/pseudo.md +++ b/src/easy/repeat/pseudo.md @@ -1 +1,9 @@ # Pseudo Code Solution + +```text + Initialize a new empty string + for i = 0 to given string.length + Get the character of string at pos i + Concentate two copies of that to the new string + return the new string +``` diff --git a/src/easy/repeat/solution.js b/src/easy/repeat/solution.js index e69de29..6c1e772 100644 --- a/src/easy/repeat/solution.js +++ b/src/easy/repeat/solution.js @@ -0,0 +1,10 @@ +function repeat(str) { + let result = "" + for (let i = 0; i < str.length; i++) { + result += str.charAt(i) + str.charAt(i) + } + return result +} + +const givenStr = '1234!_ ?' +console.log(givenStr, '->', repeat(givenStr)) \ No newline at end of file From c5f5ca9002b65e2ef7589e6825918839e6315bdc Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 17:16:27 +0300 Subject: [PATCH 12/23] Finished 'reverse' from easy --- src/easy/reverse/plain.md | 4 ++++ src/easy/reverse/pseudo.md | 7 +++++++ src/easy/reverse/solution.js | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/easy/reverse/plain.md b/src/easy/reverse/plain.md index 73370f1..3d2142f 100644 --- a/src/easy/reverse/plain.md +++ b/src/easy/reverse/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Based on the examples, supposing the problem wanted to say reverse a "string" and not a "list" then: +Create a new empty string. Traverse the given string from the end to the start. +Concatenate each letter to the new string in order to create the result. diff --git a/src/easy/reverse/pseudo.md b/src/easy/reverse/pseudo.md index 520841f..5d35e19 100644 --- a/src/easy/reverse/pseudo.md +++ b/src/easy/reverse/pseudo.md @@ -1 +1,8 @@ # Pseudo Code Solution + +```text + Initialize a new empty string + For i = given str.length - 1 to 0 going backwards + Concatenate the character at position i with the new string + Return the result +``` diff --git a/src/easy/reverse/solution.js b/src/easy/reverse/solution.js index e69de29..21c9fbf 100644 --- a/src/easy/reverse/solution.js +++ b/src/easy/reverse/solution.js @@ -0,0 +1,10 @@ +function reverse(str) { + let result = "" + for (let i = str.length-1; i >= 0; i--) { + result += str.charAt(i) + } + return result +} + +const givenStr = 'string' +console.log(givenStr, '->', reverse(givenStr)) \ No newline at end of file From 7ef049ce8c1016c24125113bc38cd0a481fba35d Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 17:24:14 +0300 Subject: [PATCH 13/23] Finished 'secondsInHours' from easy --- src/easy/secondsInHours/plain.md | 4 ++++ src/easy/secondsInHours/pseudo.md | 5 +++++ src/easy/secondsInHours/solution.js | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/src/easy/secondsInHours/plain.md b/src/easy/secondsInHours/plain.md index 73370f1..408dbfd 100644 --- a/src/easy/secondsInHours/plain.md +++ b/src/easy/secondsInHours/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Divide the given seconds by 60 to convert to minutes. +Divide minutes by 60 to convert to hours. +The above in one step: Divide by 60*60=3600 and return result. diff --git a/src/easy/secondsInHours/pseudo.md b/src/easy/secondsInHours/pseudo.md index 520841f..4d63971 100644 --- a/src/easy/secondsInHours/pseudo.md +++ b/src/easy/secondsInHours/pseudo.md @@ -1 +1,6 @@ # Pseudo Code Solution + +```text + Divide given number by 3600 + Return result +``` diff --git a/src/easy/secondsInHours/solution.js b/src/easy/secondsInHours/solution.js index e69de29..ded843a 100644 --- a/src/easy/secondsInHours/solution.js +++ b/src/easy/secondsInHours/solution.js @@ -0,0 +1,6 @@ +function secondsInHours(seconds) { + return seconds / 3600 +} + +givenTime = 16000 +console.log(givenTime, 'second(s) equal to', secondsInHours(givenTime), 'hour(s).') \ No newline at end of file From c2c45d69c032ce442c5f33eadf24ced24fc40032 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 17:28:05 +0300 Subject: [PATCH 14/23] Finished 'sum' from easy --- src/easy/sum/plain.md | 2 ++ src/easy/sum/pseudo.md | 7 +++++++ src/easy/sum/solution.js | 9 +++++++++ 3 files changed, 18 insertions(+) diff --git a/src/easy/sum/plain.md b/src/easy/sum/plain.md index 73370f1..8190584 100644 --- a/src/easy/sum/plain.md +++ b/src/easy/sum/plain.md @@ -1 +1,3 @@ # Plain English Solution + +Run a loop from 1 to 100 summing up each number and return the result. diff --git a/src/easy/sum/pseudo.md b/src/easy/sum/pseudo.md index 520841f..fe04907 100644 --- a/src/easy/sum/pseudo.md +++ b/src/easy/sum/pseudo.md @@ -1 +1,8 @@ # Pseudo Code Solution + +```text + Initialize an empty sum + for i = 1 to 100 going up by 1 + Add i to sum + return sum +``` diff --git a/src/easy/sum/solution.js b/src/easy/sum/solution.js index e69de29..d46b925 100644 --- a/src/easy/sum/solution.js +++ b/src/easy/sum/solution.js @@ -0,0 +1,9 @@ +function sum() { + let sum = 0 + for (let i = 1; i <= 100; i++) { + sum += i + } + return sum +} + +console.log("Sum of numbers from 1 to 100 is", sum()) \ No newline at end of file From 9ac60578c231f7f172c0b1d38d6cf72c93cc5707 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 17:34:31 +0300 Subject: [PATCH 15/23] Finished 'sumOfCubes' from easy --- src/easy/sumOfCubes/plain.md | 3 +++ src/easy/sumOfCubes/pseudo.md | 8 ++++++++ src/easy/sumOfCubes/solution.js | 10 ++++++++++ 3 files changed, 21 insertions(+) diff --git a/src/easy/sumOfCubes/plain.md b/src/easy/sumOfCubes/plain.md index 73370f1..d7fec25 100644 --- a/src/easy/sumOfCubes/plain.md +++ b/src/easy/sumOfCubes/plain.md @@ -1 +1,4 @@ # Plain English Solution + +Traverse through the list and keep the sum of each element to the power of three. +When finished, return the result. diff --git a/src/easy/sumOfCubes/pseudo.md b/src/easy/sumOfCubes/pseudo.md index 520841f..9397754 100644 --- a/src/easy/sumOfCubes/pseudo.md +++ b/src/easy/sumOfCubes/pseudo.md @@ -1 +1,9 @@ # Pseudo Code Solution + +```text + Initialize a sum to zero value + for each element of the list + Calculate the cube of the element + Add it to the sum + return sum +``` diff --git a/src/easy/sumOfCubes/solution.js b/src/easy/sumOfCubes/solution.js index e69de29..da9b0e0 100644 --- a/src/easy/sumOfCubes/solution.js +++ b/src/easy/sumOfCubes/solution.js @@ -0,0 +1,10 @@ +function sumOfCubes(list){ + let sum = 0 + for (let i = 0; i < list.length; i++) { + sum += Math.pow(list[i],3) + } + return sum +} + +const givenList = [1, 5, 9] +console.log(givenList, '->', sumOfCubes(givenList)) \ No newline at end of file From 16f733c7bda51bc7527119736ac1bd3a6281fa17 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 17:48:46 +0300 Subject: [PATCH 16/23] Finished 'timesTables' from easy -> Finished all problems from easy --- src/easy/timesTables/plain.md | 6 ++++++ src/easy/timesTables/pseudo.md | 11 +++++++++++ src/easy/timesTables/solution.js | 13 +++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/easy/timesTables/plain.md b/src/easy/timesTables/plain.md index 73370f1..6fd57dc 100644 --- a/src/easy/timesTables/plain.md +++ b/src/easy/timesTables/plain.md @@ -1 +1,7 @@ # Plain English Solution + +Loop once from 1 to 12 to create 12 different multiplication tables. +Nest loop a second time from 1 to 12 to create 12 entries for each table. +For each inner loop calculate the elements of each multiplication table. +When the inner loop is over push the table in a list. +When the outer loop is over return the list of tables. diff --git a/src/easy/timesTables/pseudo.md b/src/easy/timesTables/pseudo.md index 520841f..7cbfa24 100644 --- a/src/easy/timesTables/pseudo.md +++ b/src/easy/timesTables/pseudo.md @@ -1 +1,12 @@ # Pseudo Code Solution + +```text + Initialize an empty tables list + for i = 1 to 12 + Initialize an empty entries List + for j = 1 to 12 + Multiply i with j + Push the result to the entries list + Push the entries list to the tables list + Return the tables list +``` diff --git a/src/easy/timesTables/solution.js b/src/easy/timesTables/solution.js index e69de29..32862e0 100644 --- a/src/easy/timesTables/solution.js +++ b/src/easy/timesTables/solution.js @@ -0,0 +1,13 @@ +function timesTables(){ + const resultTable = [] + for (let i = 1; i <= 12; i++) { + const entryTable = [] + for (let j = 1; j <= 12; j++) { + entryTable.push(i*j) + } + resultTable.push(entryTable) + } + return resultTable +} + +console.log(timesTables()) \ No newline at end of file From 921d305778328e131dd419cb1e4bb8da5dec7fb2 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 20:32:01 +0300 Subject: [PATCH 17/23] Finished 'morseCode' from medium (Code + ReadMe) --- src/medium/morseCode/plain.md | 15 ++++++ src/medium/morseCode/solution.js | 93 ++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/src/medium/morseCode/plain.md b/src/medium/morseCode/plain.md index 73370f1..ef65102 100644 --- a/src/medium/morseCode/plain.md +++ b/src/medium/morseCode/plain.md @@ -1 +1,16 @@ # Plain English Solution + +Using this more as the ReadMe file rather than the solution. + +toMorse() -> Takes 2 arguements and returns either Morse Code or English Letters. + Second arguement is a flag. If flag is true then convert to Morse Code. If flag is false then convert to English. + First arguement is the letter to be converted and is either in English or Morse Code. + +isNotMorse() -> Checks if the arguement is an English sentence or sentence in Morse Code. + Return true if English or false if Morse. + +morseCode() -> Takes 1 arguement and uses the two functions above to make the Morse Code Converter working either way. + English Sentence -> Morse Code + Morse Code -> English Sentence + +Note: The converter only works for English Alphabet letters and white spaces. There is no conversion for punctuation. diff --git a/src/medium/morseCode/solution.js b/src/medium/morseCode/solution.js index e69de29..0d48566 100644 --- a/src/medium/morseCode/solution.js +++ b/src/medium/morseCode/solution.js @@ -0,0 +1,93 @@ +function toMorse(char, flag) { + if (flag !== true && flag !== false) { + return '' + } + if (char === ' ') { + return '/ ' + } + if (char === '/') { + return ' ' + } + const conversion = [ + { char: 'A', code: '.-' }, + { char: 'B', code: '-...' }, + { char: 'C', code: '-.-.' }, + { char: 'D', code: '-..' }, + { char: 'E', code: '.' }, + { char: 'F', code: '..-.' }, + { char: 'G', code: '--.' }, + { char: 'H', code: '....' }, + { char: 'I', code: '..' }, + { char: 'J', code: '.---' }, + { char: 'K', code: '-.-' }, + { char: 'L', code: '.-..' }, + { char: 'M', code: '--' }, + { char: 'N', code: '-.' }, + { char: 'O', code: '---' }, + { char: 'P', code: '.--.' }, + { char: 'Q', code: '--.-' }, + { char: 'R', code: '.-.' }, + { char: 'S', code: '...' }, + { char: 'T', code: '-' }, + { char: 'U', code: '..-' }, + { char: 'V', code: '...-' }, + { char: 'W', code: '.--' }, + { char: 'X', code: '-..-' }, + { char: 'Y', code: '-.--' }, + { char: 'Z', code: '--..' } + ] + if (flag) { + char = char.toUpperCase() + const punctuation = ' .!?,;' + if (punctuation.includes(char)) { + return ' ' + } + for (let i = 0; i < conversion.length; i++) { + if (char === conversion[i].char) { + return conversion[i].code + ' ' + } + } + } else { + for (let i = 0; i < conversion.length; i++) { + if (char === conversion[i].code) { + return conversion[i].char + } + } + } + return '' +} + +function isNotMorse(str) { + const morseSymb = '.-' + if (morseSymb.includes(str.charAt(0)) == true && (morseSymb.includes(str.charAt(1)) == true || morseSymb.includes(str.charAt(2)) == true)) { + return false + } else { + return true + } +} + +function morseCode(str) { + let strArray = [] + let result = '' + let flag = true + if (isNotMorse(str)) { + strArray = str.split('') + strArray.forEach(element => { + for (let i = 0; i < element.length; i++) { + result += toMorse(element.charAt(i), flag) + } + }) + } else { + flag = false + strArray = str.split(' ') + strArray.forEach(element => { + result += toMorse(element, flag) + }) + } + return result +} + +const givenEnglish = 'Morse Code' + +console.log('English Sentence:', morseCode(morseCode(givenEnglish))) +console.log('Morse Code:', morseCode(givenEnglish)) From 5f8e8c141bce11e087555d4174fce18509954a5c Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 21:22:11 +0300 Subject: [PATCH 18/23] Finished 'fibonacci' from medium (Code + Plain) --- src/medium/fibonacci/plain.md | 5 +++++ src/medium/fibonacci/solution.js | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/src/medium/fibonacci/plain.md b/src/medium/fibonacci/plain.md index 73370f1..9b587f6 100644 --- a/src/medium/fibonacci/plain.md +++ b/src/medium/fibonacci/plain.md @@ -1 +1,6 @@ # Plain English Solution + +Initialize a list including the first two Fibonacci numbers which are 1 and 1. +Loop starting from the 3rd number going up until the 100th. +Each time calculate the n-th Fibonacci number by adding up the n-2-th and n-1-th numbers from the list. +When the loop is over, return the list. diff --git a/src/medium/fibonacci/solution.js b/src/medium/fibonacci/solution.js index e69de29..a4863d9 100644 --- a/src/medium/fibonacci/solution.js +++ b/src/medium/fibonacci/solution.js @@ -0,0 +1,9 @@ +function fibonacci(){ + resultList = [1, 1] + for (let i = 2; i < 100; i++) { + resultList.push(resultList[i-2] + resultList[i-1]) + } + return resultList +} + +console.log (fibonacci()) \ No newline at end of file From a96fe4c01523684715a25cbb7df48017bed81d74 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 22:03:14 +0300 Subject: [PATCH 19/23] Finished 'coins' from medium (Code + Plain) --- src/medium/coins/plain.md | 4 ++++ src/medium/coins/solution.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/medium/coins/plain.md b/src/medium/coins/plain.md index 73370f1..b7ec148 100644 --- a/src/medium/coins/plain.md +++ b/src/medium/coins/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Check if the given money value can be divided evenly with each coin value starting from the biggest to the lowest. +Whenever a division is possible, add the number of coins to the result string and substract this from the value. +When this process has been done for all the coin values, return the result string. diff --git a/src/medium/coins/solution.js b/src/medium/coins/solution.js index e69de29..e44268d 100644 --- a/src/medium/coins/solution.js +++ b/src/medium/coins/solution.js @@ -0,0 +1,15 @@ +function coins(value) { + const coinList = [100, 25, 10, 5, 1] + let resultStr = '' + for (let i = 0; i < 5; i++) { + const result = Math.floor(value / coinList[i]) + if (result > 0) { + resultStr += result.toString() + ' * ' + coinList[i].toString() + ', ' + value -= result * coinList[i] + } + } + return resultStr.slice(0,resultStr.length-2) +} + +const givenVal = 226 +console.log('(', givenVal, ') ->', coins(givenVal)) \ No newline at end of file From 193514147fddbf15c7697dd7470e2e9ab4d012d3 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sat, 12 Aug 2023 23:55:12 +0300 Subject: [PATCH 20/23] Finished 'romanNumerals' from medium (Code + Plain) --- src/medium/romanNumerals/plain.md | 5 ++++ src/medium/romanNumerals/solution.js | 40 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/medium/romanNumerals/plain.md b/src/medium/romanNumerals/plain.md index 73370f1..03f445e 100644 --- a/src/medium/romanNumerals/plain.md +++ b/src/medium/romanNumerals/plain.md @@ -1 +1,6 @@ # Plain English Solution + +Considering the Roman symbol is given as a string, split the string to an array. +Start by getting the numeral value of the rightest/last Roman symbol of the array and add it to the result. +Compare the previous/left symbol's value with the current value and based on the conversion rules either add or substract that from the result. +Follow this procedure until the values of all the symbols have been processed and then return the result. diff --git a/src/medium/romanNumerals/solution.js b/src/medium/romanNumerals/solution.js index e69de29..26e55b0 100644 --- a/src/medium/romanNumerals/solution.js +++ b/src/medium/romanNumerals/solution.js @@ -0,0 +1,40 @@ +function getRomanValue(romanChar) { + const valueList = [ + {symbol: 'I', value: 1}, + {symbol: 'V', value: 5}, + {symbol: 'X', value: 10}, + {symbol: 'L', value: 50}, + {symbol: 'C', value: 100}, + {symbol: 'D', value: 500}, + {symbol: 'M', value: 1000} + ] + for (let i = 0; i < valueList.length; i++) { + if (romanChar === valueList[i].symbol) { + return valueList[i].value + } + } +} + +function romanNumerals(str) { + const strList = str.split('') + let result = 0 + let tmp1 = 0 + for (let i = strList.length - 1; i >= 0; i--) { + const tmp2 = getRomanValue(strList[i]) + if (i === strList.length - 1){ + tmp1 = tmp2 + result += tmp1 + } else { + if (tmp2 >= tmp1) { + result += tmp2 + } else { + result -= tmp2 + } + tmp1 = tmp2 + } + } + return result +} + +const givenRoman = 'XL' +console.log(givenRoman, 'has numeral value of', romanNumerals(givenRoman)) From 43e1aad7adba17c95f9c20da714f35906c6c9bce Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sun, 13 Aug 2023 00:14:50 +0300 Subject: [PATCH 21/23] Finished 'smallestProduct' from medium (Code + Plain) --- src/medium/smallestProduct/plain.md | 4 ++++ src/medium/smallestProduct/solution.js | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/medium/smallestProduct/plain.md b/src/medium/smallestProduct/plain.md index 73370f1..e7ce6ba 100644 --- a/src/medium/smallestProduct/plain.md +++ b/src/medium/smallestProduct/plain.md @@ -1 +1,5 @@ # Plain English Solution + +Start from the number 20 and go up by 20 every time. +With each iteration check whether the target number can be divided by all numbers in the range 1-20. +Stop when criteria is met and return the result. diff --git a/src/medium/smallestProduct/solution.js b/src/medium/smallestProduct/solution.js index e69de29..65a41cf 100644 --- a/src/medium/smallestProduct/solution.js +++ b/src/medium/smallestProduct/solution.js @@ -0,0 +1,19 @@ +function smallestProduct() { + let flag = true + let result = 0 + while (flag) { + result += 20 + for (let i = 19; i > 0; i--) { + if (i === 1) { + flag = false + break + } + if (result % i !== 0) { + break + } + } + } + return result +} + +console.log('The smallest positive number divided by [1-20] is', smallestProduct()) \ No newline at end of file From 89a4876826349bcf8b739672bd13f019b57b81d8 Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sun, 13 Aug 2023 02:25:29 +0300 Subject: [PATCH 22/23] Finished(?) 'castles' from hard (Code + ReadMe) --- src/hard/castles/plain.md | 14 ++++++++ src/hard/castles/solution.js | 63 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/src/hard/castles/plain.md b/src/hard/castles/plain.md index 73370f1..5f2013b 100644 --- a/src/hard/castles/plain.md +++ b/src/hard/castles/plain.md @@ -1 +1,15 @@ # Plain English Solution + +Using this more as a ReadMe file rathen than the solution. + +mapInit() -> Creating the map with increasingly harder monsters as well as higher rewards. + +rollDice() -> Use machine time to create a pseudo-random generating function. + +doBluff() -> Compare random chance with the bluff success rate which is 30%. + +doBattle() -> Based on each monster's difficulty compare its defeat chance with a random chance. + +castles() -> The main function. Use all the functions above to achieve the desired functionality. TLDR: Keep bluffing/fighting monsters until you run out of either lives or rooms. + +Note: Not sure if/how the pseudo-random function can be 'improved'. diff --git a/src/hard/castles/solution.js b/src/hard/castles/solution.js index e69de29..0b3f360 100644 --- a/src/hard/castles/solution.js +++ b/src/hard/castles/solution.js @@ -0,0 +1,63 @@ +function mapInit() { + const castles = 5 + const rooms = 7 + const baseChance = 85 + const baseReward = 10 + const castleLayout = [] + for (let i = 1; i <= castles; i++) { + const roomLayout = [] + for (let j = 1; j <= rooms; j++) { + const roomElements = { + monster: baseChance - (15 * i) - j, + treasure: baseReward * (Math.pow(i,2)) + } + roomLayout.push(roomElements) + } + castleLayout.push(roomLayout) + } + return castleLayout +} + +function rollDice() { + const timeInit = new Date() + return timeInit.getMilliseconds() % 101 +} + +function doBluff() { + return rollDice() < 30 +} + +function doBattle(winThreshold) { + return rollDice() < winThreshold +} + +function castles() { + let lives = 9 + let score = 0 + const castleMap = mapInit() + for (let i = 0; i < castleMap.length; i++) { + for (let j = 0; j < castleMap[i].length; j++) { + if (doBluff() || doBattle(castleMap[i][j].monster)) { + score += castleMap[i][j].treasure + } else { + lives-- + } + if (lives === 0) { + console.log(`Your adventure ends on floor ${i+1} room ${j+1}.`) + break + } + } + if (lives === 0) { + console.log('Defeat!') + break + } + } + if (score === 3850) { + console.log('You exit the castles with great treasures.') + console.log('Victory!') + } + return score +} + +//console.log(mapInit()) +console.log('Your score for this run is', castles(),'.') From e1eb6d698dc2e79e21814f2411e7eeb526d7664f Mon Sep 17 00:00:00 2001 From: iliask796 Date: Sun, 13 Aug 2023 14:57:48 +0300 Subject: [PATCH 23/23] Modified 'castles' from hard (Win Rate) --- src/hard/castles/solution.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hard/castles/solution.js b/src/hard/castles/solution.js index 0b3f360..7534e1c 100644 --- a/src/hard/castles/solution.js +++ b/src/hard/castles/solution.js @@ -1,14 +1,14 @@ function mapInit() { const castles = 5 const rooms = 7 - const baseChance = 85 + const baseChance = 0 const baseReward = 10 const castleLayout = [] for (let i = 1; i <= castles; i++) { const roomLayout = [] for (let j = 1; j <= rooms; j++) { const roomElements = { - monster: baseChance - (15 * i) - j, + monster: baseChance + 3* (6 * i) + j, treasure: baseReward * (Math.pow(i,2)) } roomLayout.push(roomElements) @@ -28,7 +28,7 @@ function doBluff() { } function doBattle(winThreshold) { - return rollDice() < winThreshold + return rollDice() > winThreshold } function castles() { @@ -52,12 +52,12 @@ function castles() { break } } - if (score === 3850) { + if (lives !== 0) { console.log('You exit the castles with great treasures.') console.log('Victory!') } return score } -//console.log(mapInit()) +// console.log(mapInit()) console.log('Your score for this run is', castles(),'.')