A variable should have a comprensible name, for that minimal have to complete the follow rules:
To Concateneid a string in JavaScript it's just necesary the simbol + example:
<script>
// Code of
function Concatenation(){
let name = prompt("Type your name");
let gretting = prompt("Type a gretting");
//To get the length of a word we just add
// the keyword "length" at the endword, example name.length
alert(name + ": " + gretting + ', for you!'+
"\nname.length: " + name.length +
"\ngretting.length: " + gretting.length
)
}
</script>
Functions are list of instrucions packedn, they could be called when it's necesary; example:
The follow button have a funtion called "sayHello()"
<script>
// Code of
This is the function
//use the keyword function its mean we're going to create a funtion
function sayHello(){ //The next word is the function name sayHello
//After the name, it's follow the parethesis (params), after that the brackets {Your code will be here }
//Params, are data for the function, but for this function any parameter has been requerid
//Insade you can write all the instructions or code you want
//Here i created an alert where say "Hello!"
alert("Hello!");
};
</script>
<button type="button" onclick="sayHello()">I'm a button with a function without params</button>
To call a funtion we just have to write the funtion name as in the button is
onclick="sayHello()"
<script>
function sayHello2(gretting){
alert("Hello " + gretting + "!");
//Here the function requered a param gretting
//The param "gretting" it's used in the gretting
};
</script>
To call this funtion it's necesary add the data for parameter,
it's mean the argument, in this case the argument for the parameter was Rychy
<button type="button" onclick="sayHello2('Rychy')">I'm a button with a function with a param</button>
//You can create a funtion, with optional arguments,
it's mean, with parameters who can get no argument
<script>
//Here the parameters a and b have a predeterminated argument, are 4 and 5
function sum(a=4,b=5){
return a + b;
}
function SumToNums(){
let a = Number(prompt("Give me a number"));
let b = Number(prompt("Give me a number"));
//At this point, we call the function sum() without args and the result is 9
alert("Calling the function 'sum' without arguments, result: "+sum());
//Here we calling the function sum() with the collect args and the result isn't 9
alert("The sum function with arguments, a="+a+" + a="+b+" is: "+sum(a + b));
}
</script>
The button who call the function responsable to do the sum
<button type="button" onclick="sumToNums()">Sum to Numbers<button>
Slice is a funtion to cut strings
<script>
// Code of
function sliceF(){
let name = prompt("Type your name: (With min 5 characters) ");
if (name.length < 5){
name = name + "-ADDING"
}
alert("Hello " + name + "!\n"+
"\nname.slice(0,1): " + name.slice(0,1) +
"\nname.slice(4,5): " + name.slice(4,5) +
"\nname.slice(1,5): " + name.slice(4,5)
);
alert(
prompt("Type a long text here: ").slice(1,140)
);
}
</script>
In js, instance of make an manual increment, it's offer a short method to that, as it's come:
//Manual way to make an increment or decrement
let a = 1;
a = a + 1; //Increment a = 2
a = a - 1; //Decrement a = 1
let b = 3;
A at hits point is = 1
a = a + b; //Sum a = 4
a = a - b; //rest a = -2
a = a * b; //multiply a = 3
a = a / b; //divition a = 0.33
//Shortest way to make an increment or decrement
let a = 1;
a++; // Increment a = 2
a--; // Decrement a = 1
let b = 3;
A at hits point is = 1
a += b; //Sum a = 4
a -= b; //rest a = -2
a *= b; //multiply a = 3
a /= b; //divition a = 0.33