Javascript gives undefined instead of the ex.message - javascript

The code below works, only when I type a letter in the prompt(), I get undefined message instead of the WrongValue ex.message which is in the catch(ex). I have tried a lot of varations but I still dont know what is wrong. How do I do this correctly?
var myList = ["Oranges", "Apples", "Pineapples", "Bananas"];
var getFruit = function(index) {
if (index > myList.length || index < 0) {
throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
} else {
return myList[index];
}
if (isNaN(index)) {
throw new WrongValue("Give a number please");
} else {
return myList[index];
}
}
try {
getFruit(prompt("Which fruit are you looking for"));
} catch (ex) {
if (ex instanceof RangeError) {
console.log(ex.message);
}
if (ex instanceof WrongValue) {
console.log(ex.message);
}
}

check isNaN FIRST ... change getFruit to the following
var getFruit = function(index) {
if(isNaN(index)) {
throw new WrongValue("Give a number please");
else if(index > myList.length || index < 0) {
throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
} else {
return myList[index];
}
}

As written now always one of your condition that will be checked since they've return and throw the both prevent the code from continue to the next instruction, so try to conbine the both condition in one and check the isNaN() first, it like:
var getFruit = function(index) {
if (isNaN(index)) {
throw new WrongValue("Give a number please");
} else if (index > myList.length || index < 0) {
throw new RangeError("The number you gave doesn't exist in the list, the number must be 0 <= # <= " + myList.length);
} else {
return myList[index];
}
}
Hope this helps.

Related

Inside the function >>> TypeError: Cannot read property 'length' of undefined

This is a code to validate a credit card number regarding to given requirements. I made the code in a way that fits all check functions in the main function and it is working well in that way. However I wanted to tidy up my code a bit and make it better practice so the code is like this now. I think I have a part of functions that I still couldn't understand fully. Can you please tell me what is my mistake here?
Any input appreciated.
'use strict';
let cardNumLength = getLength();
let isNumOnly = /^\d+$/.test(cardNum);
let isLastDigitEven = isEndEven();
let isSumValid = isSumGreaterThan16();
let allDigitsNotSame = allEqualCheck(cardNum);
let errorArray = [];
function cardNumValidator(cardNum) {
if (cardNumLength, isNumOnly, isLastDigitEven, isSumValid, allDigitsNotSame) {
console.log(`"${cardNum}" is a valid credit card number.`);
return
}
return errorArray;
}
// getLength function to check if the number has 16 digits
function getLength(cardNum) {
//console.log(cardNum.length); //debugging
if (cardNum.length == 16) {
return true;
}
return false;
}
// to check the final digit if its even
function isEndEven(cardNum) {
if (cardNum[cardNum.length - 1] % 2 == 0) {
return true;
}
return false;
}
// to check if the sum of the digits are greater than 16
function isSumGreaterThan16(cardNum) {
let intCardNum = parseInt(cardNum);
let sum = 0;
for (let i = 0; i < cardNum.length; i++) {
sum = parseInt(sum) + parseInt(cardNum[i]); //parseInt() converts string into number
}
if (sum > 16) {
return true;
}
return false;
}
function allEqualCheck(cardNum) {
if (cardNum.split('').every(char => char === cardNum[0])) {
return false;
}
return true;
}
/* using switch statement to final validation regarding to the requirements those checked seperately by previous inner functions*/
function isValidError() {
if (cardNumLength === false) {
errorArray.push('Number must be 16 digits!');
} else if (isNumOnly === false) {
errorArray.push('Invalid characters!');
} else if (isLastDigitEven === false) {
errorArray.push('Odd final number!');
} else if (isSumValid === false) {
errorArray.push('Sum less than 16!');
} else if (allDigitsNotSame === false) {
errorArray.push('All numbers can not be the same!');
}
return errorArray;
}
cardNumValidator('9999777788880000'); //valid number example
cardNumValidator('6666666666661666'); //valid number example
cardNumValidator('a92332119c011112'); //Invalid number example
cardNumValidator('4444444444444444'); //Invalid number example
cardNumValidator('1111111111111110'); //Invalid number example
cardNumValidator('6666666666666661'); //Invalid number example
In the very first line you are not passing any arguments to the getLength function.
I'm using global variables like you did, but set them depending on the cardNum.
The cardNumValidator function will now always return an error array, which will have length zero when there is no error. When there are multiple errors, the errorArray will have all of them, not just a single one.
'use strict';
let cardNumLength = false;
let isNumOnly = false;
let isLastDigitEven = false;
let isSumValid = false;
let allDigitsNotSame = false;
let errorArray = [];
function cardNumValidator(cardNum) {
cardNumLength = getLength(cardNum);
isNumOnly = /^\d+$/.test(cardNum);
isLastDigitEven = isEndEven(cardNum);
isSumValid = isSumGreaterThan16(cardNum);
allDigitsNotSame = allEqualCheck(cardNum);
errorArray = [];
isValidError();
if (errorArray.length == 0) {
console.log(`"${cardNum}" is a valid credit card number.`);
} else {
console.log(`"${cardNum}" is an invalid credit card number.`);
console.dir(errorArray);
}
return errorArray;
}
// getLength function to check if the number has 16 digits
function getLength(cardNum) {
//console.log(cardNum.length); //debugging
if (cardNum.length == 16) {
return true;
}
return false;
}
// to check the final digit if its even
function isEndEven(cardNum) {
if (cardNum[cardNum.length - 1] % 2 == 0) {
return true;
}
return false;
}
// to check if the sum of the digits are greater than 16
function isSumGreaterThan16(cardNum) {
let intCardNum = parseInt(cardNum);
let sum = 0;
for (let i = 0; i < cardNum.length; i++) {
sum = parseInt(sum) + parseInt(cardNum[i]); //parseInt() converts string into number
}
if (sum > 16) {
return true;
}
return false;
}
function allEqualCheck(cardNum) {
if (cardNum.split('').every(char => char === cardNum[0])) {
return false;
}
return true;
}
/* using switch statement to final validation regarding to the requirements those checked seperately by previous inner functions*/
function isValidError() {
if (cardNumLength === false) {
errorArray.push('Number must be 16 digits!');
}
if (isNumOnly === false) {
errorArray.push('Invalid characters!');
}
if (isLastDigitEven === false) {
errorArray.push('Odd final number!');
}
if (isSumValid === false) {
errorArray.push('Sum less than 16!');
}
if (allDigitsNotSame === false) {
errorArray.push('All numbers can not be the same!');
}
return errorArray;
}
cardNumValidator('9999777788880000'); //valid number example
cardNumValidator('6666666666661666'); //valid number example
cardNumValidator('a92332119c011112'); //Invalid number example
cardNumValidator('4444444444444444'); //Invalid number example
cardNumValidator('1111111111111110'); //Invalid number example
cardNumValidator('6666666666666661'); //Invalid number example

How to check if string contains character at any point in javascript

I need to know if a string contains a character (one or multiple times) at any point of the string.
For example with the character "&":
"&hfds" is invalid, "%$/&h&" is invalid etc.
Im doing this as part of a password validation:
function applySpecialCharacterFilter(password) {
if (password.match(/([!,%,#,#,$,^,*,?,_,~])/)) {
return 1;
} else if(password.match(/([&])/)) {
throw new Error('Das Passwort enthält unerlaubte Zeichen.');
}
return 0;
}
in the first part it checks of the password contains any of the allowed characters, and then increments the value of the validation. But then passwords containing not allowed characters can pass.
With the else if im trying to catch it, but it only works if its not in a special character sequence like $%&
Thank you
Edit:
Here is the whole function:
function checkStrength(password){
var strength = 0;
var passwordMessage = $('#passwordMessage');
if (password.length == 0) {
result.removeClass();
return '';
}
if (password.length < 6) {
validPassword = false;
result.removeClass();
result.addClass('short');
return 'Too short';
} else if(password.length > 8) {
validPassword = false;
result.removeClass();
result.addClass('short');
return 'Too long';
} else {
strength += 1;
}
try {
strength += applyLowerAndUpperCaseFilter(password);
strength += applyNumbersAndCharactersFilter(password);
strength += applySpecialCharacterFilter(password);
strength += applyTwoSpecialCharacterFilter(password);
strength += applyAlphabeticalCharacterCriteria(password);
} catch(error) {
validPassword = false;
result.removeClass();
result.addClass('short');
passwordMessage.html('').append('<p>TPassword contains invalid characters!</p>');
return 'Invalid';
}
passwordMessage.html('');
if (strength <= 2) {
validPassword = false;
result.removeClass();
result.addClass('weak');
return 'Schwach';
} else if (strength <= 3 ) {
validPassword = true;
result.removeClass();
result.addClass('good');
return 'Good';
} else {
validPassword = true;
result.removeClass();
result.addClass('strong');
return 'Strong';
}
}
function applyLowerAndUpperCaseFilter(password) {
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))
return 1;
return 0;
}
function applyNumbersAndCharactersFilter(password) {
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/))
return 1;
return 0;
}
function applySpecialCharacterFilter(password) {
if (password.match(/^([!%##$^*?_~]+)$/)) {
return 1;
} else if(password.match(/([&])/)) {
throw new Error('Das Passwort enthält unerlaubte Zeichen.');
}
return 0;
}
function applyTwoSpecialCharacterFilter(password) {
if (password.match(/(.*[!,%,#,#,$,^,*,?,_,~].*[!,",%,#,#,$,^,*,?,_,~])/))
return 1;
else if(password.match(/([&])/))
throw new Error('Das Passwort enthält unerlaubte Zeichen.');
return 0;
}
function applyAlphabeticalCharacterCriteria(password) {
var quality = 0;
var sequences = [
'abcdefghijklmnopqrstuvwxyz',
'01234567890',
'!\"§$%/()=?'
];
var proceed = true;
for(var i=0; i<(password.length-3); i++) {
for(var index = 0; index < sequences.length; index++) {
var needle = password.substring(i, 3);
if(stripos(sequences[index], needle) != false) {
quality -= 1;
proceed = false;
}
if(proceed == false) break;
}
if(proceed == false) break;
}
return quality;
}
function stripos(f_haystack, f_needle, f_offset) {
var haystack = (f_haystack + '')
.toLowerCase();
var needle = (f_needle + '')
.toLowerCase();
var index = 0;
if ((index = haystack.indexOf(needle, f_offset)) !== -1) {
return index;
}
return false;
}
The messages and classes are for real time validation output only.
Rules:
The Password needs to be between 6 and 8 characters long.
It has to have at least 1 upper and 1 lower case character.
It has to have numbers.
It has to have at leats 1 special characters (2 give more value).
Only these special characters are allowed - _ . : , ; ! # § $ % / = ? #
The characters should not appear in a sequence if possible, so no abc,123,!§$ etc.
You have to anchor the first regex and add a quantifier:
if (password.match(/^([!,%,#,#,$,^,*,?,_,~]+)$/)) {
// here ^ ^ ^
The comma is not mandatory except if you want to match it:
if (password.match(/^([!%##$^*?_~]+)$/)) {
You can use indexOf method:
function applySpecialCharacterFilter(password) {
if (password.indexOf('&')>-1) {
throw new Error('Das Passwort enthält unerlaubte Zeichen.');
}
if (password.match(/^([!,%,#,#,$,^,*,?,_,~]+)$/)) {
return 1;
}
return 0;
}
and you need to change your Regexp according #M42 answer

Javascript Testing Corners of an Array (Grid)

I'm doing this project trying to reproduce Schelling's Segregation model. I have a function(below) that is testing to see if the four immediate adjacent cells of the array are either the same or different or empty compared to the current cell being tested.
There are four possible spots to be tested for every cell in the array. But on corners and side spots, obviously you cant test spaces that are out of bounds. So in the function, if it finds one of the out of bounds spaces it decrements the number total around the cell. However, it keeps crashing telling me that I have an Uncaught Reference Error: Cannot read property '0' of undefined. I can't tell why its crashing.
The final lines of this code take the number of goods(similar cells) and the total number of cells around it (empty cells do not count) and gets a percentage similar.
Any help would be appreciated into telling me why it might be crashing and giving me an error? Thanks!
model.Test = function( i, j )
{
var numberToTest= 4;
var goods= 0;
if ((i - 1) >= 0)
{
if (model.BoardArray[i-1][j] != "E")
{
if (model.BoardArray[i][j] == model.BoardArray[i-1][j])
{
goods++;
}
}
else
{
numberToTest--;
}
}
else
{
numberToTest--;
}
if((i + 1) < $("#BoardSizeValue").val())
{
if (model.BoardArray[i+1][j] != "E")
{
if (model.BoardArray[i][j] == model.BoardArray[i+1][j])
{
goods++;
}
}
else
{
numberToTest--;
}
}
else
{
numberToTest--;
}
if ((j - 1) >= 0)
{
if (model.BoardArray[i][j-1] != "E")
{
if (model.BoardArray[i][j] == model.BoardArray[i][j-1])
{
goods++;
}
}
else
{
numberToTest--;
}
}
else
{
numberToTest--;
}
if ((j + 1) < $("#BoardSizeValue").val())
{
if (model.BoardArray[i][j+1] != "E")
{
if (model.BoardArray[i][j] == model.BoardArray[i][j+1])
{
goods++;
}
}
else
{
numberToTest--;
}
}
else
{
numberToTest--;
}
var similar = $("#SimilarSlider").val()/100;
if (numberToTest == 0)
{
return false;
}
else
{
var needed = goods/numberToTest;
if (needed >= similar)
{
return false;
}
else
{
return true;
}
}
}
From looking at your code, you would only get a Reference Error: Cannot read property '0' of undefined. if i was out of the bounds of the array.
I think the problem might be in this part of the code:
if ((i - 1) >= 0) {
if (model.BoardArray[i-1][j] != "E") {
if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) {
if i = $("#BoardSizeValue").val() and $("#BoardSizeValue").val() is a one-based index of the array size, then [i-1] would be okay, but not [i]. So try adjusting your code to this:
if ((i - 1) >= 0 && i < $("#BoardSizeValue").val()) {
if (model.BoardArray[i-1][j] != "E") {
if (model.BoardArray[i][j] == model.BoardArray[i-1][j]) {
This would also apply to the j comparisons as well.

Where in the code to validate a phone number, in JavaScript orRazor?

This is my first webpage in which I prompt the user for a phone number to add to a Do Not Call List database. Everything is working so far but I need to add the following, which I can do following the advice in this answer
stripping the input from all characters except digits
validating that the resulting string is 10 digits long
Then, when telling the user that the number was added to the list, I want to present it in the (999) 999-9999 format.
Where should I add all that code? Iside the #{ } block? In JavaScript? Razor?
Check phone number
function IsNumber(s) {
var i, currentCharacter;
for (i = 0; i < s.length; i++) {
// Check that current character is number.
currentCharacter = s.charAt(i);
if (((currentCharacter < "0") || (currentCharacter > "9"))) {
return false;
}
}
// All characters are numbers.
return true;
}
function TestInternationalPhone(strPhone) {
var bracket = 3,
openBracket,
phoneNumberOnly,
phoneNumberDelimiters = "()- ",
validWorldPhoneChars = phoneNumberDelimiters + "+",
minDigitsInIPhoneNumber = 10;
strPhone = SOS.StringHelper.Trim(strPhone);
if (strPhone.length === 0) {
return false;
}
if (strPhone.indexOf("+") > 1) {
return false;
}
if (strPhone.indexOf("-") != -1) {
bracket = bracket + 1;
}
if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) {
return false;
}
openBracket = strPhone.indexOf("(");
if (strPhone.indexOf("(") != -1 && strPhone.charAt(openBracket + 2) != ")") {
return false;
}
if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) {
return false;
}
phoneNumberOnly = SOS.StringHelper.StripCharsInBag(strPhone, validWorldPhoneChars);
return (IsNumber(phoneNumberOnly) && phoneNumberOnly.length >= minDigitsInIPhoneNumber);
}

Flow of a program not going inside if condition in Javascript

Here is my code:
<script>
function monthassign()
{
document.getElementById("month").selectedIndex=0;
}
function isleap()
{
var yr=document.getElementById("year").value;
if ((parseInt(yr)%4) == 0)
{
if (parseInt(yr)%100 == 0)
{
if (parseInt(yr)%400 != 0)
{
//alert("Not Leap");
return "false";
}
if (parseInt(yr)%400 == 0)
{
//alert("Leap");
return "true";
}
}
if (parseInt(yr)%100 != 0)
{
//alert("Leap");
return "true";
}
}
if ((parseInt(yr)%4) != 0)
{
//alert("Not Leap");
return "false";
}
}
function dateassign()
{
var yr=isleap();
var mth=parseInt(document.getElementById("month").selectedIndex);
var dt=document.getElementById("date")
if(yr)
{
if(mth==2)
{
//alert(yr);
dt.options.length = 0;
for(i=1; i<30; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
}
if(yr==false && mth==2)
{
//alert("Second fun");
dt.options.length = 0;
for(i=1; i<29; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
if(mth==4 || mth==6 || mth==9 || mth==11)
{
dt.options.length = 0;
for(i=1; i<31; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
else
{
dt.options.length = 0;
for(i=1; i<32; i++)
{
dt.add(new Option(i,i), null) //add new option to end of "date"
}
return;
}
}
</script>
My problem is when the variable yr contains false value the first if condition gets executed in function dateassign(). When the yr contains false value it is expected to shift the program control to the code block if(yr==false && mth==2), but it's not happening. I'm fed up of this problem of execution of specific condition even if the condition is false and why the control is not going inside a specific if condition. Please help me out of this issue. Thanks in Advance.
true and false are not the same thing as "true" and "false". The first is a Boolean, but the second is a string. You should be returning Boolean values, so you'll need to replace each instance of "true" with true and "false" with false.

Categories