Recently I took a challenge on freecodecamp to solve the given problem: "Sum All Odd Fibonacci Numbers"
Here is my code:
<html>
<head></head>
<body>
Sum All Odd Fibonacci Numbers of number <input type='number' id='val' min="0"/>
<button onclick="getOutput()">get Result</button>
<span id="output"></span>
<script>
function sumFibs(num) {
if(num < 2) {
return num;
}
var old = 0, now = 1, sum = 0;
for(var i = 0; i < num; i++) {
if(now <= num) {
if(now % 2 !== 0) {
sum += now;
}
temp = now;
now = now + old;
old = temp;
}
}
return sum;
}
function getOutput() {
var val = document.getElementById('val').value,
output = document.getElementById('output')
if(isNaN(val)) {
alert('not a number')
return
}
output.innerHTML = sumFibs(val);
}
</script>
</body>
</html>