codewar challenge javascript - javascript

I hope everyone is having a good day.
This is my first ever post on Stackoverflow!
I have just completed the javascript course on codeacademy and have read a couple of books on it too. Now I am on codewars. I would classify myself as a beginner in Javascript.
I find myslef a little stuck on a challenge, please can someone shed some light on what I may be doing wrong? Many thanks in advance!
Here is the instructions:
Check to see if a string has the same amount of 'x's and 'o's. The method must return a boolean and be case insensitive. The string can contains any char.
And here is my code:
function XO(str) {
var x = [];
var o = [];
for (var i = 0; i <= str.length; i++) {
if (str(i).toLowerCase === "x") {
x.push(i);
} else if (str(i).toLowerCase === "o") {
o.push(i);
}
if (x.length === o.length) {
return true;
} else {
return false;
}
}
}

i corrected mistakes and use code comment to explain
function XO(str) {
var x = [];
var o = [];
for (var i = 0; i < str.length; i++) { // i must be lower than length
// str[i] or str.charAt(i), not str(i)
if (str[i].toLowerCase() === 'x') { // toLowerCase is function, it must be called with toLowerCase()
x.push(str[i]); // should push character
} else if (str[i].toLowerCase() === 'o') {
o.push(str[i]);
}
}
// return statement must be located at the end
if (x.length == o.length) {
return true;
} else {
return false;
}
}
console.log(XO('xo'));
console.log(XO('xxo'));
console.log(XO('xoX'));
console.log(XO('xoOX'));

function XO(str) {
var x = 0, // numbers are better
o = 0;
for (var i = 0; i < str.length; i++) { // changed from '<=' to '<'
if (str[i].toLowerCase() === "x") {
x++;
} else if (str[i].toLowerCase() === "o") {
o++;
}
}
return x === o;
}

str.match(/x/g).length==str.match(/o/g).length

function checkIfOequalsX(str){
return str.match(/x/g).length==str.match(/o/g).length
}
console.log(checkIfOequalsX('xxooxo'));
console.log(checkIfOequalsX('xxooxoo'));
you can do it with
str.match(/x/g).length==str.match(/o/g).length

The third if else will never be executed because for a string there will be always a value.
If you want to return the count then the check of length should be performed after the for loop.
var xCount = 0; var oCount = 0;
for (var i = 0; i < str.length; i++) {
if (str[i].toLowerCase() === "x") {
xCount++;
} else if (str[i].toLowerCase() === "o") {
oCount++;
}
}
return xCount === oCount;
About another solutions containing the check based on str.match method, the complexity of using that piece of code is twice compared to above because the str.match loop is performed twice to match both the strings.

function XO(str) {
let strn = str.toLowerCase();
let countX = [];
let countO = [];
for(let i=0; i<strn.length; i++) {
if(strn[i] == 'x') {
countX.push(strn[i])
} else if(strn[i] == 'o') {
countO.push(strn[i])
}
}
if(countX.length == countO.length){
return true
} else if(countX.length !== countO.length) {
return false
}
}

You can use the Regex for finding those characters:
function XO(str) {
return str.match(/o/ig).length === str.match(/x/ig).length;
}

function XO(str) {
let letraO = 0
let letraX = 0
const myArray = str.toLowerCase();
for (i=0; i<myArray.length; i++){
myArray[i] === 'o'? letraO++ : letraX ++
}
return letraO===letraX? true : false
}

function XO(str) {
// make the string lowercase because we are case insensitive
str = str.toLowerCase();
// put the string into an array
var arrayOfCharacters = str.split("");
//count the x's
var countX = arrayOfCharacters.reduce( function( n, val ) {
return n + (val === 'x');
}, 0);
// count the o's
var countO = arrayOfCharacters.reduce( function( n, val ) {
return n + (val === 'o');
}, 0);
// do these numbers match? if so return true and if not return false
if ( countX == countO ) {
return true;
} else {
return false;
}
}

