The Student Room Group

Silver idea award

Stuck on the robotics section maker activation

Scroll to see replies

replying to this thread for the same answer :smile:
loops
Reply 3
Put
Rooms
On line 7
was this the part that you were stuck on?
Reply 5
Yes it is, when you have to pass the correct sound into the array. I know you do [1] but I can't remember how you call the array
(edited 5 years ago)
Original post by emmakola
Yes it is, when you have to pass the correct sound into the array. I know you do [1] but I can't remember how you call the array

I'm not quite clear what you mean by 'call the array'? Do you mean read an item of data from it? or add an item of data into it? or change an item at a position?

(nitpick: The word 'call' usually means 'do a thing' or 'execute an instruction' -- for example you'll often read/hear people say 'call a function' meaning running/executing the code inside that function.. so that doesn't really make sense with an array, which is just a list of items -- The kinds of things you'd normally do with an array would be: add/remove an item, modify (overwrite/change) some existing item, read an item, read its length, etc. )


Anyway, an array has a name just like any other variable. In fact, an array works just like a list-of-variables.

The only extra thing you need to do is add the square-brackets and your position number to the end, and then you can do all the normal things that you'd do with any other variable. e.g. https://repl.it/repls/FeminineExperiencedDecimals


var myArray = [ "dog", "cat", "bird" ];
myArray[0] = "fish";
console.log(myArray[0]);


The name of the array on its own means 'the complete array' (so the name of the array helps you do things like access/add/remove an item or get its length). The square brackets [ ] are for selecting a specific position, so you need the number for that position.

the square brackets can use just a plain number for the position - e.g [1], or they can be given another variable which contains a number (Like the 'counter' which you'd use in a "for"-loop -- e.g. [ i ] )

https://www.w3schools.com/js/js_arrays.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
(edited 5 years ago)
Reply 7
Aigghh??
Reply 8
Original post by winterscoming
I'm not quite clear what you mean by 'call the array'? Do you mean read an item of data from it? or add an item of data into it? or change an item at a position?

(nitpick: The word 'call' usually means 'do a thing' or 'execute an instruction' -- for example you'll often read/hear people say 'call a function' meaning running/executing the code inside that function.. so that doesn't really make sense with an array, which is just a list of items -- The kinds of things you'd normally do with an array would be: add/remove an item, modify (overwrite/change) some existing item, read an item, read its length, etc. )


Anyway, an array has a name just like any other variable. In fact, an array works just like a list-of-variables.

The only extra thing you need to do is add the square-brackets and your position number to the end, and then you can do all the normal things that you'd do with any other variable. e.g.


var myArray = [ "dog", "cat", "bird" ];
myArray[0] = "fish";
print(myArray[0]);



The name of the array on its own means 'the complete array' (so the name of the array helps you do things like access/add/remove an item or get its length). The square brackets [ ] are for selecting a specific position, so you need the number for that position.

the square brackets can use just a plain number for the position - e.g [1], or they can be given another variable which contains a number (Like the 'counter' which you'd use in a "for"-loop -- e.g. )

https://www.w3schools.com/js/js_arrays.asp
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

Thank ouu I'll see if that works.
Reply 9
This code doesn't work are you able to help?

That's not it. You must:
[ul]
[li]Loop through soundBlocks[/li]
[li]call the playSound() function[/li]
[li]Pass the correct sound from your sound array into it[/li]
[/ul]
TRY AGAIN


for (i=0;i<movementBlocks.length;i++) {
doMovement(movementBlocks)
}
for (i=0;i<sounfBlocks.length;i++){
playSound(soundBlocks);
}
soundBlocks[1]=happyBeep;
print(soundBlocks[1]);
var soundBlocks = [crash,happyBeep,whistle];
Original post by emmakola
This code doesn't work are you able to help?


[ul]
[li][/li][/ul]
[li][/li]
[li][/li]





for (i=0;i<movementBlocks.length;i++) {
doMovement(movementBlocks)
}
for (i=0;i<sounfBlocks.length;i++){
playSound(soundBlocks);
}
soundBlocks[1]=happyBeep;
print(soundBlocks[1]);
var soundBlocks = [crash,happyBeep,whistle];

You didn't mention the error(s) you're getting, but the obvious problem I can see is that you're attempting to use your soundBlocks array before you've even created it. (The 'var soundBlocks' line is the instruction which actually creates it - until that point it doesn't exist so the lines above that won't work if they try to use it before it exists..)

Remember that JavaScript reads things from top-to-bottom just like humans. Think of it like a 'recipe' perhaps; your program is a sequence of instructions which happen in the order you describe them. You can't put icing on a cake before you've mixed the ingredients together and baked it in the oven. The same applies here, if you're using a variable in a program then the variable needs to be created first. Just as with a recipe, sure you always consider the order in which things are happening!

Otherwise, if there are any different problems, it helps to describe what you're seeing (errors/unexpected behaviour) and what you'd expect instead..
Reply 11
Original post by winterscoming
You didn't mention the error(s) you're getting, but the obvious problem I can see is that you're attempting to use your soundBlocks array before you've even created it. (The 'var soundBlocks' line is the instruction which actually creates it - until that point it doesn't exist so the lines above that won't work if they try to use it before it exists..)

Remember that JavaScript reads things from top-to-bottom just like humans. Think of it like a 'recipe' perhaps; your program is a sequence of instructions which happen in the order you describe them. You can't put icing on a cake before you've mixed the ingredients together and baked it in the oven. The same applies here, if you're using a variable in a program then the variable needs to be created first. Just as with a recipe, sure you always consider the order in which things are happening!

Otherwise, if there are any different problems, it helps to describe what you're seeing (errors/unexpected behaviour) and what you'd expect instead..

Okay I'll change the order and put var soundBlocks at the top
I'm not good with JavaScript I normally use python. You may like the IDEA Duke of York challenge, if you complete the silver star can you include it on a CV.
Reply 12
Original post by winterscoming
You didn't mention the error(s) you're getting, but the obvious problem I can see is that you're attempting to use your soundBlocks array before you've even created it. (The 'var soundBlocks' line is the instruction which actually creates it - until that point it doesn't exist so the lines above that won't work if they try to use it before it exists..)

Remember that JavaScript reads things from top-to-bottom just like humans. Think of it like a 'recipe' perhaps; your program is a sequence of instructions which happen in the order you describe them. You can't put icing on a cake before you've mixed the ingredients together and baked it in the oven. The same applies here, if you're using a variable in a program then the variable needs to be created first. Just as with a recipe, sure you always consider the order in which things are happening!

Otherwise, if there are any different problems, it helps to describe what you're seeing (errors/unexpected behaviour) and what you'd expect instead..

Okay I'll change the order and put var soundBlocks at the top
I'm not good with JavaScript I normally use python. You may like the IDEA Duke of York challenge, if you complete the silver star can you include it on a CV.
Original post by emmakola
Okay I'll change the order and put var soundBlocks at the top
I'm not good with JavaScript I normally use python. You may like the IDEA Duke of York challenge, if you complete the silver star can you include it on a CV.

Python is definitely a much nicer language! Although if you're comfortable with Python, then there are quite a few similarities too, and I'm sure you'll get used to JavaScript once all of the braces and semicolons start to look more comfortable.

The IDEA thing seems like a good... idea.. to get people interested in writing code, I hope you're enjoying it!

(And thanks, although I already have a job working in JavaScript among other things, but I'm always glad to see things like that to get people interested and learning more about programming :smile: )
(edited 5 years ago)
Reply 14
It worked thank you, had to just put all my added code under var soundBlocks.
Reply 15
Original post by Camanero_An
what are you meant to do for fuction 2?

That's correct how you have done

lifesSupportCheck()

You've made a mistake on line 8 you've put two }}

Just remove the } from line 8 and keep the one on line 9

Then it should run
Reply 16
"That's correct how you have done

lifesSupportCheck()

You've made a mistake on line 8 you've put two }}

