creating a javascript function to reverse a word [duplicate] - javascript

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 7 years ago.
Hi am trying to create a javascript function to reverse a word, but it seems the for loop is not begin executed and the holder variable is not being append in the for loop.
function jump(str){
var holder ="";
var len = str.length-1;
for(var i =len; i == 0; i--){
holder += str[i];
}
return holder;
}
console.log(jump("Just do it!"))

Your loop is incorrect:
for(var i =len; i == 0; i--){
^^^
The loop body only fires if that middle condition is "true". On its first iteration, i is something like 10, which means 10 == 0 is NOT true, aka false.
You probably want
for(var i =len; i >= 0; i--){
instead.

I think this should work for you
var text = 'Just do it!';
var rev = text.split("").reverse().join("").split(" ").reverse().join(" ");

var result = str.split("").reverse().join("");

The loop
for(var i =len; i == 0; i--){
holder += str[i];
}
will only run when i is equal to zero - which won't be the case, since you set it up as the length of your (presumably) populated string. Try:
for(var i =len; i >= 0; i--){
holder += str[i];
}

Related

I want to know that how can I print each letter of my string by using for loop in javascript [duplicate]

This question already has answers here:
How can I process each letter of text using Javascript?
(24 answers)
Closed 4 months ago.
I can't print each letter of my name
let str = "Ashman"
console.log(str[0])
for ( let i = 0 ; str<str.length; i++) {
console.log(str)
}
You are almost correct.
console.log(str[i]) should work and str<str.length should be i<str.length
let str = "Ashman";
for (let i = 0; i < str.length; i++) {
console.log(str[i])
}
To access each letter you need to insert your console.log(str[0]) inside for loop and change the [0] using your for loop variable.
let str= "Ashman";
for(let i = 0; i < str.length; i++){
let x = str[i];
console.log(x)
}

creating a for-loop that prints # signs instead of number values [duplicate]

This question already has answers here:
How to print a half pyramid in javascript
(9 answers)
How to print star pattern in JavaScript in a very simple manner? [closed]
(22 answers)
Closed 1 year ago.
im trying to write a code that outputs lines of '#' signs that increase by one for each value until the input number is reached.
for example, when triangles(3) is called my desired output is:
'#'
'##'
'###'
ive learned how to create this sequence with number values instead of the '#' signs but not sure how to achieve this output displayed above. thanks!
function triangles(num) {
let number = '';
for (let i = 1; i <= num; i++) {
console.log(number += i);
}
}
triangles(5)
this outputs:
'1'
'12'
'123'
'1234'
'12345'
You can use String.prototype.repeat() to repeat the pound (#) character as specified by the current value of i, without needing to declare a new variable to store it:
function triangles(num) {
for (let i = 1; i <= num; i++) {
console.log('#'.repeat(i));
}
}
triangles(5);
function triangles(num) {
let symbol = "#";
for (let i = 1; i <= num; i++) {
console.log(Array(i).fill(symbol).join(""));
}
}
triangles(5);
function triangles(num) {
const c = "#";
for (let i = 1; i <= num; i++) {
console.log(c.repeat(i));
}
}
triangles(5)
You don't have to use console.log to print number += i specifically. If you want to print '#' multiple times, you can use #Terry's answer (which is shorter) or you can use another for loop in your code (this approach could be simpler for beginners).
function triangles(num) {
let number = '';
for (let i = 1; i <= num; i++) {
number++;
let poundkeys = '';
// dont use i again, name it something else (like j or k)
for (let j = 1; j <= number; j++) {
// just like you do number += i, add a pound symbol to poundkeys
poundkeys += '#';
}
// now, log poundkeys
console.log(poundkeys);
}
}
triangles(5);
Also, a quick tip. Generally, it is better not to log number += i. Instead of:
console.log(number += i);
It is encouraged and more readable if you separate them:
number += i;
console.log(number);

Javascript: iterate through array and add % after each element except last [duplicate]

This question already has answers here:
How to convert array into comma separated string in javascript [duplicate]
(3 answers)
Closed 2 years ago.
I want to add a % after each element in the array except the last. So far I have come up with this:
var array = [a, b, c];
for(var i=0; i<array.length; i++) {
var outcome += array[i] + '%';
}
Outcome:
a%b%c%
How can I fix this so the % does not appear at the end of the outcome?
You can use the Array.prototype.join method in order to get what you're after:
console.info(['a', 'b', 'c'].join('%'))
Check if the current element (value of i) is not the last element. If it's the last element don't concatenate a %, for all others concatenate with the %.
for(var i = 0; i < arr.length; i++) {
if(arr[i] < arr.length -1) {
var outcome += arr[i] + '%';
}
}

Any alternative way of using this .charAt()?

I have a problem, when I used input[index] in the statement the reverse word didn't match the value of inputString. But this works in C#. When I try it in JavaScript is not working. I want to learn what are the other alternative way of .char() method. Can anybody solve my problem?
strReverse = "";
input = document.getElementById("inputValue").value;
i = input.length;
for(var j = i; j >= 0; j--) {
strReverse = strReverse + input.charAt(j); // i used input[index]
}
If you wish to use input[index] instead of input.charAt() then you need to address the issue that you are setting i = input.length;
So when you enter your loop for the first iteration, j will be equal to i. Meaning that you are trying to access the character at the index equal to the length of the string (when you do input[j]). However, as arrays and string indexing starts at zero in javascript (and in most languages), there is no index at i (the length of the string), but, there is an index at i-1, which will give the last character in the string. Thus, if you want to reverse your array using input[j] you need to start j in your for loop as j = i - 1.
Take a look at the snippet for a working example:
strReverse = "";
input = "level";
i = input.length;
for(var j = i-1; j >= 0; j--) {
strReverse = strReverse + input[j]; // i used input[index]
}
if(strReverse === input) {
alert(input +" is a palindrome!")
} else {
alert(input +" is not a palindrome!");
}

Using for loop to get for specific iteration result in javascript

I need help in somewhere in javascript
var obj = jQuery.parseJSON(data.d);
This ajax call result returns some data (around 31).
Here is an example:
obj[0] = 10, obj[1]=20 ,obj[2]=30 , obj[4]=21,obj[5]=16,obj[6]=54 here I want to get value of
obj[0] and ob[4] using for loop . And I also need to do this for obj[10] and obj[14] the difference will be 5 between the i values .
Any idea or help?
You can do something like this (a pseudo code)
int i = 0;
while(i < 31)
{
print(i);
i = i + 4;
}
If i understood, you want to skip 5 on each step:
var array = 'abcdefghijklmnopqrstuvwxyz'.split(''); // something that generates an array just to show an example
var i = 0;
for (; i < array.length;) {
console.log(i, array[i]);
i += 5;
}
see fiddle
var iterator=0;
for (i = 0; i < obj.length; i++) {
if(i == iterator){
alert(obj[iterator]);
iterator= iterator + 4;
}
}

Categories

Resources