-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray-and-strings.txt
More file actions
124 lines (92 loc) · 3.49 KB
/
Copy patharray-and-strings.txt
File metadata and controls
124 lines (92 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
1) Given an array of strings and numbers. Print the number of integers and the number of strings in the array.
Input: [1, ‘10’, ‘hi’, 2, 3]
Output: “Numbers: 3, Strings: 2”
Input: [1, 4, ‘i am a string’, ‘456’]
Output: “Numbers: 2, Strings: 2”
Solution:
function printNum(array) {
let numOfString = 0;
let numOfNumber = 0;
array.forEach(element => {
if (typeof element != "number")
numOfString++
else
numOfNumber++
});
console.log("Number:" + numOfNumber, "String:" + numOfString)
}
2) Write a function that accepts a string(a sentence) as a parameter and finds the longest word within the string․
If there are several words which are the longest ones, print the last word(words can be separated by space, comma or hyphen).
Input: “A revolution without dancing is a revolution not worth having.”
Output: "revolution"
Input: ”Which would be worse - to live as a monster, or to die as a good man?”
Output: "monster"
Solution:
function LongestWord(string) {
let arr = string.split(/[ ,.-]/);
let longest = "";
arr.forEach(element => {
if (element.length >= longest.length)
longest = element;
});
return longest;
}
3) Write a function which receives an array and a number as arguments and returns a new array from the elements of the given
array which are larger than the given number.
Input: "[10, 25, 16, -5, 30, 15, 24] , 16"
Output: "[25, 30, 24]"
Input: "[1, 1, 2, -3, 0, 8, 4, 0], 9"
Output: "[]"
Solution:
function Comparison(array,num){
return array.filter(x => x > num)
}
4) Write a function, which will receive a number between 0 to 999 and spell out that number in English.
Input: 5
Output: "five"
Input: 9425
Output: “nine thousand four hundred twenty five”
Solution:
function NumToText(num) {
const nums = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
const nums2 = ["ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
const nums3 = ["twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"];
if (Math.floor(num / 10) != 0) {
let mat1 = num % 10;
if (Math.floor(num / 100) != 0) {
num = Math.floor(num / 10);
let mat2 = num % 10;
let mat3 = Math.floor(num / 10);
if(mat2 == 0 && mat1 == 0)
return nums[mat3] + " hundred ";
if (mat2 == 0) {
return nums[mat3] + " hundred " + nums[mat1];
}
if (mat2 == 1) {
return nums[mat3] + " hundred " + nums2[mat1];
}
if(mat1 == 0)
return nums[mat3] + " hundred " + nums3[mat2 - 2];
return nums[mat3] + " hundred " + nums3[mat2 - 2] + " " + nums[mat1];
}
let mat4 = Math.floor(num / 10);
if (mat4 == 1)
return nums2[mat1]
else if (mat1 == 0)
return nums3[mat4 - 2]
else
return nums3[mat4 - 2] + " " + nums[mat1]
}
return nums[num];
}
5) A left rotation operation on an array shifts each of the array's elements unit to the left. For example,
if left rotations are performed on array [1, 2, 3, 4, 5], then the array would become [3, 4, 5, 1, 2].
Solution:
function rotateWithNum (array, num){
for (let i = 0; i < num; i++) {
let temp = array[0];
array.shift();
array.push(temp);
}
return array;
}