Q:

How to generate javascript code with javascript if posible

Im quite new at javascript so dont expect me to know much else than whats written :)
So i have a javascript file with a lot of javascript codes that almost looks the same except a number is changing with each element.
Most of my code are inside a function just not written since it's a cut out. 
The relevant part of my code looks something like:
    //Upgrade 1
    if (CREDITS >= UpgradePrice1 && Upgrade1 === 0){
        document.getElementById("Upgrade1").style.display = "block";}
    else{document.getElementById("Upgrade1").style.display = "none";}

    //Upgrade 2
    if (CREDITS >= UpgradePrice2 && Upgrade2 === 0){
        document.getElementById("Upgrade2").style.display = "block";}
    else{document.getElementById("Upgrade2").style.display = "none";}

    //Upgrade 3
    if (CREDITS >= UpgradePrice3 && Upgrade3 === 0){
        document.getElementById("Upgrade3").style.display = "block";}
    else{document.getElementById("Upgrade3").style.display = "none";

    And so on...

The values are assigned in a file named StandardValues.js:
var UpgradeName1 = "Craftmanship Lvl 1";
var UpgradePrice1 = 100;
var UpgradeContent1 = "+100% Gods Hands! <br> +20% Blessed Farmer!";
var Upgrade1 = 0;

var UpgradeName2 = "Craftmanship Lvl 2";
var UpgradePrice2 = 200;
var UpgradeContent2 = "+100% Gods Hands! <br> +20% Blessed Farmer!";
var Upgrade2 = 0;

And so on...

If it were a html code i tried to generate i would use a php while function and make it echo the same code with the changed number a specefic amount of times.
But since this is inside a javascript file i really dont think thats an option?
The javascript code from before is in a .js file.
I think a potential fix could be:
Inside a .php file:

<?php
$Upgrades = 10;
$CurrentUpgrade = 1;

while ($Upgrades >= $CurrentUpgrade){
echo "
<script>
function Upgrade1(){
//Upgrade ".$CurrentUpgrade."
    if (CREDITS >= UpgradePrice".$CurrentUpgrade." && Upgrade".$CurrentUpgrade." === 0){
        document.getElementById('Upgrade".$CurrentUpgrade."').style.display = 'block';}
    else{document.getElementById('Upgrade".$CurrentUpgrade."').style.display = 'none';}
}
</script>
";
$CurrentUpgrade ++;

}

?>

*Sry for any typo in this part, made it quite quick.
But i would quite like to keep the javascript code inside my .js file instead of having it in my .php file.
So to sum it up i would like to (If possible):

Keep all code inside the .js file
Generate javascript code with javascript code
Repeat the same javascript element with only a number changing with each element
Try to shorten the amount of code by doing this.
Still be able to use any of the assigned variables alone somewhere like in a buy function.

I hope i gave all relevant information :)
Thanks in advance.

A:

You can create a method (in index you are passing the number in the end of your variables):
function myFunction(newUpgradePrice, newUpgrade, index) {
    if (CREDITS >= newUpgradePrice && newUpgrade === 0){
        document.getElementById("Upgrade" + index).style.display = "block";}
    else{document.getElementById("Upgrade" + index).style.display = "none";
 }

Is This what you are looking for? Anything needing to be changed to meet your needs?

