Replace all 0 in an Array with random numbers

Okey, folks so here it is, a solution to replace all the 0os in an array with a number. In the beginning I succeeded replace all 0 with the same number. But then i wanted to replace them with different values, all based on a random number. Here is the solution, written in Javascript:

<html>

<head>
</head>

<body>
<script>

var myData = [];
myData.push(5, 10, 0, 0, 0, 30, 0, 0, 0, 0);

//kanske används vid ett senare tilfälle 
var random = Math.floor((Math.random() * 100) + 1);
var random1 = Math.floor((Math.random() * 100) + 1);
var random2 = Math.floor((Math.random() * 100) + 1);
//

console.log(myData);

for(var i = 0; i < myData.length; i++){
  for(nollor in myData){
    
  if(myData[i] === 0){
    myData[i] = random;  
    random = Math.floor((Math.random() * 100) + 1);   
    myData[i] = random;
    
  }

}

  console.log(myData[i]);
}

</script>
</body>
</html>
Advertisement

Start Learning Programming And Code On Your Mobile Device

If you want to learn and code on the go, on your mobile device, then I got some news for you.

A friend of mine tipsed me about learning-to-code apps that are available for both Android and iOS.

Their developer is Sololearn Inc and the apps are to learn Javascript, Java, C++,C#, HTML,CSS,Python, PHP and SQL; they are called “Learn”+ programming language.

And as I mentioned, the apps are available for both iOS and Android. Good luck!

P.S. You can also code and visit your profile fro your PC or Mac by visiting SoloLearn.com.

Basic C# Tutorial Online

Just ahead of a new semester and new development techniques that we will go through, this tutorial may give you a hint when it comes to C#.
App development for on-premise Sharepoint(2010 – 2013) uses C# as main language and that’s why it feels that this is the right time to take a look at what it is and how it looks.

Regissör: Mosh