-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy Private challenges.js
82 lines (66 loc) · 1.62 KB
/
my Private challenges.js
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
function descendingOrder(n){
//...converts n to a string and splits it
n = n.toString();
n = n.split('');
//sorts the string in a descending order
n.sort(function(a,b) {
return b - a;
});
//converts the resulting string back to a number
return Number(n.join(''));
}
function findEvenIndex(arr)
{
//Code goes here!
var newArr = 0, rev = arr.reduce(function (a, b) {return a+b; }, 0);
for (var i=0; i<arr.length; i++) {
if (i>0) newArr += arr[i-1];
rev -= arr[i];
if(newArr == rev)
return i;
} return -1;
}
//Andela
function isIsogram(word) {
//rep stores the occurence of a letter in the string 'word'
var rep = (/([a-zA-Z]).*?\1/).test(word);
if (word == null) {
return false;
} else if (rep) {
return false;
} else if (!word || word.length === 0) {
return false;
}
//if there is no letter repetition return true
else {
return true;
}
};
function sumAll(arr) {
var sum = [];
var max= Math.max(arr[0], arr[1]);
var min = Math.min(arr[0], arr[1]);
for(var i=max+1; i>min; i--) {
sum.push(i-1);
}
//return sum;
var total = sum.reduce(function(sum, value) {
return sum + value;
}, 0);
return total;
}
sumAll([1, 4]);
function convertToRoman(num) {
var ara = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
var rom = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'];
//var box = $(this).find('[name="box"]');
var final = '';
for (var i = 0; i < rom.length; i++) {
while (num >= ara[i]) {
final += rom[i];
num -= ara[i];
}
}
return final;
}
convertToRoman(9);