a javascript exercise that imitates "include()" [duplicate] - javascript

This question already has answers here:
Function with forEach returns undefined even with return statement
(5 answers)
Why does this forEach return undefined when using a return statement
(5 answers)
Short circuit Array.forEach like calling break
(30 answers)
Closed 8 days ago.
I'm learning the basics of javascript and I ran into this exercise which confused me a bit. The exercise is to manually create a method like "includes." So when I implement it like that:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(arrr.includes(2));
it works.
but when I implement it like that, which makes much more sense to me:
function includes(num,arr) {
arr.forEach(val => {
if (val===num) return true;
return false;
})
}
const arrr=[1,2,3,4];
console.log(includes(2,arrr));
, it doesn't and the output is "undefined."
Can someone explain me the logic behind it? the first way doesn't even matches the functions arguments and works, while the second way does match the arguments and doesn't.
Thanks.

Related

JavaScript Rest operator [duplicate]

This question already has answers here:
Spread Syntax ES6
(8 answers)
Closed 7 months ago.
I am trying to learn javascript but having trouble in the spread and rest operators.
Can't understand whats happening here how dose this take in the taxRate parameter like a singel number when we spred the itemsBought parameter
function addTaxToPrices (taxRate, ...itemsBought)
{
return(itemsBought.map(item => item*taxRate));
}
let ShoppingCart=addTaxToPrices(1.1,46,89,35,79);
console.log(ShoppingCart)
for my understanding it is nearly the same than if you do:
function addTaxToPrices (taxRate, itemsBought)
{
return(itemsBought.map(item => item*taxRate));
}
itemsBought = [46,89,35,79]
let ShoppingCart=addTaxToPrices(1.1,itemsBought);
console.log(ShoppingCart)

return a function inside a function [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
Two sets of parentheses after function call
(4 answers)
Closed last year.
source code is here. The part I'm having trouble with is this:
export function forbiddenNameValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): ValidationErrors | null => {
const forbidden = nameRe.test(control.value);
return forbidden ? {forbiddenName: {value: control.value}} : null;
};
}
The part I don't understand is the return part. We're returning a function within a function, and it confused me a lot. I've looked around a bit but haven't been able to find any references to this topic. can you help me?

Recursion function javaScript [duplicate]

This question already has answers here:
Recursive function returns undefined
(3 answers)
Closed 1 year ago.
i tried to write this function for guessing if a given number is even or not using recursion,
This is my code :
function isEven(N){
if(N===0){
return true;
}else if(N===1){
return false;
}else {
isEven(N-2);
}
}
console.log(isEven(1)); // gives false
console.log(isEven(0)); //gives true
console.log(isEven(10)); // gives undefined
I dont know why the function is not working except for 0 and 1.
Because the isEven() funtion enters a loop and cannot send anything to the output. Try to change your function and write it in other way

ES6 Arrow function with brackets [duplicate]

This question already has answers here:
Curly Brackets in Arrow Functions
(3 answers)
Why doesn't my arrow function return a value?
(1 answer)
Closed 6 years ago.
I came across a little problem in my code which was kind of confusing to me and hope someone could explain why it does the things it does.
Code 1
sendText(){
return this.http.get('/api')
.map((response:Response) => response.json());
}
Code 2
sendText(){
return this.http.get('/api').map((response:Response) => {
response.json();
});
}
The key difference between these two code is that in Code 2 I placed the brackets after the arrow function to add my tasks inside those brackets and in Code 1 I took the brackets out and place the task on one line.
My question is why does my object coming from the server side coming back as undefined in Code2 with the subscribe method that angular2 provided while Code1 returns the object I suspect.
(response:Response) => response.json()
This is shorthand for this:
(response:Response) => { return response.json(); }
The {} let you add multiple statements inside the block. Without them, the function just runs the one statement and returns its value.
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

JavaScript semicolon after a function [duplicate]

This question already has answers here:
Why should I use a semicolon after every function in javascript?
(9 answers)
Do we need semicolon at the end? [duplicate]
(6 answers)
Closed 9 years ago.
I wonder the difference between two codes below in JavaScript, welcome answer.
Code 1:
function Add(a,b) {
return a + b;
}
Code 2:
function Add(a,b) {
return a + b;
};
The second code contains an empty statement. You can add as many as you want.
function Add(a,b) {
;;;;;
return a + b;;;;;;;
};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
First one is the standard method
Second one is not recommended.. Use the first method
Further reading - Why should I use a semicolon after every function in javascript?
There is no difference. The ; in the second example just creates an empty statement there.

Categories