Solved Task 3

This commit is contained in:
schrom01 2022-09-29 12:36:51 +02:00
commit 40f5b5d7b3
5 changed files with 1550 additions and 0 deletions

26
fibonacci.js Normal file
View File

@ -0,0 +1,26 @@
const assert = require("assert")
const fibonacci_rec = function(n) {
assert.ok(Number.isInteger(n), "n is not an Integer!")
if(n == 0) {
return 0
} else if (n == 1) {
return 1
} else {
return fibonacci_rec(n - 1) + fibonacci_rec(n - 2)
}
}
const fibonacci_opt = function(n) {
const g = function(n) {
return (1 + Math.sqrt(5)) / 2
}
assert.ok(Number.isInteger(n), "n is not an Integer!")
if(n == 0) {
return 0
} else if (n == 1) {
return 1
} else {
}
}

18
hello-world.js Normal file
View File

@ -0,0 +1,18 @@
/**
* WBE-Demo
* Einfacher Hello-World-Webserver
*/
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello, World!\n')
})
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`)
})

1453
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

6
package.json Normal file
View File

@ -0,0 +1,6 @@
{
"dependencies": {
"assert": "^2.0.0",
"jslint": "^0.12.1"
}
}

47
power.js Normal file
View File

@ -0,0 +1,47 @@
const { AssertionError } = require('assert')
const assert = require('assert')
const power_for = function (base, exponent) {
let result = 1
for (let count = 0; count < exponent; count++) {
result *= base
}
return result
}
const power_rec = function (base, exponent) {
if(exponent == 0) {
return 1;
}
return base * power_rec(base, exponent - 1)
}
const power_rec_opt = function (base, exponent) {
if(exponent == 0) {
return 1;
}
else if(exponent % 2 == 0) { //gerade
let value = power_rec_opt(base, exponent / 2)
return value * value
} else { //ungerade
return base * power_rec_opt(base, exponent - 1)
}
}
const power = function(base, exponent){
assert.ok(Number.isInteger(base), "Exponent is not an Integer")
assert.ok(Number.isInteger(exponent), "Exponent is not an Integer")
assert.ok(base >= 0)
assert.ok(exponent >= 0)
return power_rec_opt(base, exponent)
}
//console.log("With for Loop: " + power_for(2, 10)) // → 1024
//console.log("With recursion: " + power_rec(2, 10)) // → 1024
//console.log("With recursion optimized: " + power_rec_opt(2, 10)) // → 1024
// console.log(power(2, 10)) // → 1024
// console.log(power(2, 2.5)) // !! AssertionError
module.exports = { power }