ReferenceError: variable is not defined (but defined) - referenceerror

Consoles say: "ReferenceError: tpyeRand is not defined" (at line "else if")
var type;
var typeRand = Math.random();
typeRand = 0.5;
alert(typeRand);
if(typeRand < 0.7) {
type = "calm";
}
else if(tpyeRand < 0.9) {
type = "fuzzy";
}
else {
type = "angry";
}

You just misspelled the word "typeRand" on that line.

Related

Password Filed Validation not working in Swift 3

I had multiple images based on the text changes in textfield I need to change the image as selected like when user enter capital letter and small letter and special character and number based on that image get selected ..but I can't change according that.
here is my code:
#IBAction func textFieldEditingChanged(_ sender: Any) {
if isValidated(passwordTextField.text!){
print("succ")
}
}
func isValidated(_ password: String) -> Bool {
var lowerCaseLetter: Bool = false
var upperCaseLetter: Bool = false
var digit: Bool = false
var specialCharacter: Bool = false
for char in password.unicodeScalars {
if !lowerCaseLetter {
lowerCaseLetter = CharacterSet.lowercaseLetters.contains(char)
}
if !upperCaseLetter {
upperCaseLetter = CharacterSet.uppercaseLetters.contains(char)
}
if !digit {
digit = CharacterSet.decimalDigits.contains(char)
}
if !specialCharacter {
specialCharacter = CharacterSet.punctuationCharacters.contains(char)
}
}
if (specialCharacter) {
//do what u want
self.SpecialCharacter_img.image = UIImage(named: "GreenTick")
return false
}
if ( digit) {
//do what u want
self.onenumberImageCondiotion_img.image = UIImage(named: "GreenTick")
return false
}
if ( lowerCaseLetter && upperCaseLetter) {
//do what u want
self.UpperCaseImageConditions_img.image = UIImage(named: "GreenTick")
return false
}
else {
self.UpperCaseImageConditions_img.image = UIImage(named: "redtick")
self.SpecialCharacter_img.image = UIImage(named: "redtick")
self.onenumberImageCondiotion_img.image = UIImage(named: "redtick")
return false
}
}
Your code in the isValidated method just needs a little tweaking:
I changed it a little bit and it works in my test project.
func isValidated(_ password: String) -> Bool {
var lowerCaseLetter: Bool = false
var upperCaseLetter: Bool = false
var digit: Bool = false
var specialCharacter: Bool = false
for char in password.unicodeScalars {
if !lowerCaseLetter {
lowerCaseLetter = CharacterSet.lowercaseLetters.contains(char)
}
if !upperCaseLetter {
upperCaseLetter = CharacterSet.uppercaseLetters.contains(char)
}
if !digit {
digit = CharacterSet.decimalDigits.contains(char)
}
if !specialCharacter {
specialCharacter = CharacterSet.punctuationCharacters.contains(char)
}
}
if (lowerCaseLetter) {
lowercaseLabel.text = "✅ Has a lowercase letter"
lowercaseLabel.textColor = .green
lowercaseLabel.sizeToFit()
} else {
lowercaseLabel.text = "Does not have a lowercase letter"
lowercaseLabel.textColor = .red
lowercaseLabel.sizeToFit()
}
if (upperCaseLetter) {
uppercaseLabel.text = "✅ Has an uppercase letter"
uppercaseLabel.textColor = .green
uppercaseLabel.sizeToFit()
} else {
uppercaseLabel.text = "Does not have an uppercase letter"
uppercaseLabel.textColor = .red
uppercaseLabel.sizeToFit()
}
if (specialCharacter) {
specialLabel.text = "✅ Has a special character"
specialLabel.textColor = .green
specialLabel.sizeToFit()
} else {
specialLabel.text = "Does not have a special character"
specialLabel.textColor = .red
specialLabel.sizeToFit()
}
if (digit) {
numberLabel.text = "✅ Has a number"
numberLabel.textColor = .green
numberLabel.sizeToFit()
} else {
numberLabel.text = "Does not have a number"
numberLabel.textColor = .red
numberLabel.sizeToFit()
}
if lowerCaseLetter && upperCaseLetter && digit && specialCharacter {
return true
} else {
return false
}
The last part is important where you have to check that all the conditions are true, or else return false. Also, I removed the return false code in each of the sections where you put '// do what you want' because I don't think the function should return false except at the final check.
Then in the specific 'if statements' where you check for each subcondition being true, add an else statement that will toggle/set your images for either state (e.g. green tick for true, red X for false).
Project Code:
https://github.com/Wattholm/PasswordValidator

How to write If ELSE statement in Google Sheets to Hide columns dependent on cell value

Trying to write and if statement in Google sheets to hide a range of columns dependent on cell value in D4
If D4 == 1 hide columns H:AA
If D4 == 2 hide columns O:AA
Else show A:AA
Code below:
var cell = "D4";
var value1 = 1;
var value2 = 2;
var value3 = 3;
var activeSheet = ss.getActiveSheet();
function HideColumn(); {
If(cell == value1) {
activeSheet.hideColumn("H:AA");
Else If(cell == value2) {
activeSheet.hideColumn("O:AA");
Else If(cell == value3) {
activeSheet.unhideColumn("H:AA");
}
}
}
}
Your code needs some modifications:
var ss=SpreadsheetApp.getActive();
var value1 = 1;
var value2 = 2;
var value3 = 3;
var activeSheet = ss.getActiveSheet();
var cell = activeSheet.getRange("D4").getValue();
function HideColumn() {
if(cell == value1) {
activeSheet.hideColumns(8, (27-8+1));
}
else if(cell == value2) {
activeSheet.hideColumns(15, (27-15+1));
}
else if(cell == value3) {
activeSheet.hideColumns(8, (27-8+1));
}
}
Explanations:
If you want to hide several columns rather than one, you need to use the methods hideColumns(columnIndex, numColumns) (mind the notation)
The if and else if statements should be separate rather than nested (note the brackets)
if and else statements in Apps Script are written in low case
The correct way to obtain the content of a cell is var cell = activeSheet.getRange("D4").getValue();
I hope my answer was helpful for you.

Treating adjacent tracking areas as one contiguous area

I'm trying to present a UI of a title/less window when a mouse leaves a window's title or contentview, but not when moving from one to the other; in essence have the two tracking areas function as one (I resorted to this as I couldn't figure out how to create a single area when the title view is hidden):
override func mouseEntered(with theEvent: NSEvent) {
let hideTitle = (doc?.settings.autoHideTitle.value == true)
if theEvent.modifierFlags.contains(.shift) {
NSApp.activate(ignoringOtherApps: true)
}
switch theEvent.trackingNumber {
case closeTrackingTag:
Swift.print("close entered")
closeButton?.image = closeButtonImage
break
default:
Swift.print(String(format: "%# entered",
(theEvent.trackingNumber == titleTrackingTag
? "title" : "view")))
let lastMouseOver = mouseOver
mouseOver = true
updateTranslucency()
// view or title entered
if hideTitle && (lastMouseOver != mouseOver) {
updateTitleBar(didChange: !lastMouseOver)
}
}
}
override func mouseExited(with theEvent: NSEvent) {
let hideTitle = (doc?.settings.autoHideTitle.value == true)
switch theEvent.trackingNumber {
case closeTrackingTag:
Swift.print("close exited")
closeButton?.image = nullImage
break
default:
Swift.print(String(format: "%# exited",
(theEvent.trackingNumber == titleTrackingTag
? "title" : "view")))
let lastMouseOver = mouseOver
mouseOver = false
updateTranslucency()
if hideTitle && (lastMouseOver != mouseOver) {
updateTitleBar(didChange: lastMouseOver)
}
}
}
Additionally, there's a tracking rect on the close button to appear only when over. Anyway, from my tracer output I see the issue - mouse over window from beneath to over its title:
view entered
updateTitleBar
**view entered**
view exited
updateTitleBar
title entered
updateTitleBar
title exited
Note sure why I'm getting a second view entered event (view entered), but the movement out of the view and onto the adjacent title each triggers an updateTilebar() call which is visually stuttering - not remedied so far with animation:
fileprivate func docIconToggle() {
let docIconButton = panel.standardWindowButton(.documentIconButton)
if settings.autoHideTitle.value == false || mouseOver {
if let doc = self.document {
docIconButton?.image = (doc as! Document).displayImage
}
else
{
docIconButton?.image = NSApp.applicationIconImage
}
docIconButton?.isHidden = false
self.synchronizeWindowTitleWithDocumentName()
}
else
{
docIconButton?.isHidden = true
}
}
#objc func updateTitleBar(didChange: Bool) {
if didChange {
Swift.print("updateTitleBar")
if settings.autoHideTitle.value == true && !mouseOver {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = 1.0
panel.animator().titleVisibility = NSWindowTitleVisibility.hidden
panel.animator().titlebarAppearsTransparent = true
panel.animator().styleMask.formUnion(.fullSizeContentView)
}, completionHandler: {
self.docIconToggle()
})
} else {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
context.duration = 1.0
panel.animator().titleVisibility = NSWindowTitleVisibility.visible
panel.animator().titlebarAppearsTransparent = false
panel.animator().styleMask.formSymmetricDifference(.fullSizeContentView)
}, completionHandler: {
self.docIconToggle()
})
}
}
}
So my question is about how to defer the actions when areas are adjacent.
They (titlebar & content view) are not siblings of each other so didn't think a hitTest() was doable but basically if I could tell if I was moving into the adjacent tracking area, I'd like it to be a no-op.
Any help with why animation isn't working would be a plus.
Not a true answer, but if you know the adjacent view's rect you can use the event's location to probe whether you'd want to ignore movements among adjacent views:
override func mouseExited(with theEvent: NSEvent) {
let hideTitle = (doc?.settings.autoHideTitle.value == true)
let location : NSPoint = theEvent.locationInWindow
switch theEvent.trackingNumber {
case closeTrackingTag:
Swift.print("close exited")
closeButton?.image = nullImage
break
default:
let vSize = self.window?.contentView?.bounds.size
// If we exit to the title bar area we're still "inside"
// and visa-versa, leaving title to content view.
if theEvent.trackingNumber == titleTrackingTag, let tSize = titleView?.bounds.size {
if location.x >= 0.0 && location.x <= (vSize?.width)! && location.y < ((vSize?.height)! + tSize.height) {
Swift.print("title -> view")
return
}
}
else
if theEvent.trackingNumber == viewTrackingTag {
if location.x >= 0.0 && location.x <= (vSize?.width)! && location.y > (vSize?.height)! {
Swift.print("view -> title")
return
}
}
mouseOver = false
updateTranslucency()
if hideTitle {
updateTitleBar(didChange: true)
}
Swift.print(String(format: "%# exited",
(theEvent.trackingNumber == titleTrackingTag
? "title" : "view")))
}
}

