Hoe snel javascript jij?
Doe de onderstaande test ;-)
'T was ook echt niet hele mooie code moet ik toegeven... maar het werkte :) Ik zat wel even te prutten bij de length van een chineese string :)
Benieuwd hoe jij het hebt opgelost Wouter ;-)
Toevoeging op 08/11/2013 11:55:43:
Hertog of Jan, hoe kan jouw longestString functie goed hebben gewerkt? Bij mij werkte die niet bij chineese tekens.
Is volgens mij korter :)
3 minutes, 18 seconds :)
*kuch* nerd *kuch* haha ;-) Ik verspeelde veel tijd met odd/even ;-)
'T was ook echt niet hele mooie code moet ik toegeven... maar het werkte :)
Code (php)
1
2
3
4
5
2
3
4
5
function doubleInteger(i) {
// i will be an integer. Double it and return it.
return i * 2;
}
// i will be an integer. Double it and return it.
return i * 2;
}
Code (php)
1
2
3
4
5
2
3
4
5
function isNumberEven(i) {
// i will be an integer. Return true if it's even, and false if it isn't.
return i % 2 ? false : true;
}
// i will be an integer. Return true if it's even, and false if it isn't.
return i % 2 ? false : true;
}
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
function getFileExtension(i) {
// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one, otherwise false
if (-1 == i.indexOf('.')) {
return false;
}
var parts = i.split('.');
return parts[parts.length - 1];
}
// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one, otherwise false
if (-1 == i.indexOf('.')) {
return false;
}
var parts = i.split('.');
return parts[parts.length - 1];
}
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
function longestString(i) {
// i will be an array.
// return the longest string in the array
var value = '';
for (var ii = 0; ii <= i.length; ii++) {
if (typeof i[ii] == 'string' && i[ii].length >= value.length) {
value = i[ii];
}
}
return value;
}
// i will be an array.
// return the longest string in the array
var value = '';
for (var ii = 0; ii <= i.length; ii++) {
if (typeof i[ii] == 'string' && i[ii].length >= value.length) {
value = i[ii];
}
}
return value;
}
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function arraySum(i) {
// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
var sum = 0;
for (var ii = 0; ii <= i.length; ii++) {
if (typeof i[ii] == 'number') {
sum += i[ii];
} else if (typeof i[ii] == 'object') {
sum += arraySum(i[ii]);
}
}
return sum;
}
// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
var sum = 0;
for (var ii = 0; ii <= i.length; ii++) {
if (typeof i[ii] == 'number') {
sum += i[ii];
} else if (typeof i[ii] == 'object') {
sum += arraySum(i[ii]);
}
}
return sum;
}
Benieuwd hoe jij het hebt opgelost Wouter ;-)
Gewijzigd op 08/11/2013 10:07:26 door Joakim Broden
Code (php)
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
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
function doubleInteger(i) {
// i will be an integer. Double it and return it.
i *= 2;
return i;
}
function isNumberEven(i) {
// i will be an integer. Return true if it's even, and false if it isn't.
return i % 2 === 0;
}
function getFileExtension(i) {
// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one, otherwise false
return i.split('.')[1] || false;
}
function longestString(i) {
// i will be an array.
// return the longest string in the array
var longest = 0;
var string;
i.forEach(function (ie) {
if (typeof ie !== 'string') {
return;
}
var len = 0;
while(ie.charAt(++len) !== '');
if (len > longest) {
string = ie;
longest = len;
}
});
return string;
}
function arraySum(i) {
// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
var sum = 0;
i.forEach(function (ie) {
switch (typeof ie) {
case 'number':
sum += ie;
break;
case 'object':
sum += arraySum(ie);
break;
}
});
return sum;
}
// i will be an integer. Double it and return it.
i *= 2;
return i;
}
function isNumberEven(i) {
// i will be an integer. Return true if it's even, and false if it isn't.
return i % 2 === 0;
}
function getFileExtension(i) {
// i will be a string, but it may not have a file extension.
// return the file extension (with no period) if it has one, otherwise false
return i.split('.')[1] || false;
}
function longestString(i) {
// i will be an array.
// return the longest string in the array
var longest = 0;
var string;
i.forEach(function (ie) {
if (typeof ie !== 'string') {
return;
}
var len = 0;
while(ie.charAt(++len) !== '');
if (len > longest) {
string = ie;
longest = len;
}
});
return string;
}
function arraySum(i) {
// i will be an array, containing integers, strings and/or arrays like itself.
// Sum all the integers you find, anywhere in the nest of arrays.
var sum = 0;
i.forEach(function (ie) {
switch (typeof ie) {
case 'number':
sum += ie;
break;
case 'object':
sum += arraySum(ie);
break;
}
});
return sum;
}
Toevoeging op 08/11/2013 11:55:43:
Hertog of Jan, hoe kan jouw longestString functie goed hebben gewerkt? Bij mij werkte die niet bij chineese tekens.
Code (php)
1
2
3
4
5
2
3
4
5
function isNumberEven(i) {
// i will be an integer. Return true if it's even, and false if it isn't.
return i % 2 == 0;
}
// i will be an integer. Return true if it's even, and false if it isn't.
return i % 2 == 0;
}
Is volgens mij korter :)