JavaScript Programs
JavaScript Programs
Task description
An array A consisting of N different integers is given. The array contains
integers in the range [1..(N + 1)], which means that exactly one element is
missing.
Write a function:
function solution(A);
A[0] = 2
A[1] = 3
A[2] = 1
A[3] = 5
the function should return 4, as it is the missing element.
Score - 30%
Solution - https://app.codility.com/demo/results/training99Z8YY-9VQ/
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if(A.length <= 1){
return 1;
}
var missing;
A.sort();
2. TapeEquilibrium
Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P - 1]
and A[P], A[P + 1], ..., A[N - 1].
The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P - 1]) - (A[P] + A[P + 1]
+ ... + A[N - 1])|
In other words, it is the absolute difference between the sum of the first part and the sum of the
second part.
A[0] = 3
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
P = 1, difference = |3 - 10| = 7
P = 2, difference = |4 - 9| = 5
P = 3, difference = |6 - 7| = 1
P = 4, difference = |10 - 3| = 7
Write a function:
function solution(A);
that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.
A[1] = 1
A[2] = 2
A[3] = 4
A[4] = 3
Score - 84%
Solution - https://app.codility.com/demo/results/training72S2G5-TH3/
function solution(A) {
var sumright = 0;
var sumleft = 0;
var ans = 0;
sumright += A[i];
sumleft = A[0];
ans =Math.abs(sumright + sumleft);
sumleft += A[P];
sumright -=A[P];
return ans;
//var i;
//var j=1;
// var firstValue = 0;
// var secondValue = 0;
// }
// else{
// }
// }
// //console.log(Math.abs(firstValue - secondValue));
// }
//}
//return absoluteValue;
3. PermCheck
https://app.codility.com/programmers/lessons/4-counting_elements/perm_check/
Score - 75%
Solution - https://app.codility.com/demo/results/training6V86MP-N68/
https://app.codility.com/demo/results/trainingSJR8NF-76C/
function solution(A) {
A.sort();
if(A[i-1] != i){
return 0;
return 1;
}
4. FrogRiverOne
Find the earliest time when a frog can jump to the other side of a river.
https://app.codility.com/programmers/lessons/4-counting_elements/
frog_river_one/
Score - 81%
Solution - https://app.codility.com/demo/results/trainingY3HJ5Y-C2V/
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(X, A) {
// write your code in JavaScript (Node.js 8.9.4)
let result = 0;
for(let i=1; i<=X; i++){
//console.log(A.indexOf(i) + '_' + i);
let index = A.indexOf(i);
if( index != -1){
if(index > result){
result = index;
}
}
else {
return -1;
}
}
return result;
}
MaxCounters
Calculate the values of counters after applying all alternating operations: increase counter by 1; set
value of all counters to current maximum.
https://app.codility.com/programmers/lessons/4-counting_elements/max_counters/
Score - 100%
Solution - https://app.codility.com/demo/results/trainingY767U6-ETR/
function solution(N, A) {
var max = 0;
min = max;
//console.log(my_array.length);
my_array[j] = min;
return my_array;
--------------------------------------------------------------------------------------------------------------------------------------
MissingInteger
Find the smallest positive integer that does not occur in a given sequence.
https://app.codility.com/programmers/lessons/4-counting_elements/missing_integer/
Score - 66%
Solution - https://app.codility.com/demo/results/trainingNE3NBX-H6H/
function solution(A) {
A.sort();
return i;
return length;
PassingCars
https://app.codility.com/programmers/lessons/5-prefix_sums/passing_cars/
Score - 100%
Solution - https://app.codility.com/demo/results/trainingR3CTEK-7KU/
function solution(A) {
let num_east = 0;
let num_pass = 0;
return -1;
else
return num_pass;