Skip to content

Commit 8be7b2c

Browse files
author
Neelesh Shetty
committed
Easy
1 parent cc11eb7 commit 8be7b2c

20 files changed

+301
-1
lines changed

1.SumOfTwoNumbers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ function sumOfTwoNumbers(a, b) {
44
return a + b
55
}
66

7-
console.log(sumOfTwoNumbers(1, 2)) // 3
7+
console.log(sumOfTwoNumbers(1, 2)) // 3
8+
console.log(sumOfTwoNumbers(100, 15)) // 115
9+
console.log(sumOfTwoNumbers(52, 28)) // 80

10.LessThan100.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Given two numbers, return true if the sum of the numbers is less than 100. Otherwise return false.
2+
3+
function lessThan100(x, y) {
4+
5+
const sum = x + y;
6+
7+
if (sum < 100) {
8+
return true
9+
} else
10+
return false
11+
}
12+
13+
console.log(lessThan100(20, 30)) // true
14+
console.log(lessThan100(60, 90)) // false
15+
console.log(lessThan100(50, 50)) // false

11.ScoreCalculate.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* Create a function that takes the number of wins, draws and losses
2+
and calculates the number of points a football team has obtained so far. */
3+
4+
// wins get 3 points
5+
// draws get 1 point
6+
// losses get 0 points
7+
8+
function scoreCalculate(wins, draws, losses) {
9+
return (wins * 3) + (draws * 1) + (losses * 0)
10+
}
11+
12+
console.log(scoreCalculate(3, 4, 5)) // 13
13+
console.log(scoreCalculate(9, 8, 10)) // 35
14+
console.log(scoreCalculate(0, 4, 5)) // 4

12.VoteCount.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Given an object containing counts of both upvotes and downvotes,
2+
// return what vote count should be displayed.
3+
// This is calculated by subtracting the number of downvotes from upvotes.
4+
5+
function voteCount(votes) {
6+
return votes.upvotes - votes.downvotes
7+
}
8+
9+
console.log(voteCount({ upvotes: 13, downvotes: 0 })) // 13
10+
console.log(voteCount({ upvotes: 2, downvotes: 33 })) // -31
11+
console.log(voteCount({ upvotes: 132, downvotes: 132 })) // 0

13.FirstElementOfArray.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Create a function that takes an array and returns the first element.
2+
3+
function firstElementOfArray(array) {
4+
return array[0]
5+
}
6+
7+
console.log(firstElementOfArray([7, 2, 6, 9, 3])) // 7
8+
console.log(firstElementOfArray([10, 12, 16, 19, 23])) // 10
9+
console.log(firstElementOfArray([27, 22, 26, 29, 43])) // 27

14.ArrayToString.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Create a function that takes an array of numbers or letters and returns a string.
2+
3+
function arrayToString(arr) {
4+
let result = ""
5+
for (let i = 0; i < arr.length; i++) {
6+
result += arr[i]
7+
}
8+
return result
9+
}
10+
11+
// 2nd Method
12+
// Using built-in method join()
13+
14+
function arrayToString(arr) {
15+
return arr.join("")
16+
}
17+
18+
console.log(arrayToString([1, 2, 3, 4, 5, 6])) // "123456"
19+
console.log(arrayToString(["a", "b", "c", "d", "e", "f"])) // "abcdef"
20+
console.log(arrayToString([1, 2, 3, "a", "s", "dAAAA"])) // "123asdAAAA"

15.FirstLastElement.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Return the First and Last Elements in an Array
2+
3+
// Create a function that takes an array of numbers and return the first and last elements as a new array.
4+
5+
function firstLastElement(arr) {
6+
return [arr[0], arr[arr.length - 1]]
7+
}
8+
9+
// <--------------------------------------------> //
10+
11+
// Method -2
12+
// using build-in methods shift() & pop()
13+
14+
function firstLastElement(arr) {
15+
return [arr.shift(), arr.pop()]
16+
}
17+
18+
console.log(firstLastElement([5, 10, 15, 20, 25])) // [5, 25]
19+
console.log(firstLastElement(["edabit", 13, null, false, true])) // ["edabit", true]
20+
console.log(firstLastElement([undefined, 4, "6", "hello", null])) // [undefined, null]
21+
22+
// shift() is used to remove the first element from an array
23+
// pop() method is used to remove the last method from the array.

16.ArraySearch.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Where is Bob!?!
2+
3+
// Write a function that searches an array of names (unsorted) for the name "Bob" and returns the location in the array.
4+
// If Bob is not in the array, return -1.
5+
6+
function findBob(arr) {
7+
for (let i = 0; i < arr.length; i++) {
8+
if (arr[i] === "Bob") {
9+
return i
10+
}
11+
}
12+
return -1
13+
}
14+
15+
// <-----------------------------------------------------------> //
16+
17+
// METHOD - 2
18+
// using built-in method indexOf()
19+
20+
function findBob(arr) {
21+
return arr.indexOf("Bob")
22+
}
23+
24+
console.log(findBob(["Jimmy", "Layla", "Bob"])) // 2
25+
console.log(findBob(["Bob", "Layla", "Kaitlyn", "Patricia"])) // 0
26+
console.log(findBob(["Jimmy", "Layla", "James"])) // -1

17.ArrayBetween.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*Create a function that takes two numbers num1, num2, and an array arr and
2+
returns an array containing all the numbers in arr greater than num1 and less than num2.*/
3+
4+
function arrBetween(num1, num2, arr) {
5+
let result = []
6+
for (let i = 0; i < arr.length; i++) {
7+
if ((arr[i] > num1) && (arr[i] < num2)) {
8+
result.push(arr[i])
9+
}
10+
}
11+
return result
12+
}
13+
14+
console.log(arrBetween(3, 8, [1, 5, 95, 0, 4, 7])) // [5, 4, 7]
15+
console.log(arrBetween(1, 10, [1, 10, 25, 8, 11, 6])) // [8, 6]
16+
console.log(arrBetween(7, 32, [1, 2, 3, 78])) // []

18.WithoutFirstChar.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Word without First Character
2+
3+
//Create a function that takes a word and returns the new word without including the first character.
4+
5+
function newWord(str) {
6+
let result = ""
7+
for (let i = 1; i < str.length; i++) {
8+
result += str[i]
9+
}
10+
return result
11+
}
12+
13+
// <-----------------------------------------------> //
14+
// Method - 2
15+
// Using built-in method slice()
16+
17+
// The slice( ) method copies a given part of an array and returns that copied part as a new array. It doesn’t change the original array.
18+
// slice(from, until);
19+
20+
function newWord(str) {
21+
return str.slice(1)
22+
}
23+
24+
// <-------------------------------------> //
25+
// Method - 3
26+
// Using built-in method substr()
27+
28+
// substr() is used to return the part of the given string from start index to end index.
29+
// Indexing starts from zero (0).
30+
31+
function newWord(str) {
32+
return str.substr(1)
33+
}
34+
35+
console.log(newWord("apple")) // "pple"
36+
console.log(newWord("cherry")) // "herry"
37+
console.log(newWord("plum")) // "lum"

0 commit comments

Comments
 (0)