Related

Function returning undefined instead of boolean. BST, LeetCode

I am working on a problem in leetcode. the code is written in javascript.
https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/
Whenever I test the code it returns undefined, however when i put console.log(true) right before my return statement, it prints true, but still does not return true.
var findTarget = function (root, k) {
var stack = [];
var currentNode = root;
var arrayofVals = [];
var traverse = function (currentNode) {
console.log(currentNode);
console.log(stack);
arrayofVals.push(currentNode.val);
if (currentNode.right !== null) {
stack.push(currentNode.right);
}
if (currentNode.left !== null) {
currentNode = currentNode.left;
traverse(currentNode);
}
if (stack.length > 0 ) {
currentNode = stack.pop();
traverse(currentNode);
} else {
console.log(arrayofVals)
for (var i = 0; i <= arrayofVals.length; i++) {
for (var j = 0; j <= arrayofVals.length; j++) {
if (i === j) {
continue;
}
if(arrayofVals[i] + arrayofVals[j] === k) {
console.log(1 === 1);
return (1 === 1);
}
}
}
return false;
}
}
traverse(currentNode);
}
Can someone help me understand why my code returns undefined? I have had this problem before, but only when returning bool.
Thanks for your help!
Right now, findTarget isn't returning anything. You'll want to do return traverse(currentNode);.

Comparing 2 strings in array to match J.S

"Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array."
For some reason I can pass every other checkpoint except (["hello", "hey"]). any tips?
function mutation(arr) {
var firstIndex = arr[0].toLowerCase();
var secondIndex = arr[1].toLowerCase();
for(var i = 0; i < arr.length; i++) {
if(firstIndex.indexOf(secondIndex.charAt(i)) !== -1) {
return true;
}
else {
return false;
}
}
return arr;
}
mutation(["hello", "hey"]);
The idea is to check every character in secondIndex to see if it is contained in firstIndex. So the logic here would be: for ever character in secondIndex, if the character is not in firstIndex, return false (ending the function). If the function doesn't end after checking all the characters, you know that each character was found in firstIndex, and you can then return true.
function mutation(arr) {
var firstIndex = arr[0].toLowerCase();
var secondIndex = arr[1].toLowerCase();
for(var i = 0; i < secondIndex.length; i++) {
if(firstIndex.indexOf(secondIndex[i]) === -1) {
return false;
}
}
return true;
}
mutation(["hello", "hey"])

Check for repeated characters in a string Javascript

