简体   繁体   中英

How can i solve this code? no errors probably bad code - javascript (array,numbers,max,min)

I am new into javascript, trying to do my uni homework.

  1. I need to get 5 numbers into array by using input(user gets them in by himself).
  2. Find max and min numbers from that array.
  3. If atleast 1 number in array is bigger then 0 then divide them by max number, if they are smaller then divide by min number.
  4. Show answers on page
  5. Make button that will change color of answers

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM