Number Factorization (JavaScript)

Number Factorization (JavaScript)

I wrote a code that is supposed to do the following.

Write a program that calculates and displays all factors of a number with a function. A factor is any number that divides into a number evenly.

For examples: Factors of 20 are 1,2,4,5,10,20

For the program:

  1. Prompt the user for a number or use an input
  2. Call a function to calculate all factors using loops and conditionals and modulus
  3. Display all factors to the page

If the factor is Even : Print it in Green

If The Factor is Odd : Print it in Red

________________________________________________________________________________________

I tried everything but the code doesn’t seem to work, I would like to know whats wrong with the code.

The biggest issue I had is get the input to follow the script and I didn’t know how to do so.

My code:

<html>

<body>

<h3>A demonstration of how to access a Number field</h3>

<input type=”number” id=”myNumber” value=”2″>

<button onclick=”calculate(num)”>Try it</button>

<p id=”demo”></p>

<script>

function calculate(num) {

var half = Math.floor(num / 2), // Ensures a whole number <= num.

str = ‘1’, // 1 will be a part of every solution.

i, j;

// Determine our increment value for the loop and starting point.

num % 2 === 0 ? (i = 2, j = 1) : (i = 3, j = 2);

for (i; i <= half; i += j) {

num % i === 0 ? str += ‘,’ + i : false;

}

str += ‘,’ + num; // Always include the original number.

var x = document.getElementById(“myNumber”).value;

document.getElementById(“demo”).innerHTML = x;

}

}

calculate(232);

</script>

</body>

</html>

"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.


Discount Code: CIPD30



Click ORDER NOW..

order custom paper