can't complete the function on chainshot - javascript

i'm new to js and started trying to learn code with chainshot
there's a simple task that I can't pass:
Task description
Let's complete the isEqual function! If a is equal to b return true.
my code
function isEqual(a, b) {
if (a === b) {
console.log(" true ");
}
}
export default isEqual;

You just need to return true, instead of using console.log, and return false if a wasnt equal to b. console.log doesnt return a value, it just shows it on the console, as it's name suggest. Use the keyword return for that, and return the keyword true or false instead of a string, since you're expecting a boolean.
function isEqual(a, b) {
if (a === b) {
return true;
} else {
return false;
}
}
export default isEqual;
You can simplify the function by just returning a === b, like
function isEqual(a, b) {
return a === b;
}
export default isEqual;

Your function should return boolean value. But in your code function doesn't return any.
function isEqual(a, b) {
return a === b;
}
export default isEqual;

What website are you running that on? It worked for me.
Here is a simpler version of the code you're trying to do:
function isEqual(a, b) {
return a === b
}

Related

v-if function returning nothing, but it should

Running into an issue that's been bugging me for the past 2 hours.
I've got a function in a mixin like so:
checkPermissionMeetings(type,areaID){
if(this.$store.state.global.permissions.meetings===true) { //If user has basic meeting access
if(type==='write'){
Object.entries(this.$store.state.global.permissions.meeting_areas).forEach(([key, val]) => {
if (parseInt(key) === parseInt(areaID) && (val === 'write')) {
console.log('write_true')
return true;
}
});
}
if(type==='read'){
Object.entries(this.$store.state.global.permissions.meeting_areas).forEach(([key, val]) => {
if (parseInt(key) === parseInt(areaID) && (val === 'read' || val === 'write')) {
console.log('read_true')
return true;
}
});
}
}
},
I am calling it in a view component which renders a navigation bar:
<span v-for="area in area_meetings">
{{area.id}} {{$store.state.global.permissions.meeting_areas[area.id]}} //This is here so I can see that I am sending the correct values to the function
{{checkPermissionMeetings('read', area.id)}} //This is here so I can see the output, but it returns nothing
<router-link tag="v-list-item" link :to="'/meetings/'+area.id" class="ml-5" v-if="checkPermissionMeetings('read', area.id)">
<v-list-item-icon>
<v-icon>mdi-chair-rolling</v-icon>
</v-list-item-icon>
<v-list-item-title>{{area.name}}</v-list-item-title>
</router-link>
</span>
I've console logged the outputs as you can see in the code, and they are looking ok. I have also put the function in curly braces so I can see the return, but this returns nothing at all.
I can see them in my store
I'm even echoing them out on the render and the values match...
Is there something fundamental I'm missing here?
Because your return into forEach method, your main function doesn't return anything. You can try to save in some variable returned data and after if statements return it. Or more easy way it use another loop like for of
As claimed in other answers, using for would be working but I will describe why it won't work:
function functionWithForEach(arr) {
arr.forEach(a=>{
if(a == "matched"){
return "horrah";
}
})
// here it is anonymous function
//The return exits the current function which is anonymous so it doesn't get returned
}
function functionWithForOf(arr){
for(let a of arr){
if(a == "matched"){
return "horrah";
// here it is not anonymous function
}
}
}
function functionWithPlainFor(arr){
for(let index =0; index < arr.length; index++){
if(arr[index]=="matched"){
return "horrah";
}
}
}
function functionWithForEachVariable(arr) {
let wordMatched = "";
arr.forEach(a=>{
if(a == "matched"){
wordMatched = "horrah";
return "horrah";
}
}
)
// see how the return does let code below it to execute
console.log("I will be called no matter return is called inside forEach");
return wordMatched;
}
let matchingArr = ["mat","matched","not correct"];
console.log(functionWithForEach(matchingArr),"from forEach");
console.log(functionWithForOf(matchingArr),"from for-of");
console.log(functionWithPlainFor(matchingArr),"from plain for");
console.log(functionWithForEachVariable(matchingArr),"from forEach with Variable");
It is clear from these that we can't use forEach in this situation
If you wanna know more about these:
What does `return` keyword mean inside `forEach` function?
Short circuit Array.forEach like calling break
Why does this forEach return undefined when using a return statement

I have a function and I need to know when it will return true and not false

I have this funtion
function riddle (parme) {
if(parme !== parme) {
return true;
}
return false;
}
when will it return true (I dont know the language)
There is only a single value in JS which is not equal to itself which is NaN. Rest in all cases, it will print false
To check for NaN equality, You should use isNaN
function riddle(parme) {
if (parme !== parme) {
return true;
}
return false;
}
console.log(riddle(NaN));