How to use elseif statement to show hidden text

New to Javascript, need some help with hidden panels.
I want the user to answer some questions in a radio button form which will have different values assigned to the checked answers. There will be 3 different options of results in hidden panels, one result will be displayed at the end depending on the score.
Link to full code and html here
function results() {
var lRisk = document.getElementById('lowRisk')
var mRisk = document.getElementById('mediumRisk')
var hRisk = document.getElementById('highRisk')
if ((score >= 0) && (score =< 15)) {
lRisk.style.display = 'inline';
} else if ((score >= 16) && (score <= 25)) {
mRisk.style.display = 'inline';
} else {
lRisk.style.display = 'inline';
}
Would be very grateful for any help/advice!
If you need to display only ONE control based on the score, you should make all of them hidden in the start then do your conditions.
function results() {
var lRisk = document.getElementById('lowRisk')
var mRisk = document.getElementById('mediumRisk')
var hRisk = document.getElementById('highRisk')
lRisk.style.display = 'none';
mRisk.style.display = 'none';
hRisk.style.display = 'none';
if (score >= 0 && score <= 15) {
lRisk.style.display = 'inline';
} else if (score >= 16 && score <= 25) {
mRisk.style.display = 'inline';
} else {
hRisk.style.display = 'inline';
}
}

How can I calcuate wrapping text height in a famo.us surface?

Within famo.us, I want to place a variable-length wrapping title near the top then have images etc follow. All items must be their own surface as some have click events and animations but all the positioning must be super fast to calculate and place based on the text height and of course must avoid DOM access for the 60fps. This must happen for a series of mini-posts, streaming real time and for infinite scroll.
So far, I came up with an approach that works using an ascii character map to pixel width load off screen on init if isn't already in localStorage. It uses jquery to do the sizing for each character, then determine line height by checking a breaking word and the height of that box. After that I use this map for calculations on the fly so I don't touch DOM again. It seems to work fine but is completely dependent on knowing the style of font going to be used in a very specific way. For each new style, I would have to have a different mapping which sucks.
Is there another approach that is more built in to famo.us?
I'm answering my own question because its frankly been awhile, the new framework is going to come out June 22nd of this month (at the time of the this writing). Just going to put how I'm doing this currently which is the way I described above.
Here's the 0.3.5 way of Famou.us to do this.
This is in a file "utils/lines.js" I whipped up. Hack, yes. Works, yes.
define(function(require) {
'use strict';
var $ = require('jquery');
var cache = require('utils/cache');
function getLineInfo(id) {
var cacheKey = 'lineInfo_' + id;
var savedLineInfo = cache.get(cacheKey);
if (savedLineInfo)
return savedLineInfo;
var charStart = 32; // space
var charEnd = 126; // ~
var $charDump = $('<div>', { id: id });
$('body').append($charDump);
for (var i = charStart; i <= charEnd; i++)
$charDump.append($('<div>', { class: 'char', css: { top: (i-32) * 20 } }).html('&#' + i));
var charMap = {};
$charDump.find('.char').each(function() {
var $this = $(this);
var char = $this.text().charCodeAt(0);
var w = $this.width();
charMap[char] = w;
});
$charDump.html('<div class="line">abc<br>123<br>456</div>');
var lineHeight = $charDump.height() / 3;
$charDump.remove();
savedLineInfo = {
lineHeight: lineHeight,
charMap: charMap
};
cache.set(cacheKey, savedLineInfo);
return savedLineInfo;
}
function getLines(text, width, id, maxLines) {
var lineInfo = getLineInfo(id);
var cleaned = $.trim(text.replace(/\s+/gm, ' '));
var words = cleaned.split(' ');
var lines = 1;
var lineWidth = 0;
var spaceLength = lineInfo.charMap[32];
var allLines = '';
words.forEach(function(word) {
var wordLength = 0;
word.split('').forEach(function(char) {
var charLength = lineInfo.charMap[char.charCodeAt(0)];
if (!charLength)
charLength = spaceLength;
wordLength += charLength;
});
if (lineWidth + wordLength + spaceLength <= width) {
lineWidth += wordLength + spaceLength;
if (maxLines)
allLines += word + ' ';
}
else {
lineWidth = wordLength;
if (maxLines && lines <= maxLines)
allLines += word + ' ';
else if (maxLines)
return {text: allLines.substr(0, allLines.length - 3) + '...', lines: lines};
lines++;
}
});
if (maxLines)
return {text: allLines, lines: lines};
return lines;
}
function getTextHeight(text, width, id, maxLines) {
var lineInfo = getLineInfo(id);
var info;
if (maxLines) {
info = getLines(text, width, id, maxLines);
info.height = lineInfo.lineHeight * info.lines;
return info;
}
return lineInfo.lineHeight * getLines(text, width, id);
}
return {
getLines: getLines,
getLineInfo: getLineInfo,
getTextHeight: getTextHeight
};
});