How to check if 2 strings are equal in JavaScript? - javascript

I'm a beginner in JS and trying to sort some cars by their model. The models are sorted by ranking in this order (Mercedes, BMW, Jeep, Nissan). I would like it to be case-insensitive. I went about it by creating a variable for creating the desired rankings.
var modelRanking = function(car) {
if (car.model.toLowerCase() === 'mercedes') {
return 1;
} else if (car.model.toLowerCase() === 'bmw') {
return 2;
} else if (car.model.toLowerCase() === 'jeep') {
return 3;
} else if (car.model.toLowerCase() === 'nissan') {
return 4;
} else {
return 5;
}
}
function modelComparator(car1, car2) {
if (car1.modelRanking < car2.modelRanking) {
return true;
} else if (car1.modelRanking > car2.modelRanking) {
return false;
} else if (car1.modelRanking == car2.modelRanking) {
return yearComparator(car1, car2);
}
}
However the modelRanking is always returning 5.

Instead of car1.modelRanking, use modelRanking(car1) because modelRanking is a function in global scope, not a property of car1.
function modelComparator(car1, car2) {
if (modelRanking(car1) < modelRanking(car2)) {
return true;
} else if (modelRanking(car1) > modelRanking(car2)) {
return false;
} else if (modelRanking(car1) == modelRanking(car2)) {
return yearComparator(car1, car2);
}
}

Related

why if condation not update the component

I did that code, but the table doesn't work according to the conditions.
mobilScreen: boolean = false;
pcScreen: boolean = false;
get mobilScreen() {
if (window.matchMedia("(max-width: 500px)").matches) {
return true;
} else {
return false;
}
}
get pcScreen() {
if ((this.mobilScreen)) {
return false;
} else {
return true;
}
}
I tried using forceUpdate() but it didn't work.

can we validate multiple functions inside if condition with AND operator using java script is there any other way to check multiple functions true?

I have added these functions and need return something based which function is returning true but it is not working.
//this is function1/
function A() {
return true;
}
function B() {
return true;
}
function C() {
if ({
{
var -customJS - page_type_lookup
}
} === 'product') {
var config = document.querySelector('#product-content > div.product-variations.clearfix > div.attribute.variant-dropdown > div.attribute-values-section > div.label.va-navSectionalOrientation').innerText;
if (config.includes('Sectional Orientation')) {
return true;
} else {
return false;
}
}
}
if (A() === true && B() === true && C() === true) {
return 'A+ Content, RTA Product, Sectional Configurator';
} else if (A() === true && B() === true) {
return 'A+ Content, RTA Product';
} else if (B() === true && C() === true) {
return 'RTA Product, Sectional Configurator';
} else if (C() === true && A() === true) {
return 'Sectional Configurator, A+ Content';
} else if (A() === true) {
return 'A+ Content';
} else if (B() === true) {
return 'RTA Product';
} else {
return 'Sectional Configurator';
}
}
If you have more than one function and a data set which reflects the wanted value of each flag, you could take an array for the functions retun values and another for the strings which are filterd and joined for the result.
const
flags = [a(), b(), c()],
result = ['A+ Content', 'RTA Product', 'Sectional Configurator']
.filter((_, i) => flags[i])
.join(', ');

parameter.includes() is not a function

