I am new into javascript, trying to do my uni homework.
I have problem with max and min numbers, also cant get point 3. work. Other ones work properly, atleast i think so.
There is no errors in this one, but as answer he gives me only 0's, and max/min shows as +-infinity.
UPDATE: I used answer under question and placed max/min into aprekinat() function, but works only first half of IF, code after ELSE is not working. And if u write conosle.log(max) or min it shows that max/min is not defined.
function aprekinat() {
var max = Math.max(...numbers);
var min = Math.min(...numbers);
if (numbers => 1) {
var big = document.getElementById('big');
var bigger = numbers.map(x => x / max);
document.getElementById('big').innerHTML = bigger;
} else {
var small = document.getElementById('small');
var smaller = numbers.map(x => x / min);
document.getElementById('small').innerHTML = smaller;
}
}
CODE:
var numbers = []; function myFunction() { numvalue = parseInt(document.getElementById('num').value); numbers.push(numvalue); console.log(numbers) return false; } var max = Math.max(...numbers); var min = Math.min(...numbers); document.getElementById('apr').onclick = aprekinat; function aprekinat() { if (numbers => 1) { var big = document.getElementById('big'); var bigger = numbers.map(x => x / max); document.getElementById('big').innerHTML = bigger; } else { var small = document.getElementById('small'); var smaller = numbers.map(x => x / min); document.getElementById('small').innerHTML = smaller; } } document.getElementById('color').onclick = changeColor; var currentColor = "black"; function changeColor() { if (currentColor == "black") { document.body.style.color = "red"; currentColor = "red"; } else { document.body.style.color = "black"; currentColor = "black"; } }
<html> <head> <title>Ld_js_5</title> </head> <body> <form onsubmit="return myFunction()"> <input type="text" id="num"> <input type="submit" value="Submit"> </form> <p id="big"></p> <p id="small"></p> <button id="apr">Aprekinat!</button> <button id="color">Color!</button> </body> </html>
You're having you've placed the min() and max() calculations outside of any function, so they are calculating as soon as the page loads when the array is still empty.
Are they supposed to calculate when you press the "Aprekinat?" button, If so, you'll need to place lines 10 and 11 in your JavaScript inside of your aprekinat()
function, before the if/then statement. That way min
and max
will be re-evaluated every time you calculate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.