-3

I want to repeat this process using for loop if I put many more numbers like

var string = ["+919999999999","+918888888888","+917777777777"];

here is my code:

var string = "+919999999999";
document.write(string.slice(-10));
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • 1
    Please [edit] your question to show what research you've done into the issue and what attempts you've made to solve the issue yourself. There are many questions on how to loop through an array on Stack Overflow already... – Heretic Monkey Apr 13 '21 at 18:01
  • thanks sir, now is this right? – Hrithik Mishra Apr 13 '21 at 18:03
  • Does this answer your question? [Loop through an array in JavaScript](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Heretic Monkey Apr 13 '21 at 18:04

4 Answers4

0

Is this what you are trying to achieve?

let strings = ["+919999999999","+918888888888","+917777777777"];

strings.forEach(str => {
  return document.write(str.slice(-10) + "<br/>");
})
Behemoth
  • 5,389
  • 4
  • 16
  • 40
0
var items = ["+919999999999","+918888888888","+917777777777"];

items.forEach(item => document.write(item.slice(-10)))
Dharman
  • 30,962
  • 25
  • 85
  • 135
Byron Alex
  • 82
  • 7
0

here you have 3 different ways

var numbers = ["+919999999999","+918888888888","+917777777777"];
console.log("------ 1 ");
for(let number of numbers){
  console.log(number.slice(-10))
}
console.log("------ 2");

for(var i = 0; i < numbers.length; i++){
  console.log(numbers[i].slice(-10));
}
console.log("------ 3");

numbers.forEach(n => console.log(n.slice(-10)))
SirHectorin
  • 177
  • 11
0

this is a solution as far as i understand the problem

 var string = "+919999999999";
    
    let numToMinus='1',
    str2=string.slice(-10)
    
    for (let i=0;i<str2.length-1;i++){
        numToMinus=numToMinus+'1'
    }
    
    let list=["+919999999999"]
    for(let i=0;i<numToMinus.length-1;i++){
        str2= parseFloat(str2)-parseFloat(numToMinus)
    
        list.push("+91"+str2)
    }
    
    console.log(list)

;

Maarij Bhatti
  • 799
  • 1
  • 5
  • 4