I was wondering if there is a way to check for repeated characters in a string without using double loop. Can this be done with recursion?
An example of the code using double loop (return true or false based on if there are repeated characters in a string):
var charRepeats = function(str) {
for(var i = 0; i <= str.length; i++) {
for(var j = i+1; j <= str.length; j++) {
if(str[j] == str[i]) {
return false;
}
}
}
return true;
}
Many thanks in advance!
This will do:
function hasRepeats (str) {
return /(.).*\1/.test(str);
}
(A recursive solution can be found at the end of this answer)
You could simply use the builtin javascript Array functions some MDN some reference
var text = "test".split("");
text.some(function(v,i,a){
return a.lastIndexOf(v)!=i;
});
callback parameters:
v ... current value of the iteration
i ... current index of the iteration
a ... array being iterated
.split("") create an array from a string
.some(function(v,i,a){ ... }) goes through an array until the function returns true, and ends than right away. (it doesn't loop through the whole array, which is good for performance)
Details to the some function here in the documentation
Here some tests, with several different strings:
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var text = texts[idx].split("");
document.write(text + " -> " + text.some(function(v,i,a){return a.lastIndexOf(v)!=i;}) +"<br/>");
}
//tested on win7 in chrome 46+
If you will want recursion.
Update for recursion:
//recursive function
function checkString(text,index){
if((text.length - index)==0 ){ //stop condition
return false;
}else{
return checkString(text,index + 1)
|| text.substr(0, index).indexOf(text[index])!=-1;
}
}
// example Data to test
var texts = ["test", "rest", "why", "puss"];
for(var idx in texts){
var txt = texts[idx];
document.write( txt + " ->" + checkString(txt,0) + "<br/>");
}
//tested on win7 in chrome 46+
you can use .indexOf() and .lastIndexOf() to determine if an index is repeated. Meaning, if the first occurrence of the character is also the last occurrence, then you know it doesn't repeat. If not true, then it does repeat.
var example = 'hello';
var charRepeats = function(str) {
for (var i=0; i<str.length; i++) {
if ( str.indexOf(str[i]) !== str.lastIndexOf(str[i]) ) {
return false; // repeats
}
}
return true;
}
console.log( charRepeats(example) ); // 'false', because when it hits 'l', the indexOf and lastIndexOf are not the same.
function chkRepeat(word) {
var wordLower = word.toLowerCase();
var wordSet = new Set(wordLower);
var lenWord = wordLower.length;
var lenWordSet =wordSet.size;
if (lenWord === lenWordSet) {
return "false"
} else {
return'true'
}
}
Using regex to solve=>
function isIsogram(str){
return !/(\w).*\1/i.test(str);
}
console.log(isIsogram("isogram"), true );
console.log(isIsogram("aba"), false, "same chars may not be adjacent" );
console.log(isIsogram("moOse"), false, "same chars may not be same case" );
console.log(isIsogram("isIsogram"), false );
console.log(isIsogram(""), true, "an empty string is a valid isogram" );
The algorithm presented has a complexity of (1 + n - (1)) + (1 + n - (2)) + (1 + n - (3)) + ... + (1 + n - (n-1)) = (n-1)*(1 + n) - (n)(n-1)/2 = (n^2 + n - 2)/2 which is O(n2).
So it would be better to use an object to map and remember the characters to check for uniqueness or duplicates. Assuming a maximum data size for each character, this process will be an O(n) algorithm.
function charUnique(s) {
var r = {}, i, x;
for (i=0; i<s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
On a tiny test case, the function indeed runs a few times faster.
Note that JavaScript strings are defined as sequences of 16-bit unsigned integer values. http://bclary.com/2004/11/07/#a-4.3.16
Hence, we can still implement the same basic algorithm but using a much quicker array lookup rather than an object lookup. The result is approximately 100 times faster now.
var charRepeats = function(str) {
for (var i = 0; i <= str.length; i++) {
for (var j = i + 1; j <= str.length; j++) {
if (str[j] == str[i]) {
return false;
}
}
}
return true;
}
function charUnique(s) {
var r = {},
i, x;
for (i = 0; i < s.length; i++) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charUnique2(s) {
var r = {},
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s[i];
if (r[x])
return false;
r[x] = true;
}
return true;
}
function charCodeUnique(s) {
var r = [],
i, x;
for (i = s.length - 1; i > -1; i--) {
x = s.charCodeAt(i);
if (r[x])
return false;
r[x] = true;
}
return true;
}
function regExpWay(s) {
return /(.).*\1/.test(s);
}
function timer(f) {
var i;
var t0;
var string = [];
for (i = 32; i < 127; i++)
string[string.length] = String.fromCharCode(i);
string = string.join('');
t0 = new Date();
for (i = 0; i < 10000; i++)
f(string);
return (new Date()) - t0;
}
document.write('O(n^2) = ',
timer(charRepeats), ';<br>O(n) = ',
timer(charUnique), ';<br>optimized O(n) = ',
timer(charUnique2), ';<br>more optimized O(n) = ',
timer(charCodeUnique), ';<br>regular expression way = ',
timer(regExpWay));
let myString = "Haammmzzzaaa";
myString = myString
.split("")
.filter((item, index, array) => array.indexOf(item) === index)
.join("");
console.log(myString); // "Hamza"
Another way of doing it using lodash
var _ = require("lodash");
var inputString = "HelLoo world!"
var checkRepeatition = function(inputString) {
let unique = _.uniq(inputString).join('');
if(inputString.length !== unique.length) {
return true; //duplicate characters present!
}
return false;
};
console.log(checkRepeatition(inputString.toLowerCase()));
const str = "afewreociwddwjej";
const repeatedChar=(str)=>{
const result = [];
const strArr = str.toLowerCase().split("").sort().join("").match(/(.)\1+/g);
if (strArr != null) {
strArr.forEach((elem) => {
result.push(elem[0]);
});
}
return result;
}
console.log(...repeatedChar(str));
You can also use the following code to find the repeated character in a string
//Finds character which are repeating in a string
var sample = "success";
function repeatFinder(str) {
let repeat="";
for (let i = 0; i < str.length; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str.charAt(i) == str.charAt(j) && repeat.indexOf(str.charAt(j)) == -1) {
repeat += str.charAt(i);
}
}
}
return repeat;
}
console.log(repeatFinder(sample)); //output: sc
const checkRepeats = (str: string) => {
const arr = str.split('')
const obj: any = {}
for (let i = 0; i < arr.length; i++) {
if (obj[arr[i]]) {
return true
}
obj[arr[i]] = true
}
return false
}
console.log(checkRepeats('abcdea'))
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = '
function repeat(str){
let h =new Set()
for(let i=0;i<str.length-1;i++){
let a=str[i]
if(h.has(a)){
console.log(a)
}else{
h.add(a)
}
}
return 0
}
let str = 'haiiiiiiiiii'
console.log(repeat(str))
'
console.log(repeat(str))
Cleanest way for me:
Convert the string to an array
Make a set from the array
Compare the length of the set and the array
Example function:
function checkDuplicates(str) {
const strArray = str.split('');
if (strArray.length !== new Set(strArray).size) {
return true;
}
return false;
}
You can use "Set object"!
The Set object lets you store unique values of any type, whether
primitive values or object references. It has some methods to add or to check if a property exist in the object.
Read more about Sets at MDN
Here how i use it:
function isIsogram(str){
let obj = new Set();
for(let i = 0; i < str.length; i++){
if(obj.has(str[i])){
return false
}else{
obj.add(str[i])
}
}
return true
}
isIsogram("Dermatoglyphics") // true
isIsogram("aba")// false

