I am trying to create a word reverser function that will reverse the letter order of each word in a string, without using any loops.
So far I have this:
var strFlip = function(str){
splitStr = str.split(" ");{
return;
}
var wordFlip = function reverse(splitStr){
return splitStr.split('').reverse().join('');
};
};
console.log(strFlip("Hello Hannah"));
The wordFlip part correctly reverses the order of a single word if used independent of the rest of the code. However, the overall code continually outputs undefined. I am unfamiliar with Javascript, so I assume I am simply not noticing a simple structural issue.
Well, this part causes the function to return immediately, and since it is returning nothing, you get undefined:
var strFlip = function(str){
splitStr = str.split(" ");{
return; //<-- immediately returns here
}
...
};
I'm not sure what that part is trying to do, but to actually do what you want, you can do this:
var strFlip = function(str) {
//Since you are using a variable as a reference to a function
//expression/literal, there is no hoisting so you have to
//predefine
var wordFlip = function reverse(splitStr) {
return splitStr.split('').reverse().join('');
};
return str.split(" ").map(wordFlip).join(" ");
}
A more elegant approach would be to forgo wordFlip and just use an anonymous callback to map:
var strFlip = function(str) {
return str.split(" ").map(function(word) {
return word.split("").reverse().join("");
}).join(" ");
}
To understand what map does, let's look at forEach first since map works in a similar manner. When you use array.forEach(callback), the function callback is called for every element in array. It is equivalent to the following code:
var array = [1, 2, 3];
for(var i = 0; i < array.length; i++) {
callback(array[i]);
}
Essentially it is a "functional" way of iterating over an array and doing something with each element. map also does this; it iterates over the array as well, but converts (maps) each element of the array to something else and returns a new array consisting of the mapped elements. The something else is defined by the behavior of callback. Here's an example that is similar to the example above. Here I'm specifying an actual callback as well so that you can see the behavior a little better:
var array = [1, 2, 3];
var squares = [];
function square(num) {
return num * num;
}
for(var i = 0; i < array.length; i++) {
squares[i] = square(array[i]);
}
Here, square is the same as callback and it is called for each element. The return value of square is then used to construct the new array. Using map, you can rewrite the above as:
var squares = array.map(function(num) {
return num * num;
});
Now applying this to the solution, you can see that we are mapping each word in the sentence to its reverse and then joining on the mapped array.
You're overcomplicating things by trying to nest a function inside a function. Create a single function that does both the work of reversing the words and characters:
var flipper = function reverse(splitStr) {
return splitStr.split('').reverse().join('').split(' ').reverse().join(' ');
};
alert(flipper("Hello Hannah"));
You are pretty close, but the code is a little overbusy. Try this:
function reverse(splitStr) {
return splitStr.split('').reverse().join('');
}
console.log(reverse('Hello Hannah'));
I just took the main workhorse out of your code and simplified the function. You were on the right track,
EDIT
If you want to keep the words in place, like Vivin suggested, you can do something like:
function reverse(splitStr) {
var strs = splitStr.split(' ');
var reverseInPlace = [];
for (var i = 0; i < strs.length; i++) {
reverseInPlace.push(strs[i].split('').reverse().join(''));
}
return reverseInPlace.join(' ');
}
console.log(reverse('Hello Hannah'));
One approach would be split the sentence by empty spaces and then reverse and join empty spaces split again by a space then reverse and join with an empty space.
If you do that then:
"Hello Hannah" will be transformed to "olleH hannaH"
Edit: You can define the prototype to string as your own extension for one line solution.
String.prototype.Flip = function() {
return this.split("").reverse().join("").split(" ").reverse().join(" ");
};
var a="Hello Hannah";
alert(a.Flip());
Related
Hello there StackOverflow people,
What I expected:
Removing the numbers of the string "23Ka5X". The loop counts the length and the if statement extracts the letters into an array letterMemory. When no letters are in the string, the message '"oh no numbers!" should be the output.
What I ran into:
I have been working on this for some time now but I can't find my mistake. I don't know if I missed a simple detail or made a big mess.
My feeling and console output:
var letterMemory = [];
function orderMsg(mixedMsg) {
for (var loopString = 0; loopString < mixedMsg.length; loopString++); {
if (isNaN(parseInt(mixedMsg.charAt[loopString]))); {
letterMemory.push(mixedMsg.charAt[loopString]);
return letterMemory;
} if (!isNaN(parseInt(mixedMsg.charAt[loopString]))) {
return "oh no numbers!";
}
}
}
console.log(orderMsg("23Ka5X"));
I feel like the issue is trying to push any letter into the array letterMemory via letterMemory.push(mixedMsg.charAt[loopString])
does not work how I imagine it.
I would be really grateful for your help!
I found a simple solution via .replace() but I really want to make it work with a loop and if statements since loops combined with if statements were my latest freecodecamp lessons and I want to get better at it.
The fixed code
function orderMsg(mixedMsg){
var letterMemory = []
for (var loopString = 0; loopString < mixedMsg.length; loopString++){
if (isNaN(mixedMsg[loopString])){
letterMemory.push(mixedMsg[loopString])
}
}
if (letterMemory.length){
return letterMemory
} else {
return 'oh no numbers!'
}
}
The issue was
The for loop was not executing since you terminated it with ; at the end.
charAt is a function, so you either do string.charAt(index), or you can also simply say string[index].
You are using the return statement within the for loop, so what will happen is even if the for loop ran (without the semi-colon at the end), it would run just once.
One other issue is that the variable letterMemory is declared outside the function so that means if you were to call this function twice, it would use the same letterMemory array.
-end of answer-
Additional read: you can use split, filter and ternary operator to condense the function as follows ..
function orderMsg(mixedMsg){
const letterMemory = mixedMsg.split('').filter(isNaN)
return letterMemory.length ? letterMemory : 'oh no numbers!'
}
This could be helpful,
function orderMsg(mixedMsg) {
for (var loopString = 0; loopString < mixedMsg.length; loopString++) {
if (isNaN(parseInt(mixedMsg.charAt(loopString)))) {
letterMemory.push(mixedMsg.charAt(loopString));
}
}
return letterMemory;
}
var arr = orderMsg("23s5");
if (arr.length == 0) {
console.log("oh no numbers!")
} else {
console.log(arr);
}
Use replace with regex globally, replacing all digits by an empty string:
string.replace(/[0-9]/g, "")
You have terminated for loop in the same line with ;.
charAt() is a method.
Return value after for loop ends.
var letterMemory = [];
function orderMsg(mixedMsg) {
for (var loopString = 0; loopString < mixedMsg.length; loopString++) {
var letter=parseInt(mixedMsg.charAt(loopString));
if(isNaN(letter)){
letterMemory.push(mixedMsg.charAt(loopString));
}
}
if(letterMemory.length>0){
return letterMemory;
}
else{
return "Oh no numbers!";
}
}
console.log(orderMsg("23Ka5X"));
Maybe try using .test to match the letters.
function orderMsg(str){
var result = [];
for(var letter of str){
if(/[a-zA-Z]+/g.test(letter)){
result.push(letter)
}
}
if(result.length === 0){
return 'Oh no numbers'
}
return result
}
For a more thorough explanation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
Not sure why this is happening. At this point in my code these array may have a single element.
actualAnswer = actualAnswer.split(" ");
playerAnswer = playerAnswer.split(" ");
I then pass them through this function.
function checkForPlural(playerAnswer, actualAnswer){
var answerObject = {playerAnswer: playerAnswer,
actualAnswer: actualAnswer};
for (var answerWord in actualAnswer){
if (actualAnswer[answerWord].slice(-1) == "S")
{
answerObject.actualAnswer[answerWord] = actualAnswer[answerWord].substring(0, actualAnswer[answerWord].length - 1);
}
}
for (var answerWord in playerAnswer){
if (playerAnswer[answerWord].slice(-1) == "S")
{
answerObject.playerAnswer[answerWord] = playerAnswer[answerWord].substring(0, playerAnswer[answerWord].length - 1);
}
}
}
When I return the object IF the array's passed in only had a single element javascript decides to interpret them as a string so if I were to use answerObject.actualAnswer.length it'll give me the length of the string, rather than just 1.
Here is the plunker, unfortunately/fortunately it seems to work as intended on there so i'm confused:
https://plnkr.co/edit/nIqq3VdjjMa2GDWyqkxx
EDIT: The problem was at an earlier junction in my code, sorry!
You can use Array.prototype.map() to return a new array, String.prototype.replace() to, if exists, replace the ending "S" character in a string, return an object having properties set to the function parameter identifiers
const regexp = /S$/;
const re = "";
function parseAnswer(arr, regexp_, re_) {
return arr.map(function(prop) {return prop.replace(regexp_, re_)})
}
function checkForPlural(playerAnswer, actualAnswer) {
return {
playerAnswer: parseAnswer(playerAnswer, regexp, re),
actualAnswer: parseAnswer(actualAnswer, regexp, re)
}
}
console.log(checkForPlural("abc defS ghi".split(" "), "12S 456 78S".split(" ")))
The code in question is working correctly, the "magic" conversion was because of an error in another function that was overwriting the array with a string. My bad.
Instructions are as follows:
"Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example:
anagrams('abba', ['aabb', 'abcd', 'bbaa', 'dada']) => ['aabb', 'bbaa']"
I snagged a snippet that takes the first parameter and delivers every combination of characters possible. My trouble now is figuring out how to match this array against the second parameter, and returning some results..
function allAnagrams (word,words) {
if (word.length < 2) {
return [word];
} else {
var allAnswers = [];
for (var i = 0; i < word.length; i++) {
var letter = word[i];
var shorterWord = word.substr(0, i) + word.substr(i + 1, word.length - 1);
var shortwordArray = allAnagrams(shorterWord);
for (var j = 0; j < shortwordArray.length; j++) {
allAnswers.push(letter + shortwordArray[j]);
}
}
return allAnswers;
}
}
allAnagrams("abc",["acb","cba","bac","bca"]);
My instinct was to split the word into an array, and then have another nested for loop to match what needs to be matched. However, I seem to have run into some problems with handling the scope and keep breaking the function so I've turned to you bright minded people. If you have a moment, I would appreciate a hint of how to tackle this from here.
You basically need to see if any permutation of your first string appears in the array.
A fast way to compare if two strings are permutations of one another is to sort the two strings and compare:
function stringSort(string) {
return string.split('').sort().join('');
}
function isAnagram(first, second) {
// are the two sorted strings equal, if so then anagram
return stringSort(first) == stringSort(second);
}
Now, we can use these handy functions to help us build the final desired function:
function allAnagrams(word, words) {
return words.filter(function(element) {
return isAnagram(word, element);
});
}
Note the use of Array#filter, a very handy method for boiling an array down to a few values under some condition.
Note that I have not tested this so if there are any problems please ask.
i wanna generate a 3x3 field. I want to do this with JS, it shall be a web application.
All fields shall inital with false. But it seems so that my code is not working correctly, but i don't find my fault. The goal is, that every spacesector is accessible.
Thats my idea:
// define size
var esize = generateSpace(3);
}
space[i] = false is replacing the array with a single boolean value false, not filling in all the entries in array you just created. You need another loop to initialize all the elements of the array.
function generateSpace(x) {
var space = [];
for (var i = 0; i < x; i++) {
space[i] = [];
for (var j = 0; j < x; j++) {
space[i][j] = false;
}
}
return space;
}
Also, your for() loop condition was wrong, as you weren't initializing the last element of space. It should have been i < space.length.
And when it's done, it needs to return the array that it created.
Since I got somewhat bored and felt like messing around, you can also initialize your dataset as shown below:
function generateSpace(x) {
return Array.apply(null, Array(x)).map(function() {
return Array.apply(null, Array(x)).map(function() {
return false;
});
});
}
The other functions work equally well, but here's a fairly simply looking one using ES6 that works for any square grid:
function generateSpace(x) {
return Array(x).fill(Array(x).fill(false));
}
I am trying to make two arrays. the unique array can get the elements (no repeats) from the text array, and the counter one can count the frequency of each elements. but something is wrong with the counter one.
var unique_array=new Array();
var counter_array=new Array();
var unique=true;
for (i=0;i<text_array.length;i++){
if (unique_array.length==0){
unique_array.push(text_array[0]);
counter_array.push(1);
}
else if(unique_array.length>0&&unique_array.length<=text_array.length){
for (j=0; j<unique_array.length;j++){
if (text_array[i]==unique_array[j]){
counter_array[j]=counter_array[j]+1;// something wrong with the
alert(counter_array[j]);
var unique=false;
}
}
if (unique==true){
unique_array.push(text_array[i]);
counter_array.push[1];
}
unique=true;
}
You could also simplify the code down using a hashmap and some ES5 higher-order functions:
var text_array = ["a1","a1","a2","a3","a2","a4","a1","a5"];
var counts = {};
text_array.forEach(function(el) {
counts[el] = counts.hasOwnProperty(el) ? counts[el]+1 : 1;
});
var unique_array = Object.keys(counts);
var counter_array=unique_array.map(function(key) { return counts[key]; })
You can do this much more simply using an object. Let the values be the keys of an object, then just increment the count of each property as you go. At the end, you can get an array of the unique keys and their values:
var text_array = ['foo','bar','foo','fum','fum','foo'];
var i = text_array.length;
var obj = {};
while (i--) {
if (obj.hasOwnProperty(text_array[i])) {
obj[text_array[i]]++;
} else {
obj[text_array[i]] = 1;
}
}
console.log('Unique values: ' + Object.keys(obj)); // Unique values: foo,fum,bar
console.log('Value counts: ' + Object.keys(obj).map(function(v){return obj[v]})); // Value counts: 3,2,1
Note that the sorting of counts in the output is purely coincidental.
As Jasvir posted, you can make it pretty concise:
var obj = {};
text_array.forEach(function(v) {
obj.hasOwnProperty(v)? ++obj[v] : obj[v] = 1;
});
But the first example is a bit easier to digest.
I think the approach is what's making it difficult. A hash table / associative array would be much easier to work with.
With a hash table (an object {} in JS), you can store each word in a key and increment the value of the key when you encounter the word again. Then, at the end, just go through the hash table and gather up all the keys which have small values. Those are your unique words.
function get_unique_words(text_array) {
var hash_table, i, unique_words, keys;
hash_table = {};
for(i = 0; i < text_array.length; i++) {
if(hash_table[text_array[i]] === undefined) {
hash_table[text_array[i]] = 1;
} else {
hash_table[text_array[i]]++;
}
}
// go through the hash table and get all the unique words
unique_words = [];
keys = Object.keys(hash_table);
for(i = 0; i < keys.length; i++) {
if(hash_table[keys[i]] === 1) {
unique_words.push(keys[i]);
}
}
return unique_words.sort();
}
console.log(get_unique_words(
['blah', 'blah', 'blah', 'goose', 'duck',
'mountain', 'rock', 'paper', 'rock', 'scissors']
));
Some issues and suggestions :
Don't use var twice for the same variable.
Browsers deal with it ok, but for clarity you should only be declaring your variables once.
Always localize your loop counters - forgetting a var before your i and j will cause them to become global variables.
This is relevant when you have a page with lots of code - all global variables will show up in the debugger's watch list at all times, making it harder to debug your code.)
Use the array literal notation [] instead of the function form Array.
The function form is longer and it's easier to forget the new. It's also easier to read (IMO).
Use more whitespace (it won't bite), such as before and after an equals sign:
var x = 1;
// vs.
var x=1;
It makes the code easier to read and most people don't overdo it.
Indent your code when it's inside a block (e.g. function, if, else, while, for, etc.).
This makes it easier to read the control flow of the code and will help prevent bugs.
Use three equals signs (===) unless you are using loose equality on purpose.
This will help someone looking at your code later (probably yourself) understand better what the test is supposed to be testing.