I need your help communty, i have written a code to reverse a function but the problem is that am not able to check for palindrome, any help will be appreciated.
here is the code to reverse a string
function reverseString(str) {
return str.split("").reverse().join("");
};
the problem is how to check for palindrome am totally confused. Sorry community i am suppose to run it against a test and it is not passing properly, that's the problem.
Here is the Test spec wriiten in jasmine.
describe("Produce the reverse order of a word: ", function () {
describe("Case for en empty string", function() {
it("should return null for empty string", function() {
expect(reverseString('')).toEqual(null);
});
});
describe("Case for palindromes", function() {
it("should return true for `anna`", function() {
expect(reverseString('anna')).toEqual(true);
});
it("should return true for `NaN`", function() {
expect(reverseString('NaN')).toEqual(true);
});
it("should return true for `civic`", function() {
expect(reverseString('civic')).toEqual(true);
});
});
describe("Case for normal words", function() {
it("should return `skoob` for `books`", function() {
expect(reverseString('books')).toEqual('skoob');
});
it("should return `nomolos` for `solomon`", function() {
expect(reverseString('solomon')).toEqual('nomolos');
});
it("should return `csim` for `misc`", function() {
expect(reverseString('misc')).toEqual('csim');
});
});
});
Here is a code written by a friend in ECMA6 and it works by using a single function block. i don't want to use his code so i don't look like copy cat and is against the rule anyway just want to write my own code. Though we are still allowed to write it in ECMA 5
here is his code
const reverseString = (stringToReverse) => {
reversedString = "";
if (stringToReverse) {
for (i = String(stringToReverse).length; i >= 0; i--) {
reversedString += stringToReverse[i];
}
} else {
return null;
}
if (reversedString.substring(9) === stringToReverse) {
console.log(true);
return true;
}
return `${reversedString.substring(9)}`;
};
module.exports = reverseString;
To be able to catch palindromes such that one, you have to also remove characters like comma and dot. A simple way to do this is removing non-alphanumeric characters:
function palindrome(str) {
var re = /[^0-9a-z]/gi;
var lowRegStr = str.toLowerCase().replace(re, '');
var reverseStr = lowRegStr.split('').reverse().join('');
return reverseStr === lowRegStr;
}
palindrome("A man, a plan, a canal. Panama");
EDIT: the regex used by OP was already fine. So the question is not really necessary. Nothing to be done.
Related
I got asked this in an Interview and I couldn't solve it. Was wondering if any of you guys can help me.
fn("hello").fn("world").fn("!!!").fn();
function fn (str){
// Enter Solution Here
}
The solution should return 'hello world !!!'.
I tried method chaining and was able to get a partially right answer which is as follows:
function fn(str) {
var string = str;
this.fn1 = function(str1) {
string += " "+str1;
return this;
}
this.fn = function() {
console.log(string)
}
}
new fn("hello").fn1("world").fn1("!!!").fn();
but as you can see I cant get it to work unless I use fn1 as the function to concat the string. Any help will be appreciated, thanks.
Have the function return an object with one fn method. If, when you call it, it has an argument, update the string, otherwise return the string so you can log it.
function fn(str = '') {
return {
fn: function (s) {
if (s) {
str += ` ${s}`;
return this;
}
return str;
}
};
}
const output = fn('hello').fn('world').fn('!!!').fn();
console.log(output);
Additional documentation
Template/string literals
You could return an object with two properties, one for returning the complete string and another for collecting parts and retuning the object.
function fn(str) {
const
fns = {
fn: function () {
return str;
},
fn1: function (s) {
str += ' ' + s;
return fns;
}
};
return fns;
}
console.log(fn("hello").fn1("world").fn1("!!!").fn());
I think this should do the trick:
function fn(s){
return new function(){
this.str = s;
this.fn = (ns) => {if(ns){this.str += " "+ns; return this;} else return this.str;};
}
}
let a = fn("hello").fn("world").fn("!!!").fn();
console.log(a);
Seems like you need to use objects
const generic = {
"fn1":null,
"current":"",
"fn": () => {
//what do you want to do with "this.current"?
}
}
function fn(str) {
var ret = generic;
ret.fn1 = (wa) =>{
var again = generic;
again.current +=wa;
return again;
}
ret.current += str;
return ret;
}
You can return an object with a .fn() method which will
check if an argument is passed in or not to determine when to terminate the chain or continue chaining.
When no argument is sent, then it simply returns the accumulated string.
Otherwise, it calls fn() function again to accumulate to the string and get the next copy of the same structure as before:
const result = fn("hello").fn("world").fn("!!!").fn();
console.log(result);
function fn (str){
return {
fn(nextString) {
if (nextString === undefined)
return str;
return fn(`${str} ${nextString}`);
}
};
}
Since this operation is immutable, it means each link in the chain is independent, therefore it is no problem with assigning to variables to continue with different chains:
const helloWorld = fn("hello").fn("world");
const one = helloWorld.fn("one").fn();
const two = helloWorld.fn("two").fn();
const three = helloWorld.fn("three").fn();
console.log(one);
console.log(two);
console.log(three);
function fn (str){
return {
fn(nextString) {
if (nextString === undefined)
return str;
return fn(`${str} ${nextString}`);
}
};
}
I'm defining a processor method that returns the lower-case version of the content to achieve this behaviour:
> phrase = new TranslatedPhrase("recognize", "reconocer");
> phrase.palindrome();
true
function reverse(string) {
return Array.from(string).reverse().join("");
}
function Phrase(content) {
this.content = content;
this.processor = function(string) {
return string.toLowerCase();
}
this.processedContent = function processedContent() {
return this.processor(this.content);
}
// Returns true if the phrase is a palindrome, false otherwise.
this.palindrome = function palindrome() {
return this.processedContent() === reverse(this.processedContent());
}
}
function TranslatedPhrase(content, translation) {
this.content = content;
this.translation = translation;
// Returns translation processed for palindrome testing.
this.processedContent = function processedContent() {
return this.processor(this.translation);
}
}
I also tried return this.string.toLowerCase(); but this also doesn't work.
The error I'm currently getting is this:
> let phrase = new TranslatedPhrase("recognize", "reconocer");
undefined
> phrase.palindrome();
Thrown:
TypeError: phrase.palindrome is not a function
Any suggestions as to what I'm missing are greatly appreciated, thx!
I've written the following function in Typescript:
public searchPosts(keyword ? : string): Post[] {
return this._posts.filter(function(post) {
if (post.title.search(new RegExp('money', 'gi')) >= 0) {
return post;
}
});
}
It's working just fine but, I need to make it a little dynamic so that instead of hardcoded value i.e. money, I can put my keyword variable in RegExp (new RegExp(keyword, 'gi')). But doing so does not return anything even for 'money' as a keyword.
Any ideas on what I'm doing wrong here?
This is how it should work
var keyword = '345';
var f = ['1234', '3456'].filter(function(post) {
return (post.search(new RegExp(keyword, 'gi')) >= 0);
});
console.log(f);
This is your function in pure JS
var posts = [{title: '1234'}, {title: '3456'}];
function searchPosts (keyword) {
return posts.filter(function(post) {
return (post.title.search(new RegExp(keyword, 'gi')) >= 0);
});
}
console.log(searchPosts('345'));
If this doesnt work, the problem is somewhere else ;].
I have written a function wherein I want the arguments to be concatenated with a space:
function sayIt(str) {
var stringCollection = [];
this.addToCollection = function(str){
stringCollection.push(str);
if(!str){ // for the () part in the call
return stringCollection.join(" ");
}
};
return this.addToCollection(str);
}
console.log(sayIt('my')('name')('is')('Harry')()); // should log "my name is Harry"
Its giving me a TypeError. Any idea, why?
You are not returning a function when you have a word as an argument -- try this;
function sayIt(str) {
var stringCollection = [];
function concat(str){
stringCollection.push(str);
if(!str){ // for the () part in the call
return stringCollection.join(" ");
}
return concat;
};
return concat(str);
}
alert(sayIt('my')('name')('is')('Harry')()); // should log "my name is Harry"
I don't quite fancy the design of your method because it appears fragile to me, but I am sure you have a rationale behind it.
Modify
this.addToCollection = function(str){
stringCollection.push(str);
if(!str){ // for the () part in the call
return stringCollection.join(" ");
}
};
To
this.addToCollection = function(str){
stringCollection.push(str);
if(!str){ // for the () part in the call
return stringCollection.join(" ");
}
return this.addToCollection;
};
Hi I have just started learning javascript and are a bit unsure about syntax.
I am doing the bowling Kata and are checking to see if a spare should be awarded.
Here is my jasmine test and my attempt at the syntax. Test:
it('can check for a spare', function() {
var game = new Game();
game.roll(7);
game.roll(3);
expect(game.rolls[2]).toEqual(10);
expect(game.isSpare()).toBe(true)
});
Prototype:
Game.prototype.isSpare = function() {
if (self.roll + self.roll) === 10
}
return true;
};
Sorry if this a bit of a noob question, but can't seem to find the answer anywhere.
Thak you
What i understand from your question, there should be a rolls property, and each roll function call should add to it. And isSpare should just check if rolls property is equal to 10.
Something like this;
Game = function() {
this.rolls = 0;
}
Game.prototype.roll = function(num) {
this.rolls += num;
};
Game.prototype.isSpare = function () {
if(this.rolls === 10) {
return true;
}
return false;
};
You can also shorten isSpare function like this;
Game.prototype.isSpare = function () {
return (this.rolls === 10);
};