Reverse The Odd Words in the sentence [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I am trying to reverse the odd words in the sentence. Its working fine. But its appending undefined in the front.
var str = "get busy living or get busy dying.";
var newstr = str.split(" "), result;
for(i=0;i<newstr.length;i++){
if(i%2 !== 0){
result += newstr[i].split("").reverse().join("");
result += ' ';
} else {
result += newstr[i];
result += ' ';
}
}
Output is
undefinedget ysub living ro get ysub dying.
Can some one point me where i am going wrong!!!!!

You're not initializing result, so the first += adds something to an undefined value.
Just declare and initialize result:
var result = "";

You could also do it more concisely:
var str = "get busy living or get busy dying.";
var result = str.split(" ").map(function(word, i) {
return i % 2 == 0 ? word : word.split("").reverse().join(""); }).join(" ");
alert(result);

Related

Why is my JavaScript code not accepted as the right answer? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to do this Javascript exercise: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
I am wondering why the solution below is not an accepted answer:
let count = 0;
function cc(card) {
// Only change code below this line
const low = [2, 3, 4, 5, 6];
const high = [10, 'J', 'Q', 'K', 'A'];
if (low.includes(card)) {
count += 1;
}
else if (high.includes(card)) {
count -= 1;
}
let decision;
if (count > 0) {decision = "Bet"}
else {decision = "Hold"}
return count + decision;
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
When I am comparing it to accepted answers I don't see what they are doing differently. One thing that is not clear to me in the assignment is that should return be called every time or only after the last function call (cc('A');).
Add a space between count and decision
return count + " " + decision;
You are giving an answer in the wrong format. Just missing the space between count and decision.
Incorrect:return count + decision;
Correct:return count +" "+ decision;

javascirpt return TypeError: undefined Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm executing this code, although it produces an error.
function getUserPosts(){
$.get("../php/PostHandler.php", function(data) {
var posts = JSON.parse(data);
alert(posts);
for(var x = lastNumPosts; x <= posts.length; x++){
var postLine = "<div class='user-post'><div class='user-p-info'><img src='imgs/email-icon2.png'><h2> "+ posts[x].name +" </h2><br> <small>Posted Tuesday 23/10</small></div><div class='user-p-content'><p>" + posts[x].pContent + "</p></div></div>";
$('.post-zone').prepend(postLine);
}
});
}
TypeError: posts[x] is undefined
Any solutions?
Change this line:
for (var x = lastNumPosts; x < posts.length; x++) {
Notice the < instead of <=.

Value not assigned in while loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
So in the following code a= -1 so the statement in the while loop should be -1, but the while loop is never entered. I have looped through but don't understand why the while loop is never executed. I am sure this is frustratingly simple.
function translatePigLatin(str) {
var newStr = str.split("");
var vowels = ["a","e","i","o","u"];
var i = 0;
var a = vowels.indexOf(newStr[0]);
while (vowels.indexOf(newStr[i]) != -1) {
i = i++;
}
if(i===0){
return str + "way";
}
else{
str = str.substr(i) + str.substr(0,i) + "way";
}
//return str;
}
translatePigLatin("consonant");
In the while loop you are checking vowels.indexOf(newStr[i]) != -1 which mean that if vowels.indexOf(newStr[i]) is not -1 then while loop should bve executed. and as you say the expression results in -1 the loop should not be executed which is a expected behaviour.
Change the condition to
vowels.indexOf(newStr[i]) == -1 if you want to execute the while loop is the value returned is -1

Javascript: I am trying to write a calculate function that will "add" two numbers and print the result out as a string, but my syntax isn't working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to write a calculate function that will "add" two numbers and print the result out as a string, but my syntax isn't working. Can you please look at my code and tell me how I can fix it? Later I would like to add more mathematical functions like divide and multiple that can be plugged in. Thanks.
var add = function(x,y){
return x+y;
};
var calculate = function(string,x,y){
if(string === "add"){
var result = console.log(x + "+ " y + "= " + add(x,y));
return result;
}
};
calculate("add",5,6);
You have missed a + sign before y.
This:
var result = console.log(x + "+ " y + "= " + add(x,y));
must be
var result = console.log(x + "+ " + y + "= " + add(x,y));
Anyway, your function does not look correct. console.log returns nothing, and your result variable will always be evaluated to undefined, and function will always return undefined as well.
Also, your calculate function does actually calculate and output result which can be not that transparent for someone who calls this method and breaks single-responsibility principle.
Probably, it is a better idea to separate calculation and presentation:
function add(x,y){
return x+y;
}
function calculate(action,x,y){
if(action === "add") {
return x + "+ " + y + "= " + add(x,y);
}
}
var result = calculate("add",5,6);
console.log(result);
You may also want to replace if (action === "add") with switch statement in the future when you will have more than two actions :)

how to find no. of similar characters in two strings in javascript [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to find the no. of similar characters between two strings using JS/PHP
Example
str1: Mack
str2: Michelle
Similar Characters: "M" "C"
similar character count: 2
I'll do this:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0;
var min = Math.min(str1.length, str2.length);
for(var i = 0; i < min; i++)
{
if(str1.charAt(i) === star2.charAt(i))
counter++;
}
alert("Result: "+counter);
After your precision, here is my solution:
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
var counter = 0, find = -1;
for(var i = 0; i < str1.length; i++)
{
find = str2.indexOf(str1.charAt(i));
if(find > -1)
{
counter++;
str2 = str2.substr(0, find) + str2.substr(find + 1);
}
}
alert("Result: "+counter);
It works with the 2 examples you gave us:
Michellle and Michelle = 8
Sneha and naveen = 3
Use similar_text() function
<?php
echo "character count: ". similar_text("Mack","Michelle");
?>
Output
character count: 2
I'd need more examples to make sure this works, but using regular expressions might work for you:
function similar_text_regex(str1, str2){
var regEx = new RegExp('['+str2+']', 'gi');
var matchCnt = str1.match(regEx2).length;
return matchCnt;
}
console.log(similar_text_regex('Michelle', 'Michellle')); // --> 8
console.log(similar_text_regex('Jason', 'Jane')); // --> 3
console.log(similar_text_regex("sneha","naveen")); // --> 3

Categories