How do I show this for loop is running in reverse through the use of console.log?

How do I show this for loop is running in reverse through the use of console.log?
0

#1

I have done the following:
function longestWord (sentenceAnalyze1R) {
console.log(sentenceAnalyze1R);
var eachword = 0;
var WordList = sentenceAnalyze1R.split(’ ');

for(var i=WordList - 1; i<0; i--)

I am so close but I need the extra help.
The link that is very similar to this is here:

There is one difference, it is the initial variable:
var words: 'The cargo went to the hound’
I’m trying to find the longest word that is also the last word. I think it is the exact same process. I just don’t know how to count in reverse.


#2

I have asked other questions about iterators but I need the help with understanding this one.


#3

By putting the console.log() inside the loop.

When you share code in the forums, please format it.


#4
var = 'hectameters, mm, dm, meter, centimeter'
 /// assume that this file is running in the background and if you get the longest word 
that is also the last word, you should be able to display it and then you will pass the challenge
but you have to run a for loop as part of the instructions

function longestcount (measureAnalyze1R) {
  console.log(sentenceAnalyze1R);
  var counter = 0;
  var LastWord = [];
  var WordList = measureanalyze1R.split(' ');
  console.log(WordList);
for(var i=WordList.length-1; i<0; i--); {
    if (WordList[i].length>counter){counter=WordList[i].length;

                                   

    };
console.log(WordList[i]);
return counter  

I am trying to find the last and the longes word. I have learned the format to find the last word. Can I use -= to find the longest word?


#5

I apologize about the formatting and I am trying to format every time now.


#6
  • Do you need to return the longest word or just the length of the longest word?
  • It doesn’t look like you have a matching number of { and }s
  • You have a semicolon where one doesn’t belong.

I don’t understand the question.


#7

The question is that I want to find the longest word but that the array has to count backwards and it has to be the last word. notice that centimeter and hectameter have the same length? Hold on, I’m rearranging it


#8

You’ve been doing this for a while. I’m sure you can find it.


#9

I have figured out how to count backwards, yes! but I was wondering if I could count down using -=? I’m so close! Please help.


#10

I still don’t know what you mean by “count down using -=”?

Do you mean something like:

for (let i = 12; i>0; i -=3) {
    console.log(i); // 12, 9, 6, 3
}

:question:


#11

I can give you an example:

var multiplesOfThreeSums = 0;
var numberCounter = 0;
for (var i=1;i<=300;i++){
if (i % 3 === 0){
numberCounter++;
multiplesOfThreeSums += i;
}
}

This goes three hundred cycles by multiples of three
For example:
first iteration: 3
secont iteration: 9 (3+6) — 6 plus the previous term
third iteration: 18 (9+9) ----9 plus the previous term

could I not count backwards using - = in the condition part?


#12

- = is bolded and spaced for arguments sake for right now, wont be like that in the code. Im trying to see if I can count down from a “ceiling” instead of up to it


#13

Why don’t you try it and see what happens? If you’re confused about the results you get, it would be easier to answer those questions.


#14

I’m currently working on it give me a sec


#15
var = 'hectameters, mm, dm, meter, centimeter'
 /// assume that this file is running in the background and if you get the longest word 
that is also the last word, you should be able to display it and then you will pass the challenge
but you have to run a for loop as part of the instructions

function longestcount (measureAnalyze1R) {
  console.log(sentenceAnalyze1R);
  var counter = 0;
  var LastWord = [];
  var WordList = measureanalyze1R.split(' ');
  console.log(WordList);
for(var i=WordList.length-1; i<0; i--); {
    if (WordList[i].length-1;counter){counter=WordList[i].length; counter--

    };
console.log(WordList[i]);
return counter

I’m trying to use counter at the end but the -1 is overriding it. but if I remove the -1 it counts it (the loop) forward. I would like to use a similar approach as the link I posted, How do I go about converting an array (in this case a string) with multiple strings to a single string?
That admin person uses ++; im 100% sure you can use – at the end


#16

You have a number of syntax and logical errors in your code.
The syntax errors that I mentioned before are still there. Also you are doing for(var i=WordList.length-1; i<0; i--). Look very closely at that. Your for loop will never run unless WordList is empty, in which case it will run infinitely.


#17

Im just confused with that statement because the for loop actually runs and it runs backwards, but it doesnt count consecutively (from the last index to the first index) but i’m checking the other parts.


#18

What is the psuedocode associated with what is happening there? So that I can have some direction. Not the answer neccesarily, but what is currently happening. I’m stepping out for a second but I am definitely interested in continuing to pursue this topic so I will be back.


#19

Would that also work with an array of 12?


#20

What would the array of twelve look like? And what would you want to do with the array or the elements of the array? Do you want to subtract 3 from each element?

In general, you need to be more specific when asking questions, so we can give you the best answers.