function lessThanNinety(num) {
//return true if num is less than ninety
//otherwise return false
//code here
}
Here is the problem, here is my solution.
function lessThanNinety(num) {
if (number < 90) {
return true;
}
else {
return false;
}
}
var number = 50;
Where am I going wrong? Thanks
Seems like you are using wrong variable name, change variable number to num:
function lessThanNinety(num) {
if (num < 90) {
return true;
}
else {
return false;
}
}
and you will need to call the function:
var number = 50;
lessThanNinety(number);
Related
I have this mobile number format
+14354444744
It can accept upto 15 digits and + is mandatory
no other characters or special characters should be there
function phonenumber(inputtxt) {
var phoneno = /^([+]\d{2})?\d{15}$/;
if(inputtxt.value.match(phoneno)) {
return true;
}
else {
return false;
}
}
Any solution thanks.
function phonenumber(inputtxt) {
if(inputtxt[0]==="+") {
number=inputtxt.substring(1,inputtxt.lenght);
if(Number(number)){
if (number.length >= 15){
return true;
} else {
return false;
}
} else {
return false;
}
} else {
return false;
}
}
console.log(phonenumber("+1433f3333333"));
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
I want to get the mousedown condition in for loop.
But I cannot get it even if I mousedown trigger element(ex:#mousedown_button).
How can I get mousedown condition after starting for loop?
$("#mousedown_button").mousedown(function() {
for_loop()
});
function for_loop() {
for (i = 50; i > 0; i--) {
// I cannot get the count_flag(always false even if I mousedown #mousedown_button)
count_flag = get_mousedown_condition();
if (count_flag == false) {
break;
}
console.log(i);
var start = new Date();
while ((new Date() - start) < 250);
}
}
function get_mousedown_condition() {
// get the count flag
$("#mousedown_button").mouseover(function() {
return false;
}).mouseleave(function() {
return false;
}).mousedown(function() {
return true
}).mouseup(function() {
return false;
});
return false;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='mousedown_button'>Click</div>
the way you tried, you bind all the event-handlers like 50 times and their return value will never enter your desired scope, because i am not sure what you try - i just leave this as an answer how you could store your "count_flag" and retrieve it in your loop
var count_flag = false;
function for_loop() {
for (i = 50; i > 0; i--) {
if (count_flag == false) {
break;
}
console.log(i);
var start = new Date();
while ((new Date() - start) < 250);
}
}
function set_mousedown_condition {
$("#mousedown_button").mouseover(function() {
count_flag = false;
}).mouseleave(function() {
count_flag = false;
}).mousedown(function() {
count_flag = true;
for_loop(); // start the loop also here instead of override the binding
}).mouseup(function() {
count_flag = false;
});
}
// initialize everything once
set_mousedown_condition()
Attempting to complete the algorithms on freeCodeCamp. I eventually found an approach that works, but i still don't understand why this method did not work for all cases.
function palindrome(str) {
var alphaNumericStr = str.replace(/\W/g,"");
var lowerCaseAlphaNumericString = alphaNumericStr.toLowerCase();
var arr = lowerCaseAlphaNumericString.split("");
arr.reverse();
var reversedString = arr.join("");
if(str === reversedString){
return true;
}
return false;
}
palindrome("race car");
You're comparing a string which has been stripped of spaces and converted to lowercase to the original string. Replace your conditional with:
if(lowerCaseAlphaNumericString == reversedString){
rethrn true;
}
return false;
Here's a little refactor if you're interested:
// ...
var reversedString = arr.join('');
return lowerCaseAlphaNumericString == reversedString;
demo
This is where you are going wrong if(str === reversedString)
Try this:
if(lowerCaseAlphaNumericString === reversedString) {
return true;
}
return false;
}
There could be another approach. In this approach, the corner cases are handled separately.
function check_Palindrome(input_str){
var astr = input_str.toLowerCase().replace(/\W/g,'');
var acount = 0;
if(astr==="") {
console.log("Not Palindrome.");
return false;
}
if ((astr.length) % 2 === 0) {
acount = (astr.length) / 2;
} else {
if (astr.length === 1) {
console.log("Palindrome.");
return true;
} else {
acount = (astr.length - 1) / 2;
}
}
for (var x = 0; x < acount; x++) {
if (astr[x] != astr.slice(-1-x)[0]) {
console.log("Not Palindrome.");
return false;
}
}
console.log("Palindrome.");
return true;
}
Try this code
var isEven = function(number) {
if (number % 2 === 0) {
return true;
} else if (isNaN(number)) {
return "Not a number";
} else {
return false;
}
}
isEven();
This must work for you. I got this one as correct !!!!
You get a ReferenceError because there is no isNan function. Its called isNaN.
var isEven = function(number) {
if (number % 2 === 0) {
return true;
} else if (NaN(number)) {
return "Your input is not a number!";
} else {
return false;
}
};
var isEven = function(number) {
// Your code goes here!
if(number%2){
return true;
}
else if(isNan (number)){
return "Your input is not a number.";
}
else{
return false;
}
};`enter code here`
Check it out:
var isEven = function(number) {
if (isNaN (number)) {
return "The input was not a number!";
}
else if (number % 2 === 0) {
return true;
}
else if (isNaN (number) === true) {
return "Your input isn't a number!";
}
else {
return false;
}
};