2

Not sure if my code is working, but the error I'm seeing is "'result' is declared but its value is never read.ts(6133)"

This is just an introductory exercise, so I'm open to suggestions/guidance on how the same thing can be accomplished. The way I've done it is three IF statements for the event type with nested IF statements for each temperature range.

let eventType = window.prompt("What type of event are you going to?");
let tempFahr = window.prompt("What is will the temperature be?");


if (eventType=='casual') { // casual event

    if (tempFahr < 54) { // temp less than 54
        let result = 'Since it is ' + tempFahr + ' and you are going to a ' + eventType + ' event, you should wear something comfy and a coat';
    } else if (54 < tempFahr < 70) { // temp between 54 and 70
        let result = 'Since it is ' + tempFahr + ' and you are going to a ' + eventType + ' event, you should wear something comfy and a jacket';
    } else { // temp more than 70
        let result = 'Since it is ' + tempFahr + ' and you are going to a ' + eventType + ' event, you should wear something comfy and no jacket';
    }
} else if (eventType=='semi-formal') { // semi-formal event
    if (tempFahr .. (Etc.)...


``````````````

    {
        let result = 'Since it is ' + tempFahr + ' and you are going to a ' + eventType + ' event, you should wear a suit and no jacket';
    }
}

    console.log(result);
paulsm4
  • 114,292
  • 17
  • 138
  • 190
uuoo
  • 27
  • 5

1 Answers1

4

let is block-scoped - you'd need to declare result outside of the if statements:

let eventType = window.prompt("What type of event are you going to?");
let tempFahr = window.prompt("What is will the temperature be?");
let result = "";
if (eventType == "casual"){
    if (tempFahr < 54){
        result = 'Since it is ' + tempFahr + ' and you are going to a ' + eventType + ' event, you should wear something comfy and a coat';
    }
    //...
}
else if (eventType == "semi-formal") {...}
console.log(result);

Block scoping means that declaring with let or const inside a "block" of code {} will not exist outside of the block, only inside it. So let result = "..." inside an if statement means that the result will be removed, and any others will be completely different. They will be garbage collected because there's no reference to them outside the if statement.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 1
    Yup. Another way of saying the same thing: each of the `let result = ` statements is a *DIFFERENT "result"*. None of which are ever actually used :( – paulsm4 May 08 '19 at 00:11
  • Yes, that's true @paulsm4, I'll add that. – Jack Bashford May 08 '19 at 00:12
  • I've done that now and I can't find any changes. VSCode is still greyed out and showing "'result' is declared but its value is never read.ts(6133)" when I hover over the result variable (but I understand that might be more of an error with VSCODE and not the code itself. – uuoo May 08 '19 at 00:13
  • Another output error I'm getting is "ReferenceError: prompt is not defined" – uuoo May 08 '19 at 00:15
  • You may be in the wrong environment - is it an external file? Node.JS script? – Jack Bashford May 08 '19 at 00:16
  • Apologies, I hadn't removed the multiple "let" instances. The initial error is now solved, thank you. – uuoo May 08 '19 at 00:16
  • @JackBashford, thanks for the help on the code. I'll need to figure out the environment, so thanks for also pointing me in the right direction. – uuoo May 08 '19 at 00:18
  • Will do, I'm waiting for the timer to expire so I can. – uuoo May 08 '19 at 00:21