-1

while loop with a if statement is not printing my if statement

var rest = 0
while(rest <=10) {

   rest ++;
   console.log(rest);

   if (rest == 10)
      console.log('Done!')


  • 1. [`=` is assignment, not equality check.](https://stackoverflow.com/questions/11871616/what-is-the-difference-between-the-and-operators-and-what-is-si) 2. This is not how [`switch` should be written](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) – VLAZ Dec 04 '21 at 16:37
  • soooo ... can you help me ? – João Guimarães Dec 06 '21 at 18:52
  • you cannot change the whole question a few months after you posted it, you must create a new one. The goal behind StackOverflow is that people with the same question as you can go to this page, see the answer you got and not ask it again on SO or another forum. If you change the question people won't be able to know if it solved their problem. – Oriun Mar 14 '22 at 09:56

3 Answers3

0

A simpler way to approach this would be to use Date#getDay() to get the 0-6 index of the current day within the week and store the suffixes in an array with same indexing.

Then use the day index to retrieve the associated suffix.

const suffixes = ['snd','mnd','tsd','wdnsd','thrsd','frd','strd']

const weekPass = 'ABC_';

const day = (new Date()).getDay();

const pass = weekPass + suffixes[day];

console.log('Day index =',day, ', Passs =',pass )
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • this is what they ask me to do : "Having got the value of the weekly password stored in weeklyPass, update and print to the console the value of currentPass depending on the day of the week we are in. To know which day of the week it is simply access weekDay. Remember that updating the password is appending the letters corresponding to the consonants present in the name of the current day of the week. There are many ways of cracking this problem, but Elliot and the guys specifically asked for you to use a switch statement..." – João Guimarães Dec 04 '21 at 17:22
  • Well as mentioned in comments above you are not using the switch correctly. Also be careful with case differences ... "Friday" does not equal "friday" – charlietfl Dec 04 '21 at 17:26
0

Or just this

const currentPass = "aaa",
  weekDay = new Date().getDay(), // 0-6 (sun-mon)
  dayPass = currentPass + ['snd', 'mnd', 'tsd', 'wdnsd', 'thrsd', 'frd', 'strd'][weekDay];
  
  console.log(dayPass)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • this is what they ask me to do : "Having got the value of the weekly password stored in weeklyPass, update and print to the console the value of currentPass depending on the day of the week we are in. To know which day of the week it is simply access weekDay. Remember that updating the password is appending the letters corresponding to the consonants present in the name of the current day of the week. There are many ways of cracking this problem, but Elliot and the guys specifically asked for you to use a switch statement..." – João Guimarães Dec 04 '21 at 17:22
0

Considering what you wrote, the first step to get it working would be to fix the switch syntax and the assignment of currentPass like this:

var weekDay = 'Friday';
var currentPass = "this_week_password"; // this is the values they gave me
switch (weekDay) {
    case 'Monday':
        currentPass += 'mnd';
            break;
    case 'Tuesday':
        currentPass += 'tsd';
            break;
    case 'Wednesday':
        currentPass += 'wdnsd';
            break;
    case 'Thursday':
        currentPass += 'thrsd';
            break;    
    case 'Friday':
        currentPass += 'frd';       
            break;
    case 'Saturday':
        currentPass += 'strd';
            break;
    case 'Sunday':
        currentPass += 'snd';
            break;
    }

console.log(currentPass)
Oriun
  • 245
  • 1
  • 7