way to recursion to reduce string or array?
example case:
if we have. variable abcdfgh4zi output must be f4z
BECAUSE after d it must be e and then after h it must be I
it likes sorting...
I am trying it but failed using charCodeAt
function reduce(data) {
let result = ""
if (data.length <= 1) {
return result
} else if (data.charCodeAt(data[0])+1 !== data.charCodeAt(data[1])) {
result += data[0]
}
return result + reduce(data.slice(1))
}
console.log(reduce('abcdfgh4zi')); // f4z
the result is the same like that data, and if I am using it <, it gonna be empty string
if data "lmnop" the result is true and it gonna return empty string because they are sorting
and here to find the next alphabet I found on SO If chartCodeAt doesn't work
var abc = (parseInt(data[0], 36) + 1) % 36;
var nextAlphabet = ((!abc * 10 + abc).toString(36));
charCodeAt expects an index, you are giving it the character. So you are doing data.charCodeAt("a") + 1 !== data.charCodeAt("b")
You are also storing the first character, so you would be storing "d" and not "f"
function reduce(data) {
let result = ""
if (data.length <= 1) {
return result
} else if (data.charCodeAt(0)+1 !== data.charCodeAt(1)) {
result += data[1]
}
return result + reduce(data.slice(1))
}
console.log(reduce('abcdfgh4zi')); // f4z
Related
This question already has answers here:
Why do empty JavaScript arrays evaluate to true in conditional structures?
(7 answers)
Closed 1 year ago.
I don't know why but I always receive an error when I run the following code:
function list(names) {
let str = '';
if (!names) {
return ''
} else if (names.length == 2) {
return names[0].name + ' & ' + names[1].name;
} else if (names.length == 1) {
return names[0].name;
} else {
for (i = 0; i < (names.length - 2); i++) {
str += names[i].name + ', ';
}
str += names[names.length - 2].name + ' & ' + names[names.length - 1].name;
return str;
}
}
with test cases in codewars kata.
The error is:
TypeError: Cannot read property 'name' of undefined
at list
at /home/codewarrior/index.js:36:19
at /runner/frameworks/javascript/cw-2.js:152:11
at Promise._execute
names should be an array which can be empty and the array elements are objects with name properties.
As an alternate solution, you can do with map, lastIndexOf and slice.
You have to encounter 2 corner cases:
1) An input array of zero-length
then you've to return empty string as
if (!names.length) return "";
2) An input array of length one, then you have to just return the name as:
if (names.length === 1) return names[0].name;
function list(names) {
if (!names.length) return "";
if (names.length === 1) return names[0].name;
const namesStr = names.map((o) => o.name).join(", ");
const index = namesStr.lastIndexOf(",");
const result = namesStr.slice(0, index) + " &" + namesStr.slice(index + 1);
return result;
}
const result = list([{ name: "Bart" }, { name: "Lisa" }, { name: "Maggie" }]);
console.log(result);
The kata mentions that you should return an empty string for an empty array. I think you intended to do that with
if (!names) {
return ''
}
... but an empty array is still truthy. Perhaps that line should be if (!names.length).
I tried to use a recursive function to reverse a number it works but for one call only it's because of scoping i guess but i don't know how to fix it
let num;
let reversed='';
let result;
function reverseNum(n){
for(let i =0; i<n; i++){
num = n%10; // get the last digit e.g 352 %10 = 2
reversed+= num
result = parseInt(n / 10); // remove last digit e.g. parseInt(352/10) = 35
reverseNum(result);
if(result ===0){
break;
}
}
return reversed;
}
You need the num, reversed, and result variables to be created anew each time the function is called externally. Here's one simple tweak, by defining the recursive function inside the top reverseNum function:
function reverseNum(n) {
let num;
let reversed = '';
let result;
const recurse = (n) => {
for (let i = 0; i < n; i++) {
num = n % 10; // get the last digit e.g 352 %10 = 2
reversed += num
result = parseInt(n / 10); // remove last digit e.g. parseInt(352/10) = 35
recurse(result);
if (result === 0) {
break;
}
}
return reversed;
};
return recurse(n);
}
console.log(reverseNum(1234));
console.log(reverseNum(1234));
But a more elegant method would be:
function reverseNum(n, str = String(n)) {
const thisDigit = str[str.length - 1];
const recursiveResult = str.length === 1 ? '' : reverseNum(str.slice(0, str.length - 1));
return Number(thisDigit + recursiveResult);
}
console.log(reverseNum(1234));
console.log(reverseNum(1234));
function reverse(number){
let index = 0 ;
let reversed = '';
let max = `${number}`.toString().length-1;
while(index <= max ){
reversed += `${number}`.charAt(max-index)
index ++;
}
return reversed;
}
console.log(reverse(546))
CertainPerformance has explained why your code wasn't working.
Here is another implementation, one I find reasonably simple:
const reverseNum = (n) =>
n < 10
? String(n)
: String (n % 10) + reverseNum (Math .floor (n / 10))
console .log (reverseNum (8675309))
Note that this returns a String rather than a Number. If we wanted to, we could make this a private function and make a public function one which called this and converted the result back into a number. But that would have the weird effect that reversing, say, 1000 would yield 1, since 0001 is simplified to 1. And that would mean that when you reverse again, you don't get anything like your original value. So I choose to keep with a String.
Of course if we're going to do String reversal, perhaps we're better off just using a String reversal function in the first place:
const reverseStr = (s) =>
s.length == 0
? ''
: reverseStr (s .slice (1)) + s [0]
const reverseNum = (n) =>
reverseStr (String(n))
console .log (reverseNum (8675309))
Or if we weren't interested in doing this recursively, we could just write the more common string reversal function:
const reverseStr = (s) =>
s .split ('') .reverse () .join ('')
const reverseNum = (n) =>
reverseStr (String (n))
console .log (reverseNum (8675309))
I'm practicing recursion, and am trying to use it to constantly add individual digits in a number until there is only 1 digit left.
Basically, if the number is 84, it becomes 8+4 = 12 which then becomes 1 + 2 = 3.
Below is my attempt on it. Not sure what I'm missing..
const weirdSum = (num) => {
let result = 0;
const split = num.toString().split('');
if(split.length > 1){
for(let i=0;i<split.length;i++){
result = result + (split[i]*1);
}
weirdSum(result); // pass result as argument, which will be split.
}
return result; // return result if split.length is 1
}
there are 2 mistakes, one you need to return weirdSum(result);
another you are returning result which is 0 you should return num
const weirdSum = (num) => {
let result = 0;
const split = num.toString().split('');
if(split.length > 1){
for(let i=0;i<split.length;i++){
result = result + (split[i]*1);
}
return weirdSum(result); // pass result as argument, which will be split.
}
return num; // return result if split.length is 1
}
console.log(weirdSum(84));
let weirdSum = num => {
const split = num.toString().split('');
if(split.length > 1){
const sum = split.reduce((acc, it) => parseInt(it) + acc, 0)
return weirdSum(sum);
}
return num;
}
console.log(weirdSum(84));
console.log(weirdSum(123456));
const weirdSum = (num) => {
let result = 0;
const split = num.toString().split('');
if(split.length > 1){
for(let i=0;i<split.length;i++){
result = result + (split[i]*1);
}
return weirdSum(result); // pass result as argument, which will be split.
}
return split[0]; // return result if split.length is 1
}
console.log(weirdSum(84))
I just changed your codes to works properly, I have no idea it is optimized or not.
You need ti return the split[0] when recursion stack ends not the result!
This function isn't anonymous but it solves your problem
function weirdSum(num) {
if(parseInt(num/10) == 0) {
return num;
}
var num1 = 0;
while(num != 0) {
var d = parseInt(num%10);
num1=num1+d;
num=parseInt(num/10);
}
return weirdSum(num1);
}
How it works ?
Any single digit number when divided by 10 gives 0 as quotient ( parsing to int is required ), function exists when this condition is met ( the first if ).
In the while loop I'm extracting digits of the number ( starting from the last digit ), when we divide a number by 10, the remainder is always the same as the last digit, then we are adding this to the new num ( num1 ).
In the last step of the while loop, we are shortening the number by removing is last digit by dividing it by 10 and repacing the old num1 by quoatient.
const weirdSum = num => {
// figure out the sum
const sum = [...num + ""].reduce((a, e) => a + (e - 0), 0);
// recurse if necessary
return sum < 10 ? sum : weirdSum(sum);
}
console.log(weirdSum(84));
This question already has answers here:
Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?
(6 answers)
Closed 3 years ago.
I'm literally stuck at solving this simple question. Anyway I found out another way to solve this but I couldn't figure out the issue with my code.
function charCout(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] === " ")
{
continue;
}
else{
if(str[i] in Object.keys(f))
{
f[str[i]] += 1;
}
else
{
f[str[i]] = 1;
}
}
}
return(f);
}
input: charCout("my name is Khan23")
expected output: {2: 1,3: 1,a: 2,e: 1,h: 1,i: 1,k: 1,m: 2,n: 2,s: 1,y: 1}
what i got: {2: NaN,3: NaN,a: 1,e: 1,h: 1,i: 1,k: 1,m: 1,n: 1,s: 1,y: 1}
Simply see if the property exists by type coercion like: if(f[str[i]])
function charCount(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] === " ")
{
continue;
}
else{
if(f[str[i]])
{
f[str[i]] += 1;
}
else
{
f[str[i]] = 1;
}
}
}
return(f);
}
console.log(charCount('the value is 0000'))
You are using the wrong to way to check if key is in object or not.
You can contract your if-else to single line using || operator.
f[str[i]] = f[str[i]] + 1 || 1
It checks whether f[str[i]] already exists or not. If str[i] is not key of f then f[str[i]] will return undefined and undefined + 1 will be NaN.
NaN is a falsy value so NaN || 1 will evaluate to 1.
If the f[str[i]] exists then it will return any number greater than 0 so it will be set to f[str[i]] + 1(incremented by one)
function charCount(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] !== " ")
{
f[str[i]] = f[str[i]] + 1 || 1
}
}
return(f);
}
console.log(charCount("my name is Khan23"))
Explanation:
You can also use reduce().
const charCount = str => str
.split(' ')
.join('')
.split('')
.reduce((ac,a) =>
(ac[a] = ac[a] + 1 || 1, ac),
{})
console.log(charCount("my name is Khan23"))
using Object.keys(f).includes(str[i]) instead of str[i] in Object.keys(f) is the solution
in operator primarily would work to check if Object provides a particular attribute
Mozilla doc on in and Array.prototype.includes shall help.
following variation of your function would work
function charCout(str)
{
str = str.toLowerCase();
var f = {};
for(let i =0;i<str.length;i++)
{
if(str[i] === " ")
{
continue;
}
else{
if(Object.keys(f).includes(str[i]))
{
f[str[i]] += 1;
}
else
{
f[str[i]] = 1;
}
}
}
return(f);
}
You can take advantage of String.protoype.split and Array.prototype.reduce to solve this. Check the comments in the code for details:
const charCount = s => {
//Create an array of characters found in the string (filtering out spaces)
const chars = s.split('').filter(char => char.trim());
//Use reduce to create an occurrence map - increment by 1 each time a character is encountered
return chars.reduce((accum, char) => {
accum[char] = accum[char] ? accum[char] + 1 : 1;
return accum;
}, {});
};
console.log(charCount("my name is Khan23"))
Thank you all for your helpful suggestions. i finally figured out whats wrong with my code.
inputs and outputs of my defected code is as follows:
input: charCout("my name is Khan23")
expected output: {2: 1,3: 1,a: 2,e: 1,h: 1,i: 1,k: 1,m: 2,n: 2,s: 1,y: 1}
what i got: {2: NaN,3: NaN,a: 1,e: 1,h: 1,i: 1,k: 1,m: 1,n: 1,s: 1,y: 1};
I've used "in" operator to find out whether a specific value of string is present the Object.keys(f) array. That's exactly where I went wrong.
after referring to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in i got to know "in" can't be used to filter array using value at index.
You can use str.split("") to get the list of chars and then do something like
const splitted = str.split("");
const result = {};
splitted.map(letter => result[letter] ? result[letter]++ : result[letter] = 1)
it is just an idea
I am build an autocomplete that searches off of a CouchDB View.
I need to be able to take the final character of the input string, and replace the last character with the next letter of the english alphabet. (No need for i18n here)
For Example:
Input String = "b"
startkey = "b"
endkey = "c"
OR
Input String = "foo"
startkey = "foo"
endkey = "fop"
(in case you're wondering, I'm making sure to include the option inclusive_end=false so that this extra character doesn't taint my resultset)
The Question
Is there a function natively in Javascript that can just get the next letter of the alphabet?
Or will I just need to suck it up and do my own fancy function with a base string like "abc...xyz" and indexOf()?
my_string.substring(0, my_string.length - 1)
+ String.fromCharCode(my_string.charCodeAt(my_string.length - 1) + 1)
// This will return A for Z and a for z.
function nextLetter(s){
return s.replace(/([a-zA-Z])[^a-zA-Z]*$/, function(a){
var c= a.charCodeAt(0);
switch(c){
case 90: return 'A';
case 122: return 'a';
default: return String.fromCharCode(++c);
}
});
}
A more comprehensive solution, which gets the next letter according to how MS Excel numbers it's columns... A B C ... Y Z AA AB ... AZ BA ... ZZ AAA
This works with small letters, but you can easily extend it for caps too.
getNextKey = function(key) {
if (key === 'Z' || key === 'z') {
return String.fromCharCode(key.charCodeAt() - 25) + String.fromCharCode(key.charCodeAt() - 25); // AA or aa
} else {
var lastChar = key.slice(-1);
var sub = key.slice(0, -1);
if (lastChar === 'Z' || lastChar === 'z') {
// If a string of length > 1 ends in Z/z,
// increment the string (excluding the last Z/z) recursively,
// and append A/a (depending on casing) to it
return getNextKey(sub) + String.fromCharCode(lastChar.charCodeAt() - 25);
} else {
// (take till last char) append with (increment last char)
return sub + String.fromCharCode(lastChar.charCodeAt() + 1);
}
}
return key;
};
Here is a function that does the same thing (except for upper case only, but that's easy to change) but uses slice only once and is iterative rather than recursive. In a quick benchmark, it's about 4 times faster (which is only relevant if you make really heavy use of it!).
function nextString(str) {
if (! str)
return 'A' // return 'A' if str is empty or null
let tail = ''
let i = str.length -1
let char = str[i]
// find the index of the first character from the right that is not a 'Z'
while (char === 'Z' && i > 0) {
i--
char = str[i]
tail = 'A' + tail // tail contains a string of 'A'
}
if (char === 'Z') // the string was made only of 'Z'
return 'AA' + tail
// increment the character that was not a 'Z'
return str.slice(0, i) + String.fromCharCode(char.charCodeAt(0) + 1) + tail
}
Just to explain the main part of the code that Bipul Yadav wrote (can't comment yet due to lack of reps). Without considering the loop, and just taking the char "a" as an example:
"a".charCodeAt(0) = 97...hence "a".charCodeAt(0) + 1 = 98 and String.fromCharCode(98) = "b"...so the following function for any letter will return the next letter in the alphabet:
function nextLetterInAlphabet(letter) {
if (letter == "z") {
return "a";
} else if (letter == "Z") {
return "A";
} else {
return String.fromCharCode(letter.charCodeAt(0) + 1);
}
}
var input = "Hello";
var result = ""
for(var i=0;i<input.length;i++)
{
var curr = String.fromCharCode(input.charCodeAt(i)+1);
result = result +curr;
}
console.log(result);
I understand the original question was about moving the last letter of the string forward to the next letter. But I came to this question more interested personally in changing all the letters in the string, then being able to undo that. So I took the code written by Bipul Yadav and I added some more code. The below code takes a series of letters, increments each of them to the next letter maintaining case (and enables Zz to become Aa), then rolls them back to the previous letter (and allows Aa to go back to Zz).
var inputValue = "AaZzHello";
console.log( "starting value=[" + inputValue + "]" );
var resultFromIncrementing = ""
for( var i = 0; i < inputValue.length; i++ ) {
var curr = String.fromCharCode( inputValue.charCodeAt(i) + 1 );
if( curr == "[" ) curr = "A";
if( curr == "{" ) curr = "a";
resultFromIncrementing = resultFromIncrementing + curr;
}
console.log( "resultFromIncrementing=[" + resultFromIncrementing + "]" );
inputValue = resultFromIncrementing;
var resultFromDecrementing = "";
for( var i2 = 0; i2 < inputValue.length; i2++ ) {
var curr2 = String.fromCharCode( inputValue.charCodeAt(i2) - 1 );
if( curr2 == "#" ) curr2 = "Z";
if( curr2 == "`" ) curr2 = "z";
resultFromDecrementing = resultFromDecrementing + curr2;
}
console.log( "resultFromDecrementing=[" + resultFromDecrementing + "]" );
The output of this is:
starting value=[AaZzHello]
resultFromIncrementing=[BbAaIfmmp]
resultFromDecrementing=[AaZzHello]