Create the following JS functions: A . Write a function named isPunct that accep
ID: 3937308 • Letter: C
Question
Create the following JS functions:
A. Write a function named isPunct that accepts a character and returns true if it is punctuation, false otherwise. Use a regular expression.
B. Write a function named compress that accepts a sentence containing blanks and punctuation and returns a string that is the sentence with all blanks and punctuation removed. This function must call isPunct.
C. Write a function named isPal that accepts a sentence containing blanks and punctuation and returns true if it’s a palindrome (Links to an external site.), false otherwise. Hint: Consider the reverseStr function from Project 3.
D. Write a function named sumOfDigits that accepts a positive integer and returns the sum of its digits.
E. Write a function named isHarshad that accepts a positive integer and returns true if it’s a Harshad number, false otherwise. A Harshad (Links to an external site.) number is a positive integer divisible by the sum of its digits.
F. Write a function named hailStoneSeq that accepts a positive integer and returns a string representing the hailstone (Links to an external site.) sequence starting at that number. Your function must use a while (or do while) loop that you have written.
Explanation / Answer
A)
function isPunct(myChar){
var pattern = /[.,/#!$%^&*;:{}=-_`~()]/;
return pattern.test(mychar);
}
B)
function compress(str){
for (var x = 0; x < str.length; x++)
{
if(isPunct(str.charAt(x))){
str = str.substr(0, index) + "" + str.substr(index+character.length)
}
}
}
D)
function sumOfDigits (val){
while (val) {
sum += val % 10;
val = Math.floor(val / 10);
}
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.