Skip to content
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

Narges Barani (Solved exercises) #3

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\simple\\printMessage.js"
}
]
}
4 changes: 4 additions & 0 deletions advanced/calculateAverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
* calculateAverage([1, 2, 3, 4]) should return 2.5.
* calculateAverage([10, 20, 30]) should return 20.
*/
function calculateAverage(arr) {
const sum = arr.reduce((acc, num) => acc + num, 0)
return sum / arr.length
}

module.exports = calculateAverage;
16 changes: 16 additions & 0 deletions advanced/findDuplicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@
*/

// Write your solution here
findDuplicates = (arr) => {
let countMap = {}
let duplicates = []

for (let num of arr) {
countMap[num] = (countMap[num] || 0) + 1
}

for (let num in countMap) {
if (countMap[num] > 1) {
duplicates.push(Number(num))
}
}

return duplicates;
}

module.exports = findDuplicates;
16 changes: 16 additions & 0 deletions advanced/isPalindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@
* isPalindrome(-121) should return false (negative numbers are not palindromes).
* isPalindrome(10) should return false.
*/
isPalindrome = (num) => {
if (num < 0 || (num % 10 === 0 && num !== 0)) {
return false
}

let reversed = 0
let original = num

while (num > 0) {
let digit = num % 10
reversed = reversed * 10 + digit
num = Math.floor(num / 10)
}

return original === reversed
}

module.exports = isPalindrome;
23 changes: 23 additions & 0 deletions advanced/numberToWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,28 @@
*/

// Write your solution here
function numberToWords(num) {
if (num === 0) return "zero"
const ones = ["", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
const tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]

let words = ""

if (num >= 100) {
words += ones[Math.floor(num / 100)] + " hundred"
num %= 100
if (num > 0) words += " "
}

if (num >= 20) {
words += tens[Math.floor(num / 10)]
num %= 10
if (num > 0) words += " " + ones[num]
} else if (num > 0) {
words += ones[num]
}

return words;
}

module.exports = numberToWords;
3 changes: 3 additions & 0 deletions advanced/searchArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
* searchArray([1, 2, 3, 4], 3) should return true.
* searchArray([1, 2, 3, 4], 5) should return false.
*/
searchArray = (arr, value) => {
return arr.includes(value); // بررسی اینکه آیا مقدار مورد نظر در آرایه وجود دارد یا خیر
}

module.exports = searchArray;
3 changes: 3 additions & 0 deletions advanced/sortArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
* Example:
* sortArray([3, 1, 4, 1, 5, 9]) should return [1, 1, 3, 4, 5, 9].
*/
sortArray = (arr) => {
return arr.sort((a, b) => a - b)
}

module.exports = sortArray;
10 changes: 10 additions & 0 deletions advanced/sumOfDigits.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@
* sumOfDigits(123) should return 6 (1 + 2 + 3).
* sumOfDigits(987) should return 24 (9 + 8 + 7).
*/
sumOfDigits = (num) => {
let numStr = num.toString();
let sum = 0;

for (let i = 0; i < numStr.length; i++) {
sum += parseInt(numStr[i]);
}

return sum;
}

module.exports = sumOfDigits;
16 changes: 16 additions & 0 deletions advanced/todoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,21 @@
*/

// Write your solution here
let list = []
addTask = (to_do) => {
if(!list.includes(to_do)) {
list.push(to_do)
}
}

showTasks = () => {
return list
}

removeTask = (to_do) => {
let index = list.indexOf(to_do)
if (index !== -1) {
list.splice(index, 1)
}
}
module.exports = { addTask, removeTask, showTasks };
17 changes: 17 additions & 0 deletions advanced/userManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,22 @@
* removeUser("Alice");
* showUsers() should return [].
*/
let list = []
addUser = (user) => {
if (!list.includes(user)) {
list.push(user)
}
}

showUsers = () => {
return list
}

removeUser = (user) => {
let index = list.indexOf(user)
if (index !== -1) {
list.splice(index, 1)
}
}

module.exports = { addUser, removeUser, showUsers };
6 changes: 6 additions & 0 deletions intermediate/charCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
*/

// Write your solution here
charCount = (string , char) => {
string = string.split('')
return string.filter(item => item === char).length
}

charCount('hello' , 'l')
charCount('world' , 'o')
module.exports = charCount;
14 changes: 14 additions & 0 deletions intermediate/factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@
*/

// Write your solution here
factorial = (input) => {
let factor = 1
for (let i = 1; i <= input; i++) {
factor *= i
}
return factor
}

let inp = 5
let inp2= 0
let inp3 = 7

factorial(inp)
factorial(inp2)
factorial(inp3)
module.exports = factorial;
16 changes: 16 additions & 0 deletions intermediate/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@
*/

// Write your solution here
fibonacci = (counter) => {
let pre = 0, next = 1;
let fibonacci_array = [0]
for (let i = 1; i < counter; i++) {
let keep = pre + next
pre = next
next = keep
fibonacci_array[i] = pre
}
return fibonacci_array
}

let fibo1 = 5
let fibo2 = 3

fibonacci(fibo1)
fibonacci(fibo2)
module.exports = fibonacci;
23 changes: 23 additions & 0 deletions intermediate/isPrime.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,28 @@
*/

// Write your solution here
isPrime = (number) => {
let counter = 0
if (number < 2) {
return false
}
for (let i = 2; i <= number; i++){
if (number % i == 0) {
counter++;
}
}

if (counter > 1) {
return false
}
return true
}

let num1 = 2
let num2 = 4
let num3 = 5

isPrime(num1)
isPrime(num2)
isPrime(num3)
module.exports = isPrime;
12 changes: 12 additions & 0 deletions intermediate/sumArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@
*/

// Write your solution here
sumArray = (num_array) => {
return result = num_array.reduce((item , current) => {
return item + current
}, 0)
}

let array1 = [1, 2, 3]
let array2 = [0, 0, 0]
let array3 = [1, -1, 1]

sumArray(array1)
sumArray(array2)
sumArray(array3)
module.exports = sumArray;
Loading