Please tell me what this code means [duplicate] - javascript

This question already has answers here:
What is the result of this javascript?
(3 answers)
Closed 5 years ago.
can you tell me what this code does as I need to understand it for a coding assessment. I only just started learning JavaScript
function getAttackString() {
var foo = "d323b8b34";
var bar = "x334q3j98";
return "The code is: "+(foo.substr(3,foo.length-6))+(bar.substr(2));
}
Thanks for your help

Start by looking up what .length and .substr are.
For example, what will foo.length give you? A good way to find out would be:
var x = "abc123";
console.log(x.length);
console.log is a command to print information to your web browser console, very helpful when doing debugging work.

Related

why do you have to add the [I]? [duplicate]

This question already has answers here:
How to get value at a specific index of array In JavaScript?
(8 answers)
Closed 2 years ago.
so I am currently learning javascript on Codecademy. And a weird kind of thing got introduced that I don't quite get.
if you look at the code. you see at the end in console that after logging animals it is a [i] like why is that there? I get that is has something to do with the for loop. But I don't quite understand like why or what that it does. I don't know if question is clear enough but if you just try to explain why it is there and what it does there. that would be greatly appreciated:)
const animals = ['Grizzly Bear', 'Sloth', 'Sea Lion'];
for (let i = 0; i < animals.length; i++){
console.log(animals[i]);
}
the bracket notation is used to specify the index of the array you are looping through. You can always read more about javascript arrays on w3schools

Google script change a value in my array unexpectedly [duplicate]

This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
Closed 5 years ago.
I am stuck with a part of my google script code where one of array elements changed unexpectedly. It looks like a bug to me, but I'd like to hear from here if it's really is or just my coding error that I overlooked. Of course, I'm hoping for a solution as well in any case.
Here is that part of the code.
if (chkIn) {arr[1] = importData[i][1]+'2';
} else {
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
arr[1] = importData[i][1]+'1';
Logger.log('---------------------------------------------------');
Logger.log((i)+' '+importData[i][1]);
Logger.log((i+1)+' '+importData[i+1][1]);
Logger.log((i+2)+' '+importData[i+2][1]);
Logger.log(arr[1]);
};
(The if statement doesn't seem relevant here, but I included it just in case.)
Here is the output.
2573 2017122103
2574 20171221041
2575 20171221042
20171221042
---------------------------------------------------
2573 2017122103
2574 20171221041
2575 20171221031
20171221031
I really have no idea how importData[i+2][1] changed its value to arr[1] (the number after 2575).
Thank you in advance.
Probably this is because in your case:
arr === importData[i+2]
So when you change arr[1] you also have changed importData[i+2][1].

why does coffeescript print to the console by default, and how to disable this? [duplicate]

This question already has answers here:
Prevent Node.js repl from printing output
(6 answers)
Closed 7 years ago.
Why does this code print the entire loop to the coffeescript console if i don't console.log explicitly?
multiples = (num * 10 for num in [0..1000])
how can i disable this behavior?
in Python this works without printing anything
multiples = [x*10 for x in range(1000)]
Try to add return true at the end.

Is there a way to use a variable by combining two other variables in JavaScript? [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 8 years ago.
My problem is best explained in code form.
<!DOCTYPE HTML>
<html>
<head></head>
<body>
<script>
var x = "x";
var y = 1;
var x1 = "something";
alert(x+y);
</script>
</body>
</html>
I would then want it to alert the value of x1, which in this case would be "something".
As you can see this obviously doesn't work but how would you do this in JavaScript? It would be much apreciated if you could keep you answer as simple as possible, as I'm new to programming.
EDIT:
I just want to clarify here that the above code isn't actually my project. I was just trying to explain my problem in a simple way so everyone would understand me. I'll put an example of my actual project code below.
value_+x=document.getElementById('square_+x');
Again I know this wont work but would you change this to
value_4=document.getElementById('square_4');
If x happend to be 4?
If it's a global window variable, you can do
window[x+y];
this ll help you provided the documentation as well
alert(eval(x+y))

searching values in array javascript [duplicate]

This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 9 years ago.
Is there a built-in function in javascript to do this or this is only the option to go? Please look at the code below:
var arr=[1,3,4,'+','-', or whatever]
function value_check(user_click){
var operators=['+','-','/','*','.']
for (var i=0;i<operators.length;i++){
if (arr[arr.length-1]==operators[i]){var value1='operator found';}
if (user_click==operators[i]){
var value2= value1;alert("consecutive operators"); break;
}
}
}
I think this code achieves what I intend to do but is there a simple and shorter way of doing this. In words, I want to achieve something like this:
if (arr[arr.length-1] && user_click BOTH ARE IN operators array)
alert("consecutive operators)
Yes, there are some options:
JavaScript indexOf()
jQuery.inArray()
arrayName.indexOf() is what you are looking for.

Categories

Resources