I'm pretty new at js programming. I'm developing an admission form as part of our project to be submitted. It's also my first time asking a question here.
I'm creating a form of validation to prevent any invalid values to be entered. I also need some optimizations at my code. If you have any suggestions to make it shorter, I'll be glad to accept your suggestion too.
I tried executing the matchCheck() function on other scenarios, but it just works fine. I also tried executing validateDate() on the console and other scenarios, but it also worked without any errors. I got an error when the functions are executed at if statements.
Here is the error message: Unknown TypeError: arrayToBeChecked.includes is not a function
I got an error at these function and if statements:
function matchCheck(arrayToBeChecked, findingValue) {
return arrayToBeChecked.includes(findingValue);
}
if (matchCheck(date[0], "null") === false)
if (validateDate(bdate) === true)
Here is the code (Excluded some of the unrelated variables and codes):
//Check Match
function matchCheck(arrayToBeChecked, findingValue) {
return arrayToBeChecked.includes(findingValue);
}
//Date Validator
//Expected Format [MM/DD/YYYY]
function validateDate(date) {
//check if the date is valid
var leapYear = date[2] % 4;
var mos31 = ["1", "3", "5", "7", "8", "10", "12"];
var mos30 = ["4", "6", "9", "11"];
var flyInv = ["30", "31"];
var fnlyInv = ["29", "30", "31"];
var mos30Inv = "31";
if (matchCheck(date[0], "null") === false) {
if (matchCheck(date[1], "null") === false) {
if (matchCheck(date[2], "null") === false) {
if (leapYear == 0) {
if (date[0] == "2") {
if (matchCheck(flyInv, date[1]) === false) {
return true;
}
else {
return false;
}
}
else if (matchCheck(mos31, date[0]) === true) {
return true;
}
else if (matchCheck(mos30, date[0]) === true) {
if (matchCheck(mos30Inv, date[1]) === false) {
return true;
}
else {
return false;
}
}
else {
return false
}
}
else {
if (date[0] == "2") {
if (matchCheck(fnlyInv, date[1]) === false) {
return true;
}
else {
return false;
}
}
else if (matchCheck(mos31, date[0]) === true) {
return true;
}
else if (matchCheck(mos30, date[0]) === true) {
if (matchCheck(mos30Inv, date[1]) === false) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
}
else {
return false;
}
}
else {
return false;
}
}
else {
return false;
}
}
//Contact Number Validator
//Expected Format [09XXXXXXXXX]
function cnValid(nos) {
if (nos.value.length === 11) {
if(nos.indexOf("09") > -1) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
//Check for empty selects
function checkEmptySelects(el) {
var selects = document.querySelectorAll(el);
var i;
for (i = 0; i < selects.length; i++) {
if (selects[i].value == "0") {
return true;
}
else {
return false;
}
}
}
//Valid Relation Values
var vrv = ["mother", "father", "grandmother", "grandfather", "auntie", "uncle", "housemaid", "Mother", "Father", "Grandmother", "Grandfather", "Auntie", "Uncle", "Housemaid", "Aunt", "aunt", "brother", "Brother", "sister", "Sister", "cousin", "Cousin"];
//Start Review
function reviewForm() {
var letters = /^[a-zA-Z\s]*$/;
var mailFormat = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var noScFormat = /[^a-zA-Z0-9\\,\\.\s]/g;
//Perform Checks
if (checkEmptySelects("select") === false) {
if (ln.value.match(letters)) {
if (fn.value.match(letters)) {
if (mn.value.match(letters)) {
if (eml.value.match(mailFormat)) {
if (age.value >= 0) {
if (age.value <= 100) {
if (validateDate(bdate) === true) {
if (pob.value.match(noScFormat)) {
if (ca.value.match(noScFormat)) {
if (rlg.value.match(letters)) {
if (nsl.value.match(letters)) {
if (cmn.value.match(letters)) {
if (mo.value.match(letters)) {
if (cfn.value.match(letters)) {
if (fo.value.match(letters)) {
if (gn.value.match(letters)) {
if (matchCheck(vrv, rts) === true) {
if (cnValid(cn) === true) {
if (lrn.value.length === 12) {
//Submit Data to db if all of the requirements are passed.
}
else {
//Error Message;
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
else {
//Error Message
}
}
The error occurs when the "arrayToBeChecked" value that is passed to the "matchCheck()" function is not an arry. To fix this, you could convert "arrayToBeChecked" to an array if it's not already an array.
function matchCheck(arrayToBeChecked, findingValue) {
// Convert arrayToBeChecked to an array if it's not already an array
if (!Array.isArray(arrayToBeChecked)) {
arrayToBeChecked = [arrayToBeChecked];
}
return arrayToBeChecked.includes(findingValue);
}

I am currently reading Eloquent JavaScript and I'm trying to solve the recursion test, but my code keeps giving me an 'undefined' feedback

This is the sample of my code:
Function isEven(number) {
(number == 1) {
return false;
}
else if (number == 0) {
return true;
}
else {
number += 2;
return isEven(-number);
}
};
This is the result I keep getting:
Console.log(isEven(50)); // undefined
Try using;
function isEven(number) {
if(number == 1) {
return false;
}
else {
if (number == 0) {
return true;
}
else {
number -= 2;
return isEven(number);
}
}
}
console.log(isEven(50));
You had a lot of syntax error as well as logical error. Note this logic only works for positive number. Hope it helps.

sequential steps functions?

I want to do something that can be passed through a function immediately after a value
for example:
// my value
let str = "example";
// my func
let sum = value => {
if(value.length + 1 == 7) { return true; }
else{ return false; }
}
// I want it to work when I write it like this.
console.log( "awesome".sum() )
// I want it to work when I write it like this.
You need to add the method to String.prototype
String.prototype.sum = function()
{
return this.length + 1 == 7;
};
Or simply
String.prototype.sum = function()
{
return this.length === 6;
};
Add the function in String.prototype
String.prototype.sum = function(){
if(this.length + 1 == 7) { return true; }
else{ return false; }
}
console.log( "awesome".sum() )
console.log( "awesom".sum() )
You have to extend the String class.
Standard
let str = "example";
String.prototype.sum = value => {
if(value.length + 1 == 7) { return true; }
else{ return false; }
}
console.log( "awesome".sum(3) )
ES6
Object.assign(String.prototype, {
sum (value) {
if(value.length + 1 == 7) { return true; }
else{ return false; }
}
});

Categories

Resources