Just remove the } from line 8 and keep the one on line 9

Then it should run"


Wrong. The engine will not allow for it - you need to perform a specific task. I myself have struggled with this one, because when you follow the instructions it does not even work.

Anyone got answers for how to pass through this part? I really just want to move on at this point, IDEA's coding activities really are not the best sadly.
hello i try to put one of the previous code and i didn't really work could someone help fix it and understand y this doesn't work ??

var soundBlocks = [crash,happyBeep,whistle];
for (i=0;i<movementBlocks.length;i++) {
doMovement(movementBlocks)
}
for (i=0;i<soundBlocks.length;i ){
playSound(soundBlocks);
}
soundBlocks[1]=happyBeep;play(soundBlocks[1]);
(edited 5 years ago)
Reply 18
for (i=0;i<movementBlocks.length;i++) {
doMovement(movementBlocks)
}

var soundBlocks = [crash,happyBeep,whistle];

for (i=0;i<soundBlocks.length;i++){
playSound(soundBlocks)
}
soundBlocks[1]=happyBeep;
print(soundBlocks[1]);

Is this right?
Original post by T X M
for (i=0;i<movementBlocks.length;i++) {
doMovement(movementBlocks)
}
var soundBlocks = [crash,happyBeep,whistle];
for (i=0;i<soundBlocks.length;i++) {
playSound(SoundBlocks);
}

Can someone explain where i went wrong I have tried all methods failing me.


Remember that JavaScript is case-sensitive. That means soundBlocks is not the same thing as SoundBlocks - i.e. starting with an upper-case 'S' is different to starting with a lower-case 's'. You need to make sure that they are all consistent.

Quick Reply

Latest

Trending

Trending