-
Notifications
You must be signed in to change notification settings - Fork 0
/
codewars8kyu.js
75 lines (53 loc) · 3.18 KB
/
codewars8kyu.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
/* You're at the zoo... all the meerkats look weird.
Something has gone terribly wrong - someone has gone and switched their heads and tails around!
Save the animals by switching them back. You will be given an array which will have three values (tail, body, head).
It is your job to re-arrange the array so that the animal is the right way round (head, body, tail).
Same goes for all the other arrays/lists that you will get in the tests: you have to change the element positions
with the same exact logics */
function fixTheMeerkat(arr) {
let newArr = [];
let item = 0;
while(arr.length != 0){
let item = arr.pop();
newArr.push(item);
} return newArr;
}
/* It's bonus time in the big city! The fatcats are rubbing their paws in anticipation... but who is going to make the most money?
Build a function that takes in two arguments (salary, bonus). Salary will be an integer, and bonus a boolean.
If bonus is true, the salary should be multiplied by 10. If bonus is false, the fatcat did not make enough money and must receive only his stated salary.
Return the total figure the individual will receive as a string prefixed with "£" */
function bonusTime(salary, bonus) {
return (bonus === true ? '£' + salary*10 : '£' + salary);
}
/*It's the academic year's end, fateful moment of your school report. The averages must be calculated.
All the students come to you and entreat you to calculate their average for them. Easy ! You just need to write a script.
Return the average of the given array rounded down to its nearest integer.The array will never be empty */
function getAverage(marks){
let count = 0;
for (let item of marks) {
count += item;
}
return Math.floor(count/marks.length);
}
/*This method, which is supposed to return the result of dividing its first argument by its second, isn't always returning correct values. Fix it. */
const solve = (x, y) => x / y;
/*In this simple exercise, you will create a program that will take two lists of integers, a and b.
Each list will consist of 3 positive integers above 0, representing the dimensions of cuboids a and b.
You must find the difference of the cuboids' volumes regardless of which is bigger.
For example, if the parameters passed are ([2, 2, 3], [5, 4, 1]), the volume of a is 12 and the volume of b is 20.
Therefore, the function should return 8.Your function will be tested with pre-made examples as well as random ones.
If you can, try writing it in one line of code. */
function findDifference(a, b) {
let result = (a[0]*a[1]*a[2]) - (b[0]*b[1]*b[2]);
return result > 0 ? result : -(result);
}
/* Your task is to find the first element of an array that is not consecutive.
By not consecutive we mean not exactly 1 larger than the previous element of the array.
If the whole array is consecutive then return null. The array will always have at least 2 elements and all elements will be numbers.
The numbers will also all be unique and in ascending order. The numbers could be positive or negative and the first non-consecutive could be either too! */
function firstNonConsecutive (arr) {
for(let i = 1; i < arr.length; i += 1) {
if(arr[i] != arr[i - 1] + 1) return(arr[i]);
}
return(null);
}