Find missing letter in list of alphabets

I am trying to solve the following issue:
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
the inputs that I will get as strings are:
abce (Which should return d)
bcd (which should return undefined)
abcdefghjklmno (which should return i)
yz (which should return undefined)
my code currently looks like this:
function fearNotLetter(str) {
//create alphabet string
//find starting letter in alphabet str, using str
//compare letters sequentially
//if the sequence doesn't match at one point then return letter
//if all letters in str appear then return undefined
var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
var i = 0;
var j = 0;
while (i<alphabet.length && j<str.length) {
i++;
if (alphabet.charCodeAt(i) === str.charCodeAt(j)) {
i++;
j++;
}
else if (alphabet.charCodeAt(i) !== str.charCodeAt(j)) {
i++;
j++;
if (alphabet.charCodeAt(i) === str.charCodeAt(j-1)) {
return alphabet.charCodeAt(i-1);
}
}
}
}
fearNotLetter('abce');
Thanks for your help as always!
I would do it like this:
function fearNotLetter(str) {
var i, j = 0, m = 122;
if (str) {
i = str.charCodeAt(0);
while (i <= m && j < str.length) {
if (String.fromCharCode(i) !== str.charAt(j)) {
return String.fromCharCode(i);
}
i++; j++;
}
}
return undefined;
}
console.log(fearNotLetter('abce')); // "d"
console.log(fearNotLetter('bcd')); // undefined
console.log(fearNotLetter('bcdefh')); // "g"
console.log(fearNotLetter('')); // undefined
console.log(fearNotLetter('abcde')); // undefined
console.log(fearNotLetter('abcdefghjkl')); // "i"
i can go from 97 to 122, this interval corresponds to the ASCII codes of the lower case alphabet.
If you want it not to be case sensitive, just do str = str.toLowerCase() at the beginning of the function.
I think this is the simplest code to do this:
function skippedLetter(str) {
for (var i = 0; i < str.length - 1; i++) {
if (str.charCodeAt(i + 1) - str.charCodeAt(i) != 1) {
return String.fromCharCode(str.charCodeAt(i) + 1);
}
}
}
alert(skippedLetter('abce'));
This version will reject illegal input, accept both upper and lower case, check that there is only 1 hole in the range, and that there is exactly 1 character missing.
function skippedLetter(str) {
if (!str.match(/^[a-zA-Z]+$/)) return;
var letter = "", offset = str.charCodeAt(0);
for (var i = 1; i < str.length; i++) {
var diff = str.charCodeAt(i) - i - offset;
if (diff == 1) letter += String.fromCharCode(i + offset++)
else if (diff) return;
}
if (letter.length == 1) return letter;
}
alert(skippedLetter('123567')); // illegal characters
alert(skippedLetter('')); // empty string
alert(skippedLetter('a')); // too short
alert(skippedLetter('bc')); // nothing missing
alert(skippedLetter('df')); // skipped letter = e
alert(skippedLetter('GHIKLM')); // skipped letter = J
alert(skippedLetter('nOpRsT')); // cases mixed
alert(skippedLetter('nopxyz')); // too many characters missing
alert(skippedLetter('abcefgijk')); // character missing more than once
alert(skippedLetter('abcefgfe')); // out of order
Note that you have a typo in alphabet: There are two "e"s.
You could split the string into an array, then use the some method to short-circuit the loop when you don't find a match:
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
missing,
i= 0;
str.split('').some(function(l1) {
var l2= alphabet.substr(i++, 1);
if(l1 !== l2) {
if(i===1) missing= undefined;
else missing= l2;
return true;
}
});
return missing;
}
console.log(fearNotLetter('abce')); //d
console.log(fearNotLetter('bcd')); //undefined
console.log(fearNotLetter('abcdefghjklmno')); //i
console.log(fearNotLetter('yz')); //undefined
Another function that may help:
var alphabet = "abcdefgheijklmnopqrstuvwxyz";
function fearNotLetter(a) {
function letterIndex(text, index) {
var letter = text.charAt(0);
if (alphabet.indexOf(letter) !== index) { return alphabet.charAt(index); } else { return letterIndex(text.substring(1), index + 1) }
}
if (alphabet.indexOf(a) === -1) {
return letterIndex(a, alphabet.indexOf(a.charAt(0)));
}
return undefined;
}
fearNotLetter("abc"); //Undefined
fearNotLetter("abce"); //d
fearNotLetter("fgi"); //h
This will do what you're looking for:
Hit run and check your console
function missingLetter (str) {
var alphabet = ("abcdefghijklmnopqrstuvwxyz");
var first = alphabet.indexOf(str[0]);
var strIndex = 0;
var missing;
for (var i = first ; i < str.length ; i++) {
if (str[strIndex] === alphabet[i]) {
strIndex++;
} else {
missing = alphabet[i];
}
}
return missing;
}
console.log(missingLetter("abce"));
console.log(missingLetter("bcd"));
console.log(missingLetter("abcdefghjklmno"));
console.log(missingLetter("yz"));
I think that you wanted to say that if a string doesn't start with "a" than return "undefined". So here's my code:
function fearNotLetter(str) {
var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
var i = 0;
var j = 0;
while (i < alphabet.length && j < str.length) {
if (alphabet.charAt(i) != str.charAt(j)) {
i++;
j++;
if (alphabet.charAt(i - 1) == "a") {
return "undefined";
} else {
return (alphabet.charAt(i - 1));
}
}
i++;
j++;
}
}
alert(fearNotLetter('abce'));
Here's the working JsFiddle.
You wanted the code to return the missing letter so I used CharAt.
You can make an array of letters and then search through it to see if it maches with letters from the string....
function fearNotLetter(str) {
var a = str.split('');
var array = [];
var j = 0;
for (var i = 1; i < a.length; i++) {
var d = a[i].charCodeAt(0);
var c = a[i - 1].charCodeAt(0);
var delta = d - c;
if (delta != 1) {
array[i] = String.fromCharCode(a[i - 1].charCodeAt(0) + 1);
}
}
str = array.join('');
if (str.length === 0) {
return undefined;
} else {
return str;
}
}
fearNotLetter('abcefr');
This is an even shorter answer thanks to RegExp() that allows you to create a Regular Expression on the fly and use match() to strip off the given letters from a generated String that has all the letters in the given range:
function fearNotLetter(str) {
var allChars = '';
var notChars = new RegExp('[^'+str+']','g');
for (var i=0;allChars[allChars.length-1] !== str[str.length-1] ;i++)
allChars += String.fromCharCode(str[0].charCodeAt(0)+i);
return allChars.match(notChars) ? allChars.match(notChars).join('') : undefined;
}
How about this one? it finds all missing letters anywhere between the first and the last given letters:
function fearNotLetter(str) {
var strArr = str.split('');
var missingChars = [], i = 0;
var nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
while (i<strArr.length - 1) {
if (nextChar !== strArr[i+1]){
missingChars.push(nextChar);
nextChar = String.fromCharCode(nextChar.charCodeAt(0)+1);
} else {
i++;
nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
}
}
return missingChars.join('') === '' ? undefined : missingChars.join('') ;
}
console.log(fearNotLetter("ab"));
Here is what I use:
function fearNotLetter(str) {
var firstLtrUnicode = str.charCodeAt(0),
lastLtrUnicode = str.charCodeAt(str.length - 1);
var holder = [];
for (var i=firstLtrUnicode; i<=lastLtrUnicode; i++) {
holder.push(String.fromCharCode(i));
}
var finalStr = holder.join('');
if ( finalStr === str ) { return undefined; }
else { return holder.filter( function(letter) {
return str.split('').indexOf(letter) === -1;
}).join(''); } }
Try this:
function fearNotLetter(str) {
var alp = ('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ').split(''), i;
for (i = alp.indexOf(str.charAt(0)); i < str.length; i++) {
if (str.split('').indexOf(alp[i]) === -1) {
return alp[i];
}
}
return undefined;
}
fearNotLetter('bcd');
Here's my solution, which i find to be quite easy to interpret:
function fearNotLetter(str) {
var missingLetter;
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) - str.charCodeAt(i-1) > 1) {
missingLetter = String.fromCharCode(str.charCodeAt(i) - 1);
}
}
return missingLetter;
}
I just did this challenge. I am a beginner to Javascript so this was my approach, very simple, sometimes you don't have to use the methods they provide but they also help. I hope you can understand it.
function fearNotLetter(str) {
var alphabet= "abcdefghijlmnopqrstuvwxyz";
var piece =alphabet.slice(0, str.length+1);
for(var i=0; i < piece.length; i++ ){
if(str.charCodeAt(0) != 97){
return undefined;
}
else if(str.indexOf(piece[i])===-1){
return piece[i];
}
}// for loop
}
fearNotLetter("abce");// It will return d
fearNotLetter("xy");//It will return undefined
fearNotLetter("bce");//It will return undefined
function fearNotLetter(str) {
//transform the string to an array of characters
str = str.split('');
//generate an array of letters from a to z
var lettersArr = genCharArray('a', 'z');
//transform the array of letters to string
lettersArr = lettersArr.join('');
//substr the a to z string starting from the first letter of the provided
string
lettersArr = lettersArr.substr(lettersArr.indexOf(str[0]), str.length);
//transform it again to an array of letters
lettersArr = lettersArr.split('');
//compare the provided str to the array of letters
for(var i=0; i<lettersArr.length;i++){
if(str[i] !== lettersArr[i])
return lettersArr[i];
}
return undefined;
}
function genCharArray(charA, charZ) {
var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0);
for (; i <= j; ++i) {
a.push(String.fromCharCode(i));
}
return a;
}
fearNotLetter("bcd");
Here's my another answer in missing letters:
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var first = alphabet.indexOf(str[0]);
var last = alphabet.indexOf(str[str.length-1]);
var alpha = alphabet.slice(first,last);
for(var i=0;i<str.length;i++){
if(str[i] !== alpha[i]){
return alpha[i];
}
}
}
console.log(fearNotLetter("abce"));
Overview:
const fearNotLetter = str => {
if(str.length < 26) {
return String.fromCharCode(
str.split("")
.map(x=> x.charCodeAt(0))
.sort((a,b) => a-b)
.find((x,i,a) => a[i+1]-x > 1) + 1
);
}
}
1 liner:
const fearNotLetter = str => str.length < 26 ? String.fromCharCode(str.split("").map(x=> x.charCodeAt(0)).sort((a,b) => a-b).find((x,i,a) => a[i+1]-x > 1) + 1) : undefined;
function fearNotLetter(str) {
var allChars = "";
var notChars = new RegExp("[^" + str + "]","g");
for (var i = 0; allChars[allChars.length - 1] !== str[str.length - 1]; i++)
allChars += String.fromCharCode(str[0].charCodeAt(0) + i);
return allChars.match(notChars) ? allChars.match(notChars).join("") : undefined;
}
function fearNotLetter(str) {
let alphabet = "abcdefghijklmnopqrstuvwxyz"
let startingPoint = alphabet.indexOf(str[0]);
let leaterNotMatch = alphabet.slice(startingPoint)
console.log(startingPoint)
for(let i = 0; i<str.length; i +=1){
if(str[i] != leaterNotMatch[i]){
return leaterNotMatch[i];
}
}
}
let result = fearNotLetter("abcdefghjklmno");
console.log(result)
function findMissingLetter(letters) {
const alphabet = ['A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
let otherAlphabet = [];
if (letters[0] !== letters[0].toUpperCase()) {
otherAlphabet = alphabet.map(item => item.toLowerCase());
} else {
otherAlphabet = alphabet;
}
const firstIndex = otherAlphabet.findIndex((aplhabetEl) => {
return aplhabetEl === letters[0];
});
const lastIndex = otherAlphabet.findIndex((aplhabetEl) => {
return aplhabetEl === letters[letters.length - 1];
});
const sliceAlphabet = otherAlphabet.slice(firstIndex, lastIndex + 1);
let result = '';
for (let i = 0; i < letters.length; i++) {
if (letters[i] !== sliceAlphabet[i]) {
result += sliceAlphabet[I];
}
}
return result.slice(0, 1);
}
You can also solve this question by using a map which we will call seen. For every letter, we will add it to our seen map. Then we will loop through the alphabet checking if we have come across that letter in our seen map.
If we have come across that letter in our seen map, then we will use a boolean flag foundFirstLetter and set that to true. Then inside another conditional foundFirstLetter, we can check if the letter is missing and if it is we can just return the letter because we know it's missing.
This solution works, but it isn't ideal because it uses more space (because of our seen map), unlike the other solutions.
const findMissingLetter = (array) => {
const convertUpperCase = array[0] === array[0].toUpperCase() ? true : false;
const alphabet = "abcdefghijklmnopqrstuvwxyz";
const seen = {};
let foundFirstLetter = false;
// add the words we have run across to our seen map
for(const letter of array){
seen[letter.toLowerCase()] = true;
}
// loop through our alphabet checking if letter is in our map. If we find the first letter, then that means
// if we don't find the next letter in our map then it's missing.
for(const letter of alphabet){
if(seen.hasOwnProperty(letter)){
foundFirstLetter = true;
}
if(foundFirstLetter){
if(!seen.hasOwnProperty(letter)){
if(convertUpperCase){
return letter.toUpperCase();
} else {
return letter;
}
}
}
}
}
function fearNotLetter(str) {
var string = array. join("");
for (var i = 0; i < string. length; i++) {
if (string. charCodeAt(i + 1) - string. charCodeAt(i) != 1) {
return String. fromCharCode(string. charCodeAt(i) + 1);
}
}
return undefined
}

Get multiple character positions in array?

I want to get position of dots and commas in an array.
w.wer,ads,
should give something like:
[0] > 1
[1] > 5
[2] > 9
How can this be done with javascript?
function getPositions(regex, str) {
var counts = [], m;
while (m = regex.exec(str)) {
counts.push(regex.lastIndex - m[0].length);
}
return counts;
}
// Usage:
getPositions(/[,.]/g, 'w.wer,ads,'); // => [1,5,9]
Try the following
var getAllIndexesOf = function(str, toFind) {
var all = [];
for (var i = 0; i < str.length; i++) {
if (toFind.indexOf(str[i]) >= 0) {
all.push(i);
}
}
return all;
}
var result = getAllIndexsOf("w.wer,ads,", [".", ","]);
Option 1
Simply loop through each character in the string:
var myString = "w.wer,ads,";
var matchedIndexes = [];
for (var i = 0; i < myString.length; i++) {
if (myString[i] == "," || myString[i] == ".") {
matchedIndexes.push(i);
}
}
Here's a working fiddle.
Option 2
If you wanted to get a bit fancier with it, you could create an object that stores the position and the character at that position:
var myString = "w.wer,ads,";
var matchedIndexes = [];
for (var i = 0; i < myString.length; i++) {
if (myString[i] == "," || myString[i] == ".") {
matchedIndexes.push(new myMatch(myString[i], i));
}
}
function myMatch(position, character) {
this.position = position;
this.character = character;
}
Result:
​
Here's a working fiddle.
function dotsAndCommas(s) {
var rv = [];
for (var i = 0; i < s.length; ++i) {
if (s[i] === '.' || s[i] === ',') rv.push(i);
}
return rv;
}
Basically, just do it. There's no shortcut way I can think of to do it with a regex.
Probably not ideal, but as a thought experiment, here is a way to do it with built-in javascript functions (no explicit loops):
var pos = mystr.split(/[,.]/).slice(0,-1).map(function(val, i, a) { return a.slice(0, i+1).join('').length+i; });
Demo: http://jsfiddle.net/jtbowden/hHSB2/
To get rid of the nested .slice() which is O(n^2):
var pos = mystr.split(/[,.]/).slice(0,-1).map(function(val, i, a) { return a[i] = val.length+(a[i-1]||-1)+1; });
Demo: http://jsfiddle.net/jtbowden/hHSB2/1/
And, yes, it is still ugly and pointless.
Yet another almost one-liner:
var q = [];
[].forEach.call('w.wer,ads,', function(v, i) { /[,.]/.test(v) ? q.push(i) : 0 } );
// q == [1,5,9]
I would loop through the string and check each character like so:
var s = "w.wer,ads,";
var arr = new Array();
for( var i = 0; i < s.length; i++ ) {
if( s.charAt(i) == "." || s.charAt(i) == "," )
arr.push(i);
}
for( var i = 0; i < arr.length; i++ ) {
document.write( arr[i] );
}
Since there're 5 solutions already, I'll write my almost one-liner.
function dotsAndCommasPositions(str) {
var i = -1;
return str.split(/[.,]/).slice(0, -1).map(function(s){ return i += s.length + 1; });
};
You can create a function that loops through all the characters and saves the index of . and ,.
​function getPositionsOfDotsCommas(str) {
var result = [];
for (var i = 0; i < str.length; i++) {
if (str[i] === '.' || str[i] === ',') {
result.push(i);
}
}
return result;
}​

Categories

Resources