This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -23,6 +23,8 @@
|
||||
|
||||
const findDeepestLevel = (arr, currentLevel = 1) => {};
|
||||
|
||||
// Array.isArray()
|
||||
|
||||
// Test cases
|
||||
console.log(findDeepestLevel([1, [2, [3, [4]], 5]])); // => 4
|
||||
console.log(findDeepestLevel([1, 2, 3, 4, 5])); // => 1
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<!DOCTYPE html>
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
@@ -20,9 +20,47 @@
|
||||
<script>
|
||||
'use strict';
|
||||
|
||||
const findMax = (arr) => {};
|
||||
const findMax2 = (ar) => {
|
||||
if (ar.length === 0) return null;
|
||||
if (ar.length === 1) return ar[0];
|
||||
let max = ar[0];
|
||||
let i = -1;
|
||||
while (++i < ar.length) {
|
||||
if (ar[i] > max) {
|
||||
max = ar[i];
|
||||
}
|
||||
}
|
||||
return max;
|
||||
};
|
||||
|
||||
const findMax = (arr) => {
|
||||
// Basisfall
|
||||
if (arr.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (arr.length === 1) {
|
||||
return arr[0];
|
||||
}
|
||||
|
||||
return Math.max(arr[0], findMax(arr.slice(1))); // Rekursion
|
||||
|
||||
// return arr.length === 0 ? null : arr.length === 1 ? arr[0] : Math.max(arr[0], findMax(arr.slice(1)));
|
||||
};
|
||||
|
||||
// Test cases
|
||||
console.time('rekursion max');
|
||||
console.log(findMax([1, 5, 3])); // => 5
|
||||
console.timeEnd('rekursion max');
|
||||
|
||||
console.time('Math.max');
|
||||
console.log(Math.max(...[1, 5, 3]));
|
||||
console.timeEnd('Math.max');
|
||||
|
||||
console.time('for');
|
||||
console.log(findMax2([1, 5, 3]));
|
||||
console.timeEnd('for');
|
||||
|
||||
console.log(findMax([1, 5, 3, 9, 2])); // => 9
|
||||
console.log(findMax([-10, -20, -3, -4])); // => -3
|
||||
console.log(findMax([42])); // => 42
|
||||
|
||||
Reference in New Issue
Block a user