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

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] + '%';
}
}

Related

find the number of unique numerical elements in any array [duplicate]

This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Count unique elements in array without sorting
(9 answers)
Closed 14 days ago.
have made some code but it is broken and gives errors. the code is supposed to find the number of unique numerical elements in any array. So far, i created the following code snippet which contains a uniqueLength() function to return the number of unique elements.
function uniqueLength(nums) => {
if(nums.length === 0) {
return 0;
}
let i=0;
while(j>nums.lengths) {
if(nums[j]] ==! nums[i]) {
i++;
nums[i] = nums[j];
j++;
} else {
j++;
}
}
give i+1;
}
// Should return 5
const result = uniqueLength([1,1,2,3,4,5,5]);
console.log(result);
// Should return 1
const result2 = uniqueLength([1,1,1,1]);
console.log(result2);
where there is dashes one of these hints should go there
return
nums[j]
!=
nums[j] == nums[i]
nums[i]
give
j > nums.length
returns
let j=1;
=
**!==
j < nums.length**

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)
}

How to Add specific values to an array? [duplicate]

This question already has answers here:
Why does console.log return undefined after correct output?
(1 answer)
Javascript even and odd range
(6 answers)
How to append something to an array?
(30 answers)
Closed 9 months ago.
I am creating a function which takes in a minimum and max value. The output should list all the even numbers between these two parameters. I have attempted at creating an array to display these numbers but I get this output:
evenNumbers(4,13) returns: undefined
evenNumbers(3,10) returns: undefined
evenNumbers(8,21) returns: undefined
How to fix my code so it shows the list properly?
var evenNumbers = function(minNumber, maxNumber){
var list = [];
for (let i = minNumber; i < maxNumber; i++){
if (i % 2 == 0){
for(let k = 0; k < maxNumber; k++){
i = list[k];
}
}
}
return console.log(list);
}
console.log('evenNumbers(4,13) returns: ' + evenNumbers(4,13));
console.log('evenNumbers(3,10) returns: ' + evenNumbers(3,10));
console.log('evenNumbers(8,21) returns: ' + evenNumbers(8,21));

creating a javascript function to reverse a word [duplicate]

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];
}

rotate an array in JavaScript [duplicate]

This question already has answers here:
Rotate the elements in an array in JavaScript
(42 answers)
How to randomize (shuffle) a JavaScript array?
(69 answers)
Closed 8 years ago.
I want to rotate my entire array, for example:
[1,2,3,4] becomes [3,4,1,2]
my current function is:
function shuffle(o){
for(i = 0; i<Math.floor((Math.random() * o.length)); i++){
o.push(o.shift());
}
};
Please tell me what I am doing wrong.
function shuffle(o){
for(i = 0; i < o.length; i++)
{
var Index = o.indexOf(i);
index=index+2;
if(index>3)
{
index=index-2;
var item=o.splice(index,1)
o.push(item);
}
else
{var item=o.splice(index,1)
o.push(item)
}
}
};
Your current function just shifts it by a random amount, i.e. it offsets it by a random amount.
Instead, you want to randomly pick from the array and move that element. Try this (untested):
function shuffle(o){
for(i = 0; i < o.length; i++){
var randomIndex = Math.floor(Math.random() * (o.length - i));
var item = o.splice(randomIndex, 1);
o.push(item);
}
};
EDIT: It seems there's some confusion about what you're trying to accomplish. My answer above assumes you mean shuffle (randomize the order of elements in the array).

Categories

Resources