# Final piece to my puzzle

Hi guys,
I’m new to coding and am trying to do something but can’t seem to figure out how.
The basic code:
Var z=pickRandom(2)
For(var I=100,I>0,i-20)
if(z<2)
print I
Else
Print(‘miss’)
if(I===0)
print (‘done’)
what I want to have happen is if z=1 it prints I and if it doesn’t it does not subtract 20 from I. After it loops once I want it to repick z and loop again.
The outcome should look something like:
100
Miss
Miss
80
60
40
Miss
20
Miss
Miss
Done
I know this is not the easiest but any help would be appreciated.

2 Likes

Hey there, very cool! You can try playing around with this in the Code Playground.

A couple quick notes:

• Be mindful of capitalization. `I` is not the same as `i`. JavaScript is case-sensitive.
• The 2nd part of a for loop’s setup tells the for loop to keep running as long as a certain condition is true. If that condition is `i > 0`, then the loop won’t run when `i === 0`, so the loop will never print `'done'`

Post a screenshot when your project is complete. Good luck!
Ben

I definitely will post it when I can figure it out. I will say the actual code does have i not I. Besides that though, it does end up printing ‘done’. Regardless I’m almost done, I just need the stupid difficulty implemented essentially. The difficulty is suppose to give a chance to miss or hit essentially, but I can’t crack how to do it for the life of me.

The line `var z = pickRandom(2)` is outside the for loop. This means it only runs once. `z` will be either `1` or `2` and will remain that number the entire time the code runs.

Move it inside the for loop to have it choose a number each time.

Hey there @Sindhu_Indurthi, try changing your notification settings. It seems like you are getting emails every time someone posts anything to the forum.

You can change your settings here.

This may seem stupid but where in the loop? Beneath it? Inside the restraints? Again I’m new to coding and almost all my knowledge comes from this app.

Inside the code block `{}` of the loop.

Only click below if you need extra help. It shows one possible solution, but you should try to work this out on your own as best as you can.

``````for (var i = 100; i >= 0; i = i - 20) {
var randomNum = pickRandom(2);
if (randomNum === 1) {
print(i);
} else {
print('Miss');
}
if (i === 0) {
print('Done');
}
}
``````

This is close to what I need but not quite. I have the random chance now which is perfect, but the code still subtracts 20 every time. It just says miss instead of the new value of i sometimes.
This is the current outcome:
100
Miss
Miss
40
Miss
Done
This is the outcome I need:
100
Miss
Miss
80
Miss
60
40
Miss
20
Done
This is where the main problem lies unfortunately. I have been spending so much time rewriting my code. This is really good practice and a great learning opportunity, but I’m dying to put on this last touch to the rest of my code.

So you want `i` to decrement by `20` only if `randomNum` is `1`.

This would be better in a `while` loop, but unfortunately we don’t have this in the app, as we don’t have any lessons that use it.

With a while loop, it would look like this:

``````var i = 100;

while (i >= 0) {
var randomNum = pickRandom(2);

if (randomNum === 1) {
print(i);
i = i - 20;
} else {
print('Miss');
}
if (i === 0) {
print('Done');
}
}
``````

You can try coding this outside of Grasshopper, for example, in your browser’s JS console. There are just 2 things to note:

`print()` and `pickRandom()` are functions we created for grasshopper, and can’t be used outside the app.

`print()` is just `console.log()` with a friendlier name. If coding outside of grasshopper, use `console.log()` instead whenever you want to print.

For `pickRandom()`, it’s a bit trickier, but you can write the `pickRandom()` function yourself with the following code:

``````function pickRandom(argument) {
if (typeof argument === 'number') {
return Math.floor(Math.random() * Math.floor(argument)) + 1;
}
if (Array.isArray(argument)) {
return argument[Math.floor(Math.random() * argument.length)];
}
}
``````

Good luck!
Ben

As promised, here is version 1.0 of the code I wanted to create:

``````///Fighting Game///
///version1.0///

//selfreminders//

//This is Javascript, not java./

//goals/thoughts//

//create a better visual of monster's health and the damage it took/
//create a way to have user input options for random occurences/
//create a way for user to tap in order to swing/
//create a way for the user to have health and be in danger of being attacked/
//give some sort of award and or other story elements like exp, areas to explore, a win condition/
//make more user friendly on looks too/

//functions to call//

var push={
push: function(){
},
};

function pickRandom(argument){
if (typeof argument === 'number'){
return Math.floor(Math.random()*Math.floor(argument))+1;
}
if (Array.isArray(argument)){
return argument[Math.floor(Math.random()*argument.length)];
}
}

//variables//

/difficulty/

var easy=8
var medium=6
var hard=4
var difficulty=[
easy,
medium,
hard,
];
let selecteddifficulty=[
];

/battle variables/

let damage=[
];

/monster variables/

var slime='slime';
var zombie='zombie';
var dragon='dragon';
var monster=[
slime,
zombie,
dragon,
];
let monsterhealth=[
];
let selectedmonster=[
];

/weapon variables/

var hammer='You attacked with a hammer!';
var sword='You attacked with a Broadsword!';
var dagger='You attacked with a dagger!';
var weapon=[
hammer,
sword,
dagger,
];
let selectedweapon=[
];

//selections//

/monster/

var surprisemonster=pickRandom(monster);
if(surprisemonster===zombie){
selectedmonster.push(zombie);
monsterhealth.push(100);
}

if(surprisemonster===slime){
selectedmonster.push(slime);
monsterhealth.push(60);
}

if(surprisemonster===dragon){
selectedmonster.push(dragon);
monsterhealth.push(140);
}

/difficulty/

var randomdifficulty=pickRandom(difficulty);
if(randomdifficulty===easy){
selecteddifficulty.push(easy);
console.log('easy');
console.log(selecteddifficulty);
}

if(randomdifficulty===medium){
selecteddifficulty.push(medium);
console.log('medium');
console.log(selecteddifficulty);
}

if(randomdifficulty===hard){
selecteddifficulty.push(hard);
console.log('hard');
console.log(selecteddifficulty);
}

/weapon/

var randomweapon=pickRandom(weapon);
if(randomweapon===hammer){
selectedweapon.push(hammer);
damage.push(20);
}

if(randomweapon===sword){
selectedweapon.push(sword);
damage.push(10);
}

if(randomweapon===dagger){
selectedweapon.push(dagger);
damage.push(5);
}

//gameplay//

console.log('Attack! It\'s a '+(selectedmonster+'!'));
while (monsterhealth>0){
console.log(selectedweapon);
var randomNum = pickRandom(10);
if (randomNum < selecteddifficulty){
monsterhealth=monsterhealth-damage;
console.log(monsterhealth);
}else{
console.log('Miss');
}
if (monsterhealth === 0){
console.log('Win');
}
}
``````
1 Like

Look’s great! Can’t wait to see it in stores!

-Ben

Idk if I should post here or a new post or what, but I’m deeper into some ideas and stuck. I’m trying to figure out how to find someone knowledgeable in javascript and html in order to ask questions to a lot. Almost like a mentor. Any suggestions cause my social media and phone contacts are devoid of coders.

While I’m not really sure where you can find a mentor, there are online communities that are much larger than this forum where you can learn a lot. Check out Reddit’s r/learnJavaScript subreddit, and join Stack Overflow.

You should be able to learn a lot there.

– Ben