How to reduce cognitive complexity of nested if

I have an if statement set up like this
if (A && B) {
// do something 1
} else {
if (B) {
// set some boolean to false
}
// do something 2
}
I'm wondering if I can lower the cognitive complexity? Right now this is a score of 4.
I would say the best way to lower the cognitive complexity is to use functions. This is similar to #GuerricP original answer, but handles the multiple case of do somthing 2
eg.
function doSomething2() {}
if (A && B) {
// do something 1
} else if (B) {
// set some boolean to false
doSomething2();
} else {
doSomething2();
}
This reduces complexity, because it's not obvious that there are 2 routes to doSomething2 in your original version.
Well you could have only one level of depth like this:
function originalImplementation(A, B) {
if (A && B) {
console.log("do something 1");
} else {
if (B) {
console.log("set some boolean to false");
}
console.log("do something 2");
}
}
function newImplementation(A, B) {
if (A && B) {
console.log("do something 1");
}
else if (B) {
console.log("set some boolean to false");
}
if (!A || !B) {
console.log("do something 2");
}
}
console.log("originalImplementation");
originalImplementation(0, 0);
originalImplementation(0, 1);
originalImplementation(1, 0);
originalImplementation(1, 1);
console.log("newImplementation");
newImplementation(0, 0);
newImplementation(0, 1);
newImplementation(1, 0);
newImplementation(1, 1);
I think this is the right way to do it and the cleanest.
const DoSomething = function(){}
if (A && B) {
}
else if (B) {
DoSomething();
}
else {
DoSomething();
}
Assuming you do one and only one thing for each case you can try decluttering the syntax:
One-liner if statements don't need the curly braces
You can avoid if, else if, etc. with an early return
const run = (a, b) => {
if (a && b) return fun1();
if (a) return fun2();
if (b) return fun3();
return fun4();
}
In this case I prefer using nested ternaries. Something usually considered as "bad practice" by tool makers and opinion leaders in the industry but I think with the right indentation they offer more decluttering opportunities:
const run = (a, b) =>
( a && b ? fun1()
: a ? fun2()
: b ? fun3()
: fun4());
Of course YMMV ;)
Setting the boolean first can setup a clearer if/else
if(!A && B) {
// set some boolean to false
}
if (A && B) {
// do something 1
} else {
// do something 2
}
Another strategy is to drop out of functions asap
e.g.
if(X) {
// do stuff
return;
}
if(Z)
{
// do different stuff
return;
}
// do default stuff
return;
This allows the reader to dismiss logic beyond the condition they are interested in
Finally you can also create functions with meaningful names rather than comments
if(X) {
return doSomething2();
}

Sum two numbers given as arguments

Is there a way to sum two numbers given one argument?
function sum(a) {
// code should go here
}
sum(5)(10)
This should return 15
What you're looking for is this
function sum(a) {
return function(b) {
return a + b;
}
}
Usage:
const mySum = sum(5)(10) // 15
const someSum = sum(5);
someSum(10) // 15
Example: https://repl.it/repls/LoneDramaticParticles
The only way I can think of is to return another function.
function sum(a) {
return (b) => {
return a + b
}
}
Not terribly extensible that way, perhaps if you added info about why you want to do this it'd help.
Yes, it's called currying. You can define it as follows:
function sum(a)
{
return (b) => {
return a + b;
};
}
console.log(sum(5)(10));

Trying to write a function in JS passing through true and false statement

I have a little question. I want to write a function in javascript and the function needs to take two extra parameters, for when the function is true and false. But I don't know how to write it. So, I want something like this:
function greater (para1, para2, casetrue, casefalse) {
if (para1 > para2) {
casetrue
}
else {
casefalse
}
}
Is this possible? Because I wrote that function and than I called the function with
greater(5,3, function() { return "is greater" }, function() { return "is smaller" }) and it didn't work.
Can anybody please help?
Thanks in advance
try this way, otherwise you'll be returning a function:
function greater (para1,para2,casetrue,casefalse) {
if (para1 > para2) {
return casetrue();
} else {
return casefalse();
}
}
To make the function greater return "is greater" or "is smaller" one must return the return value of the parameter functions:
function greater(para1, para2, casetrue, casefalse) {
return (para1 > para2) ? casetrue() : casefalse();
}
Tested on Firefox 24.0 / Linux
function greater (para1, para2, casetrue, casefalse) {
if (para1 > para2) {
return casetrue();
}
else {
return casefalse();
}
}
Like has been suggested in other answers, the greater function is returning functions instead of strings. If this is the desired behavior, you can call the returned function with an extra pair of brackets:
greater(5,3, function() {return "is greater"},function() {return "is smaller"